serverless-cloudfront-lambdaedge-plugin
v1.0.4
Published
A serverless plugin that injects actual Lambda Version ARN to LambdaAssociations in CloudFront CFN resource
Downloads
72
Maintainers
Readme
serverless-cloudfront-lambdaedge-plugin
A serverless plugin that injects actual Lambda Version ARN to LambdaAssociations in CloudFront CFN resource
Why?
Lambda@Edge replicates function to all CloudFront Edges.
Due to this behavior, Lambda@Edge configuration requires Full Lambda Function ARN.
You cannot reference alias or $LATEST
tag when configuring Lambda@Edge.
You MUST reference fully-qualified Lambda Function version ARN.
Currently there's no easy/reliable way to setup Lambda@Edge using Serverless Framework.
Recently released Serverless framework added support of Lambda@Edge triggers, but it looks not easily customizable. so you will have to use CloudFormation Template to configure Lambda@Edge.
Unfortunately, Serverless framework creates a new Lambda Version in every deployment with unpredictable hash suffix, and it's by default. You cannot reference Lambda Function version resource directly in your CloudFormation template.
This plugin replaces all LambdaFunctionAssociations
in your serverless.yml
CloudFormation template to automatically created Lambda Version references.
Install
First, install package as development dependency.
$ npm i serverless-cloudfront-lambdaedge-plugin --save-dev
Then, add the plugin to serverless.yml
# serverless.yml
plugins:
- serverless-cloudfront-lambdaedge-version
Setup
Just specify LambdaFunctionARN
of LambdaFunctionAssociations to matching function name.
Let's suppose that you've defined functions in serverless.yml
like below:
functions:
APIOriginRequest:
name: ${self:service}-${self:provider.stage}-api-origin-request
handler: handlers/api/origin-request.handler
memorySize: 128
timeout: 5
role: LambdaEdgeExecutionRole
WebOriginRequest:
name: ${self:service}-${self:provider.stage}-web-origin-request
handler: handlers/api/origin-request.handler
memorySize: 128
timeout: 5
role: LambdaEdgeExecutionRole
Just Specify LambdaFunctionARN
block like below:
resources:
Resources:
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
# ... TRUNCATED ...
DefaultCacheBehavior:
LambdaFunctionAssociations:
- EventType: origin-request
LambdaFunctionARN: WebOriginRequest # Specify matching function name
# ... TRUNCATED ...
CacheBehaviors:
- PathPattern: "/api/*"
LambdaFunctionAssociations:
- EventType: origin-request
LambdaFunctionARN: APIOriginRequest # Specify matching function name
# ... TRUNCATED ...
# ... TRUNCATED ...
and then, deploy your stack.
This plugin will automatically replace LambdaFunctionARN
block to { Ref: "MatchingLambdaVersionLogicalId" }
.
Changelog
See CHANGELOG.
License
See full license on mooyoul.mit-license.org