Skip to content

Commit ed193a6

Browse files
committed
add example with encrypted envariables in a file
1 parent bf17843 commit ed193a6

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secrets.*.yml
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Serverless
2+
3+
This example demonstrates how to store secrets like API keys encrypted in your repository while providing them as environment variables to your AWS Lambda functions.
4+
5+
## Use-cases
6+
7+
- Provide secrets like API keys to your Lambda functions
8+
9+
## Why?
10+
11+
While repository hosting services like Github or Bitbucket have very high security standards it's recommended to not store your unencrypted secrets there. In addition in larger teams not everybody needs to have access to theses secrets of your production environment.
12+
13+
Encrypting your secrets per stage and only adding the encrypted files into your repository is a sensible strategy to fulfill the previously described goals. The passwords to decrypt and encrypt the secrets files should only be shared between the necessary developers over a secure channel. In case you are using a Continuous Integration to deploy your infrastructure obviously this system must be aware of the passwords as well.
14+
15+
## Setup
16+
17+
Since this plugin uses the Serverless plugin `serverless-secrets-plugin` you need to setup the `node_modules` by running:
18+
19+
```bash
20+
npm install
21+
```
22+
23+
## Usage
24+
25+
### Decrypt and Deploy
26+
27+
In order to deploy the you endpoint simply run
28+
29+
```bash
30+
serverless deploy --stage dev
31+
```
32+
33+
The expected result should be similar to:
34+
35+
```bash
36+
Error --------------------------------------------------
37+
38+
Couldn't find the secrets file for this stage: secrets.dev.yml
39+
40+
For debugging logs, run again after setting SLS_DEBUG env var.
41+
42+
Get Support --------------------------------------------
43+
Docs: docs.serverless.com
44+
Bugs: github.com/serverless/serverless/issues
45+
46+
Please report this error. We think it might be a bug.
47+
48+
Your Environment Information -----------------------------
49+
OS: darwin
50+
Node Version: 6.2.2
51+
Serverless Version: 1.2.0
52+
```
53+
54+
This is happening since the `serverless-secrets-plugin` makes sure a secrets file for the specific stage exists.
55+
56+
Let's decrypt the secrets file so you can deploy the service. To do so run
57+
58+
```bash
59+
serverless decrypt --stage dev --password 'va$27dC}9382G7ac6?V'
60+
```
61+
62+
The expected result should be similar to:
63+
64+
```bash
65+
Serverless: Sucessfully encrypted 'secrets.dev.yml.encrypted' to 'secrets.dev.yml'
66+
```
67+
68+
Now that you have the unencrypted version of your secrets file this directory you can deploy with
69+
70+
```bash
71+
serverless deploy --stage dev
72+
```
73+
74+
### Encrypt
75+
76+
In case you want to add, update or remove entries in your secrets file simply modify your secrets file. Once you are done encrypt it with
77+
78+
```bash
79+
serverless encrypt --stage dev --password 'va$27dC}9382G7ac6?V'
80+
```
81+
82+
The expected result should be:
83+
84+
```bash
85+
Serverless: Sucessfully encrypted 'secrets.dev.yml' to 'secrets.dev.yml.encrypted'
86+
```
87+
88+
The encrypted file can be checked into your version control system e.g. Git.
89+
90+
### Decrypt and Encrypt the Production Secrets
91+
92+
```bash
93+
serverless decrypt --stage prod --password 'v2]83WDneGt9AGXv]X6QfP9NW3^J&K3V'
94+
```
95+
96+
```bash
97+
serverless encrypt --stage prod --password 'v2]83WDneGt9AGXv]X6QfP9NW3^J&K3V'
98+
```
99+
100+
# Important Note
101+
102+
Make sure the the secrets files are listed in .gitignore to make sure they are never checked into your repository.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
/* eslint-disable no-console */
4+
5+
module.exports.resetPassword = (event, context, callback) => {
6+
console.log('SESSION_KEY: ', process.env.SESSION_KEY);
7+
8+
// Authenticate the user session
9+
10+
console.log('EMAIL_SERVICE_API_KEY: ', process.env.EMAIL_SERVICE_API_KEY);
11+
12+
// The email service api key would be used to send a reset password email.
13+
14+
const response = {
15+
statusCode: 200,
16+
body: JSON.stringify({
17+
message: 'Password sent.',
18+
}),
19+
};
20+
21+
callback(null, response);
22+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "env-variables-encrypted-in-a-file",
3+
"version": "1.0.0",
4+
"description": "Serverless example managing secrets in an encrypted file",
5+
"author": "",
6+
"license": "MIT",
7+
"dependencies": {
8+
"serverless-secrets-plugin": "^0.0.1"
9+
}
10+
}
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�q�۵�2�)8�G���Gg4}׬}J ]f��۲Ul��f�"ŵ�]�<����~L�8���>�6��_g�L�諩g��M�H��k�wWm��>/����%BRN�EM%�!N�b�
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
service: env-variables-encrypted-in-a-file
2+
3+
frameworkVersion: ">=1.2.0 <2.0.0"
4+
5+
plugins:
6+
- serverless-secrets-plugin
7+
8+
provider:
9+
name: aws
10+
runtime: nodejs4.3
11+
stage: dev
12+
13+
custom:
14+
secrets: ${file(secrets.${opt:stage, self:provider.stage}.yml)}
15+
16+
functions:
17+
resetPassword:
18+
handler: handler.resetPassword
19+
environment:
20+
SESSION_KEY: ${self:custom.secrets.SESSION_KEY}
21+
EMAIL_SERVICE_API_KEY: ${self:custom.secrets.EMAIL_SERVICE_API_KEY}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy