Serverless-application-model: Lambda@Edge example only works on us-east-1 region

Created on 23 Oct 2018  路  16Comments  路  Source: aws/serverless-application-model

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 than allow 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

areresources typfeature

Most helpful comment

@anhtv08 My team is using the technique described here https://github.com/awslabs/serverless-application-model/issues/635#issuecomment-432187356 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 馃

All 16 comments

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.

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

@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?)

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

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

I am facing same issue

@anhtv08 My team is using the technique described here https://github.com/awslabs/serverless-application-model/issues/635#issuecomment-432187356 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 馃

@yvele could you create a repo, please?

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

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

@yvele could you please share or create a repo ?

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

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.)

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

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

@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.

Was this page helpful?
0 / 5 - 0 ratings