Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify parameter definition in CDK stack?

Tags:

aws-cdk

Use an AWS CloudFormation Parameter section of AWS CDK mentions how to customize your AWS CloudFormation templates. It refers to CloudFormation template. I would like to add parameters to my CDK stack and get the parameters section of the synthesized CloudFormation template.

Do I understand correctly that documentation suggests adding parameters section to synthesized template? If yes, it will be overwritten with every run of cdk synth.

Any other way to define the parameters section?

like image 410
zdenko.s Avatar asked Sep 27 '19 16:09

zdenko.s


People also ask

What is parameter in stack?

With parameters, you can customize your stack at creation time. Your parameter values can be used in the stack template to modify how resources are configured. That way you don't have to hard code values in multiple templates to specify different settings.

What is Deploymentstack in CDK?

The unit of deployment in the AWS CDK is called a stack. All AWS resources defined within the scope of a stack, either directly or indirectly, are provisioned as a single unit. Because AWS CDK stacks are implemented through AWS CloudFormation stacks, they have the same limitations as in AWS CloudFormation.

What are parameters in CloudFormation template?

Parameters can be used inside the CloudFormation template to refer to values that are provided at the time the CloudFormation template is used to create a new stack. This means that the same CloudFormation template can be used to create multiple stacks that have variations in the places where parameters are used.


1 Answers

Edit: Here is a typescript example that reads a bucket name from the context: https://github.com/cloudshiftstrategies/aws-cdk-examples/tree/master/context-example-typescript-app

You can add parameters to the CDK using the CfnParameter type like so:

new cdk.CfnParameter(this, 'MyParameter', {
    type: 'String',
    default: 'Blah',
    noEcho: true,
});

But that is generally discourages by the CDK. The design is to have fully deployable stacks and use code/config as the conditions for what should be created for a given account. This is from their documentation:

When you run the cdk synth command for an app with multiple stacks, the cloud assembly includes a separate template for each stack instance. Even if the two stacks are instances of the same class, the AWS CDK emits them as two individual templates.

You can synthesize each template by specifying the stack name in the cdk synth command. The following example synthesizes the template for stack1.

This approach is conceptually different from how AWS CloudFormation templates are normally used, where a template can be deployed multiple times and parameterized through AWS CloudFormation parameters. Although AWS CloudFormation parameters can be defined in the AWS CDK, they are generally discouraged because AWS CloudFormation parameters are resolved only during deployment. This means that you cannot determine their value in your code. For example, to conditionally include a resource in your app based on the value of a parameter, you must set up an AWS CloudFormation condition and tag the resource with this condition. Because the AWS CDK takes an approach where concrete templates are resolved at synthesis time, you can use an if statement to check the value to determine whether a resource should be defined or some behavior should be applied.

Parameters in the cdk would be done through context values either from the command line or via the cdk.json as documented here: https://docs.aws.amazon.com/cdk/latest/guide/get_context_var.html

like image 82
Max Schenkelberg Avatar answered Sep 24 '22 05:09

Max Schenkelberg