Skip to content

Instantly share code, notes, and snippets.

@timkinnane
Created March 16, 2022 05:23
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 timkinnane/a70f3fcac623e212b6e078b8432784a0 to your computer and use it in GitHub Desktop.
Save timkinnane/a70f3fcac623e212b6e078b8432784a0 to your computer and use it in GitHub Desktop.
AWS CDK SSM: A construct for accessing SSM values from a stack as Cloud Formation tokens to resolve on deploy.
import { Construct } from 'constructs'
import { aws_ssm as SSM } from 'aws-cdk-lib'
/**
* Create a resource for accessing SSM values (as Cloud Formation tokens to resolve on deploy).
*/
export class ParamsConstruct extends Construct {
private configPath: string
private secretPath?: string
constructor (scope: Construct, id: string, options: { configPath: string, secretPath?: string }) {
super(scope, id)
this.configPath = options.configPath
this.secretPath = options.secretPath
}
config (key: string) {
return SSM.StringParameter.valueForStringParameter(this, `${this.configPath}/${key}`)
.toString()
}
secret (key: string) {
return SSM.StringParameter.fromSecureStringParameterAttributes(this, key, {
parameterName: `${this.secretPath}/${key}`
}).stringValue
}
}
import { App, Stack, StackProps } from 'aws-cdk-lib'
import { Construct } from 'constructs'
import { ParamsConstruct } from './construct'
const app = new App()
class ExampleStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props)
//... add construct here to define SSM params
const configPath = `/my-app/config`
const secretPath = `/my-app/secret`
const params = new ParamsConstruct(this, 'TestParams', { configPath, secretPath })
console.log('My config token', params.config('MY_CONFIG'))
console.log('My secret token', params.secret('MY_SECRET'))
}
}
new ExampleStack(app, 'TestStack')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment