Felix
Felix

Reputation: 526

How to get rid of the BootstrapVersion parameter with cdk synth?

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.

Upvotes: 11

Views: 6469

Answers (4)

Kartik Thummar
Kartik Thummar

Reputation: 19

We can do the same using Python CDK in the below mentioned manner :

import aws_cdk as cdk
from constructs import Construct
from aws_cdk import (
    Stack,
)


class CloudformationStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:

        super().__init__(scope, construct_id, stack_name="cloudformation-stack-management" ,**kwargs)

        ## CDK resources


app = cdk.App()
CloudformationStack(app, "test-stack",
                    synthesizer=cdk.DefaultStackSynthesizer(generate_bootstrap_version_rule=False))

app.synth()

Upvotes: 1

addicted
addicted

Reputation: 3041

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({})
});

Upvotes: 3

Kaustubh Khavnekar
Kaustubh Khavnekar

Reputation: 2923

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

Upvotes: 7

Felix
Felix

Reputation: 526

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.

Upvotes: 15

Related Questions