Reputation: 111
Currently I have a generic CF template that is shared across multiple projects/AWS accounts. It uses CFN parameters so individual projects can enable/disable features as needed.
I am looking to convert this to CDK but I am unsure of how to do this properly.
I have tried the following so far:
Convert the CFN Parameters to context variables - so we can have the projects parameters available at synth time to enable/disable features. I think this will work but context variables don't seem suited for this use-case, from the CDK documentation they cache all sorts of data to do with the AWS account. Because of this I believe I would need to clear the context cache every time I used it, which seems like it would break this:
The project file cdk.context.json is where the AWS CDK caches context values retrieved from your AWS account. This practice avoids unexpected changes to your deployments when, for example, a new Amazon Linux AMI is released, changing your Auto Scaling group.
The fact that CDK is going to cache account specific values makes me think if a generic stack like this is even possible. Will I need to create a CDK app for each project and then reuse the stack for each app?
How do I structure a generic stack that is reusable across projects/AWS accounts?
Upvotes: 1
Views: 1657
Reputation: 216
I'm using ssm parameter store to share parameters from one stack to another:
One stack writes the parameter
import * as ssm from '@aws-cdk/aws-ssm';
new ssm.StringParameter(this, 'MyParameter', {
parameterName: '/myPrefix/myS3BucketArn',
stringValue: s3Bucket.bucketArn
});
The other stack reads it
const s3BucketArn = ssm.StringParameter.valueFromLookup(this, '/myPrefix/myS3BucketArn');
Upvotes: 0
Reputation: 1033
CDK is support for CfnParameter as well. You can use the parameters same way as you are doing now with CFN template.
You can create parameter like below and use it wherever you want to use.
const uploadBucketName = new CfnParameter(this, "uploadBucketName", {
type: "String",
description: "The name of the Amazon S3 bucket where uploaded files will be stored."});
const bucket = new Bucket(this, "myBucket",
{ bucketName: uploadBucketName.valueAsString});
To deploy you can pass the parameters.
cdk deploy MyStack --parameters uploadBucketName=UploadBucket
Upvotes: 0