Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda@Edge example only works on us-east-1 region #635

Closed
yvele opened this issue Oct 23, 2018 · 21 comments
Closed

Lambda@Edge example only works on us-east-1 region #635

yvele opened this issue Oct 23, 2018 · 21 comments

Comments

@yvele
Copy link

yvele commented Oct 23, 2018

I'm unable to use the Lambda@Edge example with CloudFront being set on an other region than us-east-1.

Because a Lambda@Edge function must be in region us-east-1:

The function must be in region 'us-east-1'. ARN: arn:aws:lambda:eu-west-1:xxxxxx:function:OriginRequestLambdaFunct-xxxxxx:1 (Service: AmazonCloudFront; Status Code: 400; Error Code: InvalidLambdaFunctionAssociation; Request ID: xxxxxx)

Can you please add a Lambda@Edge example workaround, or recommendations that allows CloudFront and Lambda@Edge to be set up on different regions?

Maybe 2 separate Cloudformation templates?

This issue is related to #323 and #565

PS: Other people on Twitter also expresses this needs

image

Edit: Workaround here #635 (comment)

@timoschilling
Copy link
Contributor

I'm planning to solve that Issue via an CloundFormation custom resource which deployes that lambda to the other region, but it would be nice if SAM Transforme will so that out of the box for us.

@yvele
Copy link
Author

yvele commented Oct 23, 2018

@timoschilling do you have some code tho share? I'm interested

@yvele
Copy link
Author

yvele commented Oct 23, 2018

@timoschilling how do you make you custom resource work with something like CodeUri that should detect code change? 🤔 CodeUri is processed by AWS SAM right?

Should we try to directly use AWS::CloudFormation::Macro? (or maybe hacking with StackSets?)

@timoschilling
Copy link
Contributor

Sorry at the moment I only have the concept, will share it if its done.

@yvele
Copy link
Author

yvele commented Oct 23, 2018

Ok.. you can share the detailed concept if you want 😉

I was thinking about the following concept (quite hacky but well...):

In a single cloudformation.yml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

  Distribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        DefaultCacheBehavior:
          LambdaFunctionAssociations:
            - EventType: origin-request
              LambdaFunctionARN: !Ref LambdaFunctionStandard.Version

  # Unused Lambda function only to get `CodeUri` working
  LambdaFunctionCodeProxy:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src
      AutoPublishAlias: live

  # Custom resource to "copy" the Lambda in the standard region (us-east-1)
  LambdaFunctionStandard:
    Type: Custom::LambdaEdgeCopy
    Properties:
      ServiceToken: !ImportValue CustomRessourceLambdaEdgeCopy
      Parameters:
        Version: !Ref LambdaFunctionCodeProxy.Version
        Region: us-east-1

Something awesome and more simple could be

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

  Distribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        DefaultCacheBehavior:
          LambdaFunctionAssociations:
            - EventType: origin-request
              LambdaFunctionARN: !Ref LambdaFunction.Version

  LambdaFunction:
    Type: Custom::LambdaEdgeCopy
    Properties:
      ServiceToken: !ImportValue CustomRessourceLambdaEdgeCopy
      Parameters:
        CodeUri: src # Unfortunately `CodeUri` is not processed by SAM for Custom resources

Unfortunately CodeUri is not processed by SAM for Custom resources

@anhtv08
Copy link

anhtv08 commented Sep 22, 2019

I am facing same issue

@yvele
Copy link
Author

yvele commented Sep 22, 2019

@anhtv08 My team is using the technique described here #635 (comment) for a whole year and everything is going well. The technique consists (within a single CloudFormation template) of deploying a classic Lambda function on the region of your choice + deploying a custom resource that "copies" the Lambda in us-east-1. If you are interested with the code I can create a repo 🤔

Edit: I've shared my solution in a repo -> https://github.com/yvele/aws-cfn-custom-resource-lambda-edge 🚀

@michalkvasnicak
Copy link

@yvele could you create a repo, please?

@ghost
Copy link

ghost commented Oct 6, 2019

@yvele I'm also interested in seeing a repo / example of the method as I'm facing the same issue.

@stalkerg
Copy link

stalkerg commented Oct 8, 2019

@yvele for me very interesting too how CustomRessourceLambdaEdgeCopy originaly work.

@ZubairAshraf
Copy link

@yvele could you please share or create a repo ?

@cmwint
Copy link

cmwint commented Oct 22, 2019

@yvele please create and share a repo with the code example. I am facing this same issue and would love to see a solution.

@txase
Copy link
Contributor

txase commented Oct 22, 2019

In case it's useful for others, we published a blog post and GitHub repo showing how to build and deploy a Lambda@Edge app. Note that under the covers it works by using a CFN Custom Resource that invokes a Lambda Function (Stackery provides this for you, but you can create your own) to create the Edge Lambda Function.

(Full disclosure, I'm the CTO @ Stackery. But you can use our free tier if you just want help building your app. You can decide afterwards if you want to pay for us to help you with centralized deployment and management of multiple apps built by teams of developers.)

@MatteoGioioso
Copy link

MatteoGioioso commented Jan 6, 2020

Same error here

@yvele will traffic shifting still works with your method?
Also you left the most interesting part CustomRessourceLambdaEdgeCopy, would you mind posting an example? I am guessing you are using lambda.createFunction API

@tomhatzer
Copy link

@yvele could you possibly share your CustomRessourceLambdaEdgeCopy so we can use it too?

yvele added a commit to yvele/aws-cfn-custom-resource-lambda-edge that referenced this issue Jun 15, 2020
@yvele
Copy link
Author

yvele commented Jun 15, 2020

@yvele could you possibly share your CustomRessourceLambdaEdgeCopy so we can use it too?

Okay I've adapted my code to be open source 😅

I made a repository https://github.com/yvele/aws-cfn-custom-resource-lambda-edge that deploys a CloudFormation custom resource allowing to deploy Lambda@Edge on any other region.

The custom resource "copies" the Lambda from your region to us-east-1 allowing to bind it to a CloudFront event via Lambda@Edge.

Please fill an issue in the repository if you have any problem or question.

I hope AWS CloudFormation (or SAM) will support Lambda@Edge deployment on other regions than us-east-1.

@yvele will traffic shifting still works with your method?

Yep 👍

Also you left the most interesting part CustomRessourceLambdaEdgeCopy, would you mind posting an example? I am guessing you are using lambda.createFunction API

I'm not using lambda.createFunction. I'm deploying the Lambda in the destination region with an other CloudFormation stack within the custom resource. Check the source code and let me know what do you think about it.

yvele added a commit to yvele/aws-cfn-custom-resource-lambda-edge that referenced this issue Jun 15, 2020
@aprilmintacpineda
Copy link

The linked example repo no longer works...

@yvele
Copy link
Author

yvele commented Mar 17, 2021

The linked example repo no longer works...

What kind of error do you have? 🤔 Do not hesitate to fill an issue here https://github.com/yvele/aws-cfn-custom-resource-lambda-edge/issues

@aprilmintacpineda
Copy link

@yvele that link works, but the link on the description of the issue shows this, might be just a typo?
image

@yvele
Copy link
Author

yvele commented Mar 18, 2021

@yvele that link works, but the link on the description of the issue shows this, might be just a typo?

Nope... the AWS team just changed the URL to the example 🤷 nothing I can do..

@jfuss
Copy link
Contributor

jfuss commented Oct 12, 2021

This is not something SAM can directly help with. SAM transforms a SAM Spec into raw CloudFormation. A stack is deployed to a specific region within an account, so once SAM transforms the spec CloudFormation and Service restrictions are in play. Since this is a restriction on the service, SAM can't do anything.

There are solutions here to that use a custom resource, which would be a way forward. I would encourage creating forum requests or reach out to your account managers to request Lambda@Edge to support more than us-east-1.

Closing

@jfuss jfuss closed this as completed Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests