Skip to content

Instantly share code, notes, and snippets.

@ryan-blunden
Last active April 12, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryan-blunden/f509d68064cf5c797d8caecd93d1b2ff to your computer and use it in GitHub Desktop.
Save ryan-blunden/f509d68064cf5c797d8caecd93d1b2ff to your computer and use it in GitHub Desktop.
AWS SAM template automated embedding of environment variables using the Doppler CLI and a Node.js script
template-deploy.yaml
import fs from 'fs';
import { execSync } from 'child_process'
let environmentTemplate =`Environment:
Variables:`
const template = fs.readFileSync('template.yaml', { encoding: 'utf-8'})
const secrets = JSON.parse(execSync('doppler secrets download --no-file', { encoding: 'utf-8'}).toString())
Object.keys(secrets).forEach( key => {
environmentTemplate += `\n ${key}: ${secrets[key]}`
})
console.log(template.replace('#$ENVIRONMENT', environmentTemplate))
{
"type": "module",
"scripts": {
"deploy-template": "node deploy-template.js > template-deploy.yaml",
"local-dev": "node deploy-template.js > template-deploy.yaml; sam local start-api --template template-deploy.yaml; rm template-deploy.yaml",
"deploy": "node deploy-template.js > template-deploy.yaml; sam deploy --template template-deploy.yaml; rm template-deploy.yaml;"
}
}
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs14.x
#$ENVIRONMENT
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment