7

When generating the cloudformation template with aws cdk:

cdk synth

I always get:

  "Parameters": {
    "BootstrapVersion": {
      "Type": "AWS::SSM::Parameter::Value<String>",
      ...

Here the code:

import * as cdk from 'aws-cdk-lib';
import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sqs from 'aws-cdk-lib/aws-sqs';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    
    const queue = new sqs.Queue(this, 'Example', {
      visibilityTimeout: cdk.Duration.seconds(300)
    });
  }
};

const app = new cdk.App();
new MyStack(app, 'MyStack');

Full output (some shortening ...):

$ cdk synth 
Resources:
  ExampleA925490C:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 300
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Metadata:
      aws:cdk:path: MyStack/Example/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/zPSM9EzUEwsL9ZNTsnWzclM0qsOLklMztYBCsUXFxbrVQeWppam6jin5YEZtSBWUGpxfmlRMljUOT8vJbMkMz+vVicvPyVVL6tYv8zQTM8YaGpWcWamblFpXklmbqpeEIQGAChZc6twAAAA
    Metadata:
      aws:cdk:path: MyStack/CDKMetadata/Default
    Condition: CDKMetadataAvailable
Conditions:
  CDKMetadataAvailable:
    Fn::Or:
      - Fn::Or:
          - Fn::Equals:
              - Ref: AWS::Region
              - af-south-1
          ...
      - Fn::Or:
          - Fn::Equals:
              - Ref: AWS::Region
              - us-west-1
          ...
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
Rules:
  CheckBootstrapVersion:
    Assertions:
      - Assert:
          Fn::Not:
            - Fn::Contains:
                - - "1"
                  - "2"
                  - "3"
                  - "4"
                  - "5"
                - Ref: BootstrapVersion
        AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.

Here the environment:

$ cdk doctor
ℹ️ CDK Version: 2.8.0 (build 8a5eb49)
ℹ️ AWS environment variables:
  - AWS_PAGER = 
  - AWS_DEFAULT_PROFILE = sbxb.admin
  - AWS_STS_REGIONAL_ENDPOINTS = regional
  - AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  - AWS_SDK_LOAD_CONFIG = 1
ℹ️ No CDK environment variables

How to get rid of that cloudformation parameter? I just want to use CDK to create a cloudformation template.

Later I want to use that template with the service catalog and don't want the BootstrapVersion parameter to be exposed nor do I need it.

Felix
  • 433
  • 4
  • 10
  • 1
    Are you planning to deploy the generated CloudFormation template separately or using the `cdk deploy` command? – Kaustubh Khavnekar Jan 17 '22 at 16:52
  • 1
    @KaustubhKhavnekar I am planning to deploy it seperately with `aws cloudformation` not with cdk! – Felix Jan 17 '22 at 17:13
  • @Paolo this is completely independent of the content of my stack I guess. Here some excerpts: The app is something like: ``` const app = new cdk.App(); new EMRStack(app, 'EMRStack', { env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, }); ``` The stack is created like: ``` export class EMRStack extends Stack { constructor(scope: Construct, id: string, props: EMRStackProps) { super(scope, id, props); ... ``` – Felix Jan 17 '22 at 17:17
  • 1
    @Paolo I will. But the example you mention does create the unwanted Parameters. – Felix Jan 17 '22 at 17:30
  • @Paolo is probably using CDK v1, see my answer – Kaustubh Khavnekar Jan 17 '22 at 17:44
  • Thanks @KaustubhKhavnekar your hint worked. It needed some try and errors to find the correct code. I have created an answer containing the code. – Felix Jan 17 '22 at 17:59
  • If you are looking to use the template with service catalog. You can write products directly with code using the `ProductStack`. See https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-servicecatalog.ProductStack.html – BernieFeynman Mar 24 '22 at 13:52

3 Answers3

12

Here the modified code which works:

import * as cdk from 'aws-cdk-lib';
import { DefaultStackSynthesizer, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sqs from 'aws-cdk-lib/aws-sqs';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const queue = new sqs.Queue(this, 'Example', {
      visibilityTimeout: cdk.Duration.seconds(300)
    });
  }
};

const app = new cdk.App();
new MyStack(app, 'MyStack' , {
  synthesizer: new DefaultStackSynthesizer({
    generateBootstrapVersionRule: false
  })
});

As mentioned by the other answer one has to override the DefaultStackSynthesizer with generateBootstrapVersionRule: false.

Felix
  • 433
  • 4
  • 10
6

Edit: updated the answer to mention the generateBootstrapVersionRule parameter. See @Felix's answer for code.

By default, the following is included in all templates when using DefaultStackSynthesizer:

"Parameters": {
    "BootstrapVersion": {
      "Type": "AWS::SSM::Parameter::Value<String>",
      "Default": "/cdk-bootstrap/hnb659fds/version",
      "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
    }
  },
  "Rules": {
    "CheckBootstrapVersion": {
      "Assertions": [
        {
          "Assert": {
            "Fn::Not": [
              {
                "Fn::Contains": [
                  [
                    "1",
                    "2",
                    "3",
                    "4",
                    "5"
                  ],
                  {
                    "Ref": "BootstrapVersion"
                  }
                ]
              }
            ]
          },
          "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
        }
      ]
    }
  }

The BootstrapVersion parameter and the associated rule are used by CDK to check the version of the Bootstrap stack which is deployed in your environment. You can remove it if you are confident that your stack doesn't require bootstrapping or you have the correct BootstrapVersion. The parameter isn't used anywhere else in stack.

By default, CDK v2 uses the DefaultStackSynthesizer so this parameter will always be included. One way of avoid this is to create a custom object with generateBootstrapVersionRule parameter with a value of false (see Felix's answer for code). Alternatively can also specify the LegacyStackSynthesizer when instantiating the CDK to avoid creating the parameter, however this makes a few changes in the way your stack is synthesized and how you use the bootstrap stack. A table of differences is given in the v1 documentation link below.

CDK v1 is the opposite and uses the LegacyStackSynthesizer by default.

References

Kaustubh Khavnekar
  • 2,553
  • 2
  • 14
0

Adding on to the great answers above. Another way to skip the bootstrap parameters is to use BootstraplessSynthesizer. Here is how to integrate it in your code

import * as cdk from 'aws-cdk-lib';
import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sqs from 'aws-cdk-lib/aws-sqs';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    
    const queue = new sqs.Queue(this, 'Example', {
      visibilityTimeout: cdk.Duration.seconds(300)
    });
  }
};

const app = new cdk.App();
new MyStack(app, 'MyStack', {
  synthesizer: new cdk.BootstraplessSynthesizer({})
});
addicted
  • 2,901
  • 3
  • 28
  • 49