Borislav Hadzhiev
Reading timeยท2 min
Photo from Unsplash
CDK gets compiled down to CloudFormation before deployment, so we are able to use CloudFormation Parameters in CDK.
Parameters are key-value pairs that we pass into the CDK stack at deployment time.
The cause of the "CloudFormation Parameters are missing a value" error in CDK, is that we have defined Parameters for which we didn't provide values when we tried to deploy.
For example:
export class MyCdkStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props: cdk.StackProps) { super(scope, id, props); const s3Bucket = new s3.Bucket(this, 'avatars-bucket', { removalPolicy: cdk.RemovalPolicy.DESTROY, }); // ๐ Defining a Parameter const bucketName = new cdk.CfnParameter(this, 'bucketName', { type: 'String', description: 'The name of the S3 bucket', }); } }
We defined a Parameter, using the CfnParameter construct.
I'll now try to deploy my stack:
npx aws-cdk deploy
After issuing the command, I get the Error:
In order to resolve the "CloudFormation Parameters are missing a value" error,
we have to provide the necessary parameters via the --parameters
flag when
deploying.
For example:
npx aws-cdk deploy my-cdk-stack \ --parameters bucketName=avatars-bucket
This resolves the error and we can deploy our CDK stack successfully.
If you have multiple parameters, you have to specify the --parameters
flag for
each one:
npx aws-cdk deploy my-cdk-stack \ --parameters bucketName=avatars-bucket \ --parameters env=development
Parameters in CDK derive their name from the id
parameter we've passed to the
CfnParameter
construct and from their location within the stack.
Since parameters in CloudFormation are only allowed to contain alphanumeric
[A-Za-z0-9] characters, if we specify an id
that is non-alphanumeric, all of
the non-alphanumeric characters will be stripped.
For example, let's specify a non-alphanumeric id
for our bucketName
parameter:
// ๐ BUCKET_NAME contains an underscore const bucketName = new cdk.CfnParameter(this, 'BUCKET_NAME', { type: 'String', description: 'The name of the S3 bucket', });
Since BUCKET_NAME
contains an underscore it's not alphanumeric. I'll now try
to deploy:
npx aws-cdk deploy \ --parameters BUCKET_NAME=myBucket
The output shows an error:
From the error message, we can see that CDK automatically stripped all non alphanumeric characters from our parameter key.
To resolve the error, we have to remove all of the non-alphanumeric characters
from the parameter key and provide the --parameters
flag when deploying.