Skip to content

Commit

Permalink
Initial Environment Variables from AWS Systems Manager Parameter Stor…
Browse files Browse the repository at this point in the history
…e sample.
  • Loading branch information
mwarman committed Mar 22, 2019
1 parent f70d68c commit c249129
Show file tree
Hide file tree
Showing 5 changed files with 471 additions and 0 deletions.
64 changes: 64 additions & 0 deletions rest-environment-ssm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Sample: Environment Variables from AWS Systems Manager Parameter Store

This sample serverless application illustrates how to implement REST services whose functions are passed [environment variables][sls-envvar] sourced from [AWS Systems Manager Parameter Store][aws-paramstore].

Environment variables declared in the `provider` block are passed to all functions. Environment variables declared in a function block are passed only to that function.

## How to Use

### Create Parameters

Before deploying the serverless application you must create the parameters in AWS Systems Manager Parameter Store. Sign into the AWS console and navigate to [AWS Systems Manager](https://console.aws.amazon.com/systems-manager). Select _Parameter Store_ from the left side navigation menu.

In this example, [parameters are organized into a hierarchy][aws-paramhierarchy] with three levels:

/ServiceName/Stage/ParameterName

In the serverless specification, the parameters are retrieved with dynamic references to the service name and stage using the following syntax: `${ssm:/${self:service}/${self:provider.stage}/greeting-audience}`.

Create three SSM parameters to hold values for the `greeting-audience` varible in three stages: dev, qa, and prod. The parameters are:

```
/samples-environment-ssm/dev/greeting-audience # Value=Developers
/samples-environment-ssm/qa/greeting-audience # Value=Testers
/samples-environment-ssm/prod/greeting-audience # Value=World
```

### Install Dependencies

Install the project dependencies with the Yarn package manager. Navigate to the application base directory and issue the following command:

```
yarn install
```

### Run the Application

Run the serverless application in offline mode. Navigate to the application base directory and issue the following command:

```
# Deploy to 'dev' stage by default
sls deploy
OR
# Deploy to a specific stage
sls deploy --stage prod
```

Test the application by calling the API endpoints:
- `GET /hello`
- `GET /goodbye`

Try deploying the application to different stages. Notice that the correct parameter value is used for each environment.

## See Also
[Serverless Functions - Environment Variables][sls-envvar]
[Reference Variables using the SSM Parameter Store][sls-varssm]
[AWS Systems Manager Parameter Store][aws-paramstore]
[Organizing Parameters into Hierarchies][aws-paramhierarchy]

[sls-envvar]: https://serverless.com/framework/docs/providers/aws/guide/functions/#environment-variables "Environment Variables | Serverless Docs"
[sls-varssm]: https://serverless.com/framework/docs/providers/aws/guide/variables#reference-variables-using-the-ssm-parameter-store "Reference Variables using the SSM Parameter Store | Serverless Docs"
[aws-paramstore]: https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html "AWS Systems Manager Parameter Store | AWS Docs"
[aws-paramhierarchy]: https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-su-organize.html "Organizing Parameters into Hierarchies | AWS Docs"
21 changes: 21 additions & 0 deletions rest-environment-ssm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const serverless = require('serverless-http');
const express = require('express');
const app = express();

// Environment variables with defaults
const GREETING_PREFIX = process.env.GREETING_PREFIX || 'Aloha';
const GREETING_AUDIENCE = process.env.GREETING_AUDIENCE || 'World';

_getMessage = (prefix, audience) => {
return `${prefix} ${audience}!`;
};

app.get('/hello', function(req,res) {
res.send(_getMessage(GREETING_PREFIX, GREETING_AUDIENCE));
});

app.get('/goodbye', function(req,res) {
res.send(_getMessage(GREETING_PREFIX, GREETING_AUDIENCE));
});

module.exports.handler = serverless(app);
13 changes: 13 additions & 0 deletions rest-environment-ssm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "serverless-samples-environment-ssm",
"version": "1.0.0",
"main": "index.js",
"repository": "https://github.com/mwarman/serverless-samples",
"author": "Matt Warman",
"license": "Apache-2.0",
"private": false,
"dependencies": {
"express": "^4.16.4",
"serverless-http": "^1.9.1"
}
}
24 changes: 24 additions & 0 deletions rest-environment-ssm/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
service: samples-environment-ssm

provider:
name: aws
runtime: nodejs8.10
region: us-east-1
stage: ${opt:stage, 'dev'}
memorySize: 128
environment:
GREETING_AUDIENCE: ${ssm:/${self:service}/${self:provider.stage}/greeting-audience}

functions:
hello:
handler: index.handler
environment:
GREETING_PREFIX: 'Hello'
events:
- http: GET /hello
goodbye:
handler: index.handler
environment:
GREETING_PREFIX: 'Goodbye'
events:
- http: GET /goodbye
Loading

0 comments on commit c249129

Please sign in to comment.