npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-plugin-existing-cloudfront-lambda-edge

v2.0.1

Published

Plugin for the Serverless v2.x to provide support for Lambda@Edge for existing CloudFront Distributions

Downloads

714

Readme

Serverless Plugin: Support Lambda@Edge for Existing CloudFront Distributions

Build Status Coverage Status Dependency Status Dev Dependency Status

First things first

If you don't have an existing CloudFront distribution, just use serverless-plugin-cloudfront-lambda-edge V2.

What is it?

This is a plugin for the Serverless framework that adds support for associating a Lambda function with a existing CloudFront distribution to take advantage of the new Lambda@Edge features of CloudFront.

A Few Other Things to Keep In Mind

  1. CloudFront distributions can take a long time to deploy, so you probably want to keep this separate from other "normal" serverless services.
  2. When removing the service or deleting functions, see Deleting Functions below.

How do I use it?

There are three steps:

Install the Plugin as a Development Dependency

npm install --save-dev serverless-plugin-existing-cloudfront-lambda-edge

Telling Serverless to Use the Plugin

Simply add this plugin to the list of plugins in your serverless.yml file:

plugins:
  - serverless-plugin-existing-cloudfront-lambda-edge

Configuring Functions to Associate With CloudFront Distributions

Also in your serverless.yml file, you will modify your function definitions to include a lambdaAtEdge property. That object will contain two key/value pairs: distributionID and eventType.

The eventType is one of the four Lambda@Edge event types:

  • viewer-request
  • origin-request
  • viewer-response
  • origin-response

For example:

functions:
  directoryRootOriginRequestRewriter:
    name: '${self:custom.objectPrefix}-origin-request'
    handler: src/DirectoryRootOriginRequestRewriteHandler.handler
    memorySize: 128
    timeout: 1
    lambdaAtEdge:
      distributionID: OIJOI2332OLIN
      eventType: 'origin-request'

And here is an example function that would go with this Serverless template:

module.exports = {
  // Invoked by CloudFront (origin requests)
  async handler(event, context) {
    // When first testing, I recommend to log these
    console.log('event', JSON.stringify(event))
    console.log('context', JSON.stringify(context))
    const req = event.Records[0].cf.request

    if (
      req.uri &&
      req.uri.length &&
      req.uri.substring(req.uri.length - 1) === '/'
    ) {
      console.log('changing "%s" to "%s"', req.uri, req.uri + 'index.html')
      req.uri = req.uri + 'index.html'
    }

    return req
  }
}

You can find more in the examples directory.

Plugin V2

Using serverless/CloudFormation is a little finicky, but it seems to be getting better. For example, when I first forked this repo, I couldn't even manually delete the Lambda@Edge functions. Now you can. There are still some caveats such as sometimes needing to deploy twice (usually when you're changing the function signature or name).

Having said that, it's still much easier to manage than manually doing it all, assuming you have an existing CloudFront distribution. If not, just use serverless-plugin-cloudfront-lambda-edge V2.

Logs

Given that Lambda@Edge can run in any region that CloudFront operates, the execution logs can be scattered throughout CloudWatch regions. Nothing will be logged to the expected CloudWatch logs created by the Serverless framework.

Deleting Functions

Running the standard sls remove won't work because the association with the function to CloudFront. So before running sls remove, you need to manually remove the CloudFront event function association(s), and then wait until the CloudFront distribution is fully deployed. See the full AWS Documentation for more details.

Lastly, even when you follow the steps above, and your distribution is fully deployed, it can still take another hour or two before you can successfully delete the lambda function(s).

How do I contribute?

Feel free to open an issue and/or create a pull request.

License

This software is released under the MIT license. See the license file for more details.

Credits