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-static-env

v1.0.0

Published

Replace environment variables with static strings before deployment. It is for Lambda @ Edge.

Downloads

2,753

Readme

serverless npm version License: MIT

Static Env ServerlessFramework Plugin

This plugin is is intended for use with Lambda@Edge to replace process.env and other similar mechanisms referencing environment variables with static values so that environment specific configuration can be used in Lambda@Edge (which does not support environment variables).

This is forked from serverless-plugin-embedded-env-in-code with as it takes a different solution to the same problem of providing environment variables to Lambda@Edge.

In summary, what this does is take all environment variables specified in the serverless.yml in provider.environment or function.environment and packages them into the deployment zip so they become defaults if the environment variables defined when the function is executed. Practically speaking this means any environment variables specified in provider.environment or function.environment are available to Lambda@Edge functions the same way as they would to a normal lambda. The key difference being a redeployment is required to update the values.

Configuration

Environment variables and their values are configured as normal, either in the top level provider.environment or function.engironment

When the function is packaged for deployment the values will be embedded in the package so that they are present in process.env as normal. Note that any environment variables actually set in the environment will take precedence, these are packaged as defaults only.

By default, any function with a cloudFront event trigger will have it's environment variables embedded during packaging.

A function can explicitly opt in or out of this behavior by specifying the boolean includeStaticEnv in the function definition.

Example

The below serverless.yml shows the various usages

service:
  name: static-env-example

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage, 'development'}
  region: 'us-east-1'

  # These environment variables will be made available to all function and included
  # where static environment is embedded
  environment:
    SHARED_ENVIRONMENT_VAR1: 'value1'
    SHARED_ENVIRONMENT_VAR2: 'value2'

plugins:
  - serverless-plugin-static-env

functions:
  edgeFunctionWithStaticEnv:
    handler: dist/endpoints/edgeFunctionWithStaticEnv.handler
    environment:
      FUNCTION_SPECIFIC_ENVIRONMENT_VAR1: 'value3'
      FUNCTION_SPECIFIC_ENVIRONMENT_VAR2: 'value4'
    # includeStaticEnv: true # this will have static env by default because it includes a
                             # cloudFront event trigger. If this was set to false it would
                             # not have a static env even though it has a cloudFront trigger
                             #
                             # Without a cloudFront trigger this would have to be explicitly
                             # set to true to opt in to embedding the static environment
    events:
      - cloudFront:
          eventType: viewer-response
          origin: s3://bucketname.s3.amazonaws.com/files

How it Works

For each function an additional -env.js file is generated with the static environment. E.g. with the example configuration above the file dist/endpoints/edgeFunctionWithStaticEnv-env.js would be generated with the following content

process.env["SHARED_ENVIRONMENT_VAR1"] = process.env["SHARED_ENVIRONMENT_VAR1"] == null ? "value1": process.env["SHARED_ENVIRONMENT_VAR1"];
process.env["SHARED_ENVIRONMENT_VAR2"] = process.env["SHARED_ENVIRONMENT_VAR2"] == null ? "value2": process.env["SHARED_ENVIRONMENT_VAR2"];
process.env["FUNCTION_SPECIFIC_ENVIRONMENT_VAR1"] = process.env["FUNCTION_SPECIFIC_ENVIRONMENT_VAR1"] == null ? "value3": process.env["FUNCTION_SPECIFIC_ENVIRONMENT_VAR1"];
process.env["FUNCTION_SPECIFIC_ENVIRONMENT_VAR2"] = process.env["FUNCTION_SPECIFIC_ENVIRONMENT_VAR2"] == null ? "value4": process.env["FUNCTION_SPECIFIC_ENVIRONMENT_VAR2"];

The file dist/endpoints/edgeFunctionWithStaticEnv.js would also be updated to require dist/endpoints/edgeFunctionWithStaticEnv-env.js as it's first action ensuring that these variables are present before any other required code is executed.