Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@pmuens
Copy link
Contributor

@pmuens pmuens commented Aug 7, 2019

This is a rebased version of #4796 from @laardee 👍 🥇 🙌

What did you implement:

Closes #3944

Adds support for Lambda@Edge via pure CloudFormation. Thanks again to @laardee who jumped in to work on an initial implementation for this!

How did you implement it:

Compile all the necessary CloudFormation resources (CloudFront / Versioned Lambda function, etc.) to add support for Lambda@Edge.

Todos:

  • Retain Lambda Functions to avoid errors on Stack removals
  • Log info that functions have to be removed by hand when removing stack

How can we verify it:

Create a new serverless service and replace the handler.js and serverless.yml code with the following snippets:

// handler.js
module.exports.cloudfront = (event, context, callback) => {
  const response = event.Records[0].cf.response;
  const headers = response.headers;

  headers['x-serverless-time'] = [{ key: 'x-serverless-time', value: Date.now().toString() }];

  return callback(null, response);
};
service: test-${self:custom.idx}

provider:
  name: aws
  runtime: nodejs10.x
  versionFunctions: false

custom:
  idx: 0

functions:
  hello:
    handler: handler.hello
    events:
      - cloudFront:
          eventType: viewer-response
          origin: https://serverless.com/framework/docs

Deploy the service (this could take a couple of minutes (~10 - 20mins)). After that you can access the CloudFront distribution URL which is printed in your console (you can also use serverless info). This should open up the Serverless Framework docs page.

Inspect the headers (e.g. using this guide if you're running Chrome). You should see the x-serverless-time header which was added by the Lambda@Edge function.

Once validated you might want to remove the stack. Note the info message about the necessary manual Lambda@Edge function removals which is logged on the console.

Todos:

Note: Run npm run test-ci to run all validation checks on proposed changes

  • Write tests and confirm existing functionality is not broken.
    Validate via npm test
  • Write documentation
  • Ensure there are no lint errors.
    Validate via npm run lint-updated
    Note: Some reported issues can be automatically fixed by running npm run lint:fix
  • Ensure introduced changes match Prettier formatting.
    Validate via npm run prettier-check-updated
    Note: All reported issues can be automatically fixed by running npm run prettify-updated
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@pmuens pmuens added this to the 1.50.0 milestone Aug 7, 2019
@pmuens pmuens self-assigned this Aug 7, 2019
@pmuens pmuens force-pushed the lambda-at-edge branch 3 times, most recently from 2d13a8b to 35101af Compare August 9, 2019 14:13
@medikoo medikoo modified the milestones: 1.50.0, 1.51.0 Aug 14, 2019
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I done more stylistics review (with a lot of nit picks where we can rely on native language features instead of lodash).

Otherwise it looks good, and my guess is that the only thing that is stopping from releasing it, is testing this.

docs/providers/aws/events/cloudfront.md Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/events/cloudFront/index.js Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/events/cloudFront/index.js Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/events/cloudFront/index.js Outdated Show resolved Hide resolved
lib/plugins/aws/info/getStackInfo.js Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/events/cloudFront/index.js Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/events/cloudFront/index.js Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/events/cloudFront/index.js Outdated Show resolved Hide resolved
lib/plugins/aws/package/compile/functions/index.js Outdated Show resolved Hide resolved
@pmuens pmuens force-pushed the lambda-at-edge branch 4 times, most recently from eb49d12 to 4956527 Compare August 26, 2019 15:37
@pmuens pmuens modified the milestones: 1.51.0, 1.52.0 Aug 27, 2019
@pmuens pmuens force-pushed the lambda-at-edge branch 3 times, most recently from b1c66f6 to 7dec3a6 Compare August 28, 2019 14:13
@pmuens pmuens marked this pull request as ready for review August 29, 2019 13:51
@pmuens pmuens requested a review from medikoo August 29, 2019 13:52
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of tests crash when run individually (exposed by isolated test run)

@pmuens
Copy link
Contributor Author

pmuens commented Aug 29, 2019

Some of tests crash when run individually (exposed by isolated test run)

Good catch. Just pushed a fix...

docs/providers/aws/events/cloudfront.md Outdated Show resolved Hide resolved
lib/plugins/aws/lib/monitorStack.js Outdated Show resolved Hide resolved
lib/plugins/aws/lib/naming.test.js Show resolved Hide resolved
@pmuens
Copy link
Contributor Author

pmuens commented Sep 5, 2019

Just did another rebase and fixed the issue where the removal log info is always executed. It's now only shown when the command is remove.

Furthermore I did a deep dive into the testing today and could validate that it's working as expected. Shoutouts to @laardee who helped me here and provided https://github.com/laardee/serverless-lambda-at-edge as an example service 🙇 👍

I also updated the PR description to provide a step-by-step walkthrough to validate that this integration works.

@pmuens pmuens requested a review from medikoo September 5, 2019 12:07
@pmuens pmuens merged commit c4b7726 into master Sep 5, 2019
@pmuens pmuens deleted the lambda-at-edge branch September 5, 2019 13:40
@laardee
Copy link
Contributor

laardee commented Sep 5, 2019

FINALLY! 🎉:muscle:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Lambda@Edge

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.