https://github.com/mooyoul/serverless-cloudfront-lambdaedge-plugin
A serverless plugin that injects actual Lambda Version ARN to LambdaAssociations in CloudFront CFN resource
https://github.com/mooyoul/serverless-cloudfront-lambdaedge-plugin
lambda lambda-edge serverless serverless-plugin
Last synced: 10 months ago
JSON representation
A serverless plugin that injects actual Lambda Version ARN to LambdaAssociations in CloudFront CFN resource
- Host: GitHub
- URL: https://github.com/mooyoul/serverless-cloudfront-lambdaedge-plugin
- Owner: mooyoul
- License: mit
- Created: 2019-12-13T21:51:21.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-04-11T08:13:47.000Z (10 months ago)
- Last Synced: 2025-04-13T00:15:52.724Z (10 months ago)
- Topics: lambda, lambda-edge, serverless, serverless-plugin
- Language: JavaScript
- Size: 1.4 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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.](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-how-it-works.html)
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.](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-lambdafunctionassociation.html)
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](https://github.com/serverless/serverless/issues/6785).
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](https://github.com/serverless/serverless/blob/5fa10f59279d0b8c2e31c9ab5c3121fc06b32813/lib/plugins/aws/package/compile/functions/index.js#L478-L551), 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.
```bash
$ npm i serverless-cloudfront-lambdaedge-plugin --save-dev
```
Then, add the plugin to `serverless.yml`
```yaml
# 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:
```yaml
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:
```yaml
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](CHANGELOG.md).
## License
[MIT](LICENSE)
See full license on [mooyoul.mit-license.org](http://mooyoul.mit-license.org/)