Skip to content

Instantly share code, notes, and snippets.

@daaru00
Last active February 13, 2021 15:31
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 daaru00/7e34ac0ff42003d44c589bdc48a1a52d to your computer and use it in GitHub Desktop.
Save daaru00/7e34ac0ff42003d44c589bdc48a1a52d to your computer and use it in GitHub Desktop.
AWS CloudFormation manage secrets with System Manager Parameter
const axios = require('axios')
/**
* AWS clients
*/
const SSM = require('aws-sdk/clients/ssm')
const ssm = new SSM()
/**
* Lambda handler
**/
module.exports = async () => {
// Retrieve parameter value
let { Parameter: param } = await ssm.getParameter({
Name: process.env.SECRET_PARAMETER_NAME
}).promise()
// Use the value
await axios.post('/protected', {}, {
headers: {
'Authorization': `Basic ${param.Value}`
}
})
}
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
# Template Parameters
Parameters:
SecretValue:
Type: String
Description: "A secret value"
NoEcho: true
# Global function defaults
Globals:
Function:
Runtime: nodejs12.x
Timeout: 3
CodeUri: ./
# Template Resources
Resources:
SecretValueParameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Value: !Ref Parameters
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${AWS::StackName}-function"
Handler: lambda.handler
Environment:
Variables:
SECRET_PARAMETER_NAME: !Ref SecretValueParameter
Policies:
- SSMParameterReadPolicy:
ParameterName: !Ref SecretValueParameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment