Created
March 16, 2022 05:23
-
-
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.
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 { 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 | |
} | |
} |
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 { 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