Git Product home page Git Product logo

robin-thomas / serverless-aws-secrets Goto Github PK

View Code? Open in Web Editor NEW
85.0 2.0 7.0 464 KB

๐Ÿ›ต Serverless plugin that reads environment variables and replaces secrets using AWS Secrets Manager ๐Ÿ›ต

Home Page: https://www.npmjs.com/package/serverless-aws-secrets

License: MIT License

Shell 1.18% TypeScript 97.41% JavaScript 1.42%
aws aws-lambda aws-secrets-manager environment-variables serverless security serverless-plugin jest typescript javascript secrets-manager

serverless-aws-secrets's Introduction

Serverless AWS Secrets Tweet

A Serverless Plugin for the Serverless Framework, which can replace environment variables with secrets from AWS Secrets Manager.

Introduction

If you are using a serverless plugin like Serverless Dotenv Plugin, then you shall be having .env.* files that looks like:

MYSQL_USERNAME=username
MYSQL_PASSWORD=password

Rather than storing these secrets in your .env.* file, you can instead store them in AWS Secrets Manager. This plugin will then replace the environment variables (that are already loaded into Serverless framework) with the secrets from AWS Secrets Manager.

You need to change your above .env.* files to:

MYSQL_USERNAME=secret:MYSQL_USERNAME
MYSQL_PASSWORD=secret:MYSQL_PASSWORD

The plugin will then search within AWS Secrets Manager (refer to secretId configuration) for a secret with the name MYSQL_USERNAME and MYSQL_PASSWORD and replace the environment variables with the secret value.

Secrets are recognized as environment variables whose name started with a pre-defined prefix. (refer to secretPrefix configuration below).

Getting Started

These instructions will help you integrate this plugin into your serverless service.

Prerequisites

You need to have the below softwares running on your system:

Installing the plugin

Run below command to install the plugin:

$ npm install --save-dev serverless-aws-secrets

Add the plugin to serverless.yml:

plugins:
  - serverless-aws-secrets

This will run the plugin during the below serverless hooks:

  • before:package:initialize
  • offline:start:init

Configuring the plugin

The plugin can be configured by:

custom:
  serverless-aws-secrets:
    secretId: ...
    secretPrefix: ...
  • secretId: Location of the secret in AWS Secrets Manager. Default: ${provider.stage}/${app}-${service}

  • secretPrefix: Prefix of the secret name in AWS Secrets Manager. Default: secret:

CLI commands

This plugin also exposes a CLI command that can be used along with serverless.

Display the secret values

$ sls aws-secrets --verbose

This will display the output:

[serverless-aws-secrets]: Running the command: sls aws-secrets
[serverless-aws-secrets]: Loading secret: {secretId} in {provider.region}
โœ” [serverless-aws-secrets]: Secret: {secretKey}, Value: {secretValue}

Local Development

These instructions will help you to run the project in your local.

Setup

Run the below commands to setup the project:

$ git clone [email protected]:robin-thomas/serverless-aws-secrets.git
$ cd serverless-aws-secrets
$ nvm use 18
$ npm install

Running the tests

You can run the unit tests written in Jest by running:

$ npm run test

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

serverless-aws-secrets's People

Contributors

dependabot[bot] avatar jaas666 avatar robin-thomas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serverless-aws-secrets's Issues

Expose the secret only during runtime

Rather than exposing the secret during the build stage (sls deploy), figure out a way whereby the secret shall be exposed only during runtime.

Some possible ways:

  1. Replace all instances of process.env.SECRET_ENV_VAR with a shim that connects to AWS Secrets Manager and retrieve the secret during runtime
  2. Encrypts the secret during build stage, and decrypt it during runtime
  3. Inject the secret into code (not safe, since the secret can be determined by anyone who can see the lambda code)

It dosen't work

  • serverless.yml
service: hellow
frameworkVersion: '3'
custom:
  serverless-aws-secrets:
    secretId: test

provider:
  name: aws
  runtime: nodejs18.x
  region: ap-northeast-2
  environment:
    TEST: secret:TEST

functions:
  test:
    handler: index.handler

plugins:
  - serverless-aws-secrets

typing sls aws-secrets --verbose
It ran right

DOTENV: Loading environment variables from .env:
[serverless-aws-secrets]: Running the command: sls aws-secrets
[serverless-aws-secrets]: Loading secret: test in ap-northeast-2
โœ” [serverless-aws-secrets]: Secret: TEST, Value: good

but when I typed a console.log , It didn't run right

{
   ...
   TEST: 'secret:TEST',
   ...
}

how should I do??
can i forgot something?

my environment
language : nodejs

Add some logging for this plugin

Is it possible to add some logging for this plugin, like what are the secret keys that are replaced by this plugin?

Right now, it logs only when an error occurs. Good to have some verbose logging.

Thanks for making this plugin!

Support custom serverless hooks

At the moment, we support only 2 serverless lifecycle hooks:

  • before:package:initialize
  • offline:start:init

See whether we can accept a list of lifecycle hooks (besides the 2 above) from the user as a custom configuration

Replace secrets in environment variables defined under function

The plugin replaces the environment variables defined under provide.environment key in serverless.yml.

Rather than restricting to this, replace the ones that can be defined like:

functions:
  function1:
    handler: index.handler
    env:
      ...

Also, make provider.environment as optional so that the plugin wont crash if its not set.

Update to Node.js v20

Since Node.js v18 shall reach its end-of-life on 18 Oct 2023, update to Node.js v20 after this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.