Last active
April 12, 2021 07:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template-deploy.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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;" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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