Deploy test project

AWS CDK apps are effectively only a definition of your infrastructure using code. When CDK apps are executed, they produce (or “synthesize”, in CDK parlance) an AWS CloudFormation template for each stack defined in your application.

To synthesize a CloudFormation template from a CDK app, use the cdk synth command

The CDK CLI requires you to be in the same directory as your cdk.json file.

Let’s check out the template synthesized from the sample app:

cdk synth

It will output the following CloudFormation template:

Resources:
  CdkLabQueue2E31BBF6:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 300
    Metadata:
      aws:cdk:path: CdkLabStack/CdkLabQueue/Resource
  CdkLabQueuePolicy42EE3F64:
    Type: AWS::SQS::QueuePolicy
    Properties:
      PolicyDocument:
        Statement:
          - Action: sqs:SendMessage
            Condition:
              ArnEquals:
                aws:SourceArn:
                  Ref: CdkLabTopic75C68173
            Effect: Allow
            Principal:
              Service: sns.amazonaws.com
            Resource:
              Fn::GetAtt:
                - CdkLabQueue2E31BBF6
                - Arn
        Version: "2012-10-17"
      Queues:
        - Ref: CdkLabQueue2E31BBF6
    Metadata:
      aws:cdk:path: CdkLabStack/CdkLabQueue/Policy/Resource
  CdkLabQueueCdkLabStackCdkLabTopic1CBA682F3339E555:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: sqs
      TopicArn:
        Ref: CdkLabTopic75C68173
      Endpoint:
        Fn::GetAtt:
          - CdkLabQueue2E31BBF6
          - Arn
    Metadata:
      aws:cdk:path: CdkLabStack/CdkLabQueue/CdkLabStackCdkLabTopic1CBA682F/Resource
  CdkLabTopic75C68173:
    Type: AWS::SNS::Topic
    Metadata:
      aws:cdk:path: CdkLabStack/CdkLabTopic/Resource
...........

As you can see, this template includes four resources:

  • AWS::SQS::Queue - our queue
  • AWS::SQS::QueuePolicy - the IAM policy which allows this topic to send messages to the queue
  • AWS::SNS::Subscription - the subscription between the queue and the topic
  • AWS::SNS::Topic - our topic

Bootstrapping an environment

The first time you deploy an AWS CDK app into an environment (account/region), you’ll need to install a “bootstrap stack”. This stack includes resources that are needed for the toolkit operation. For example, the stack includes an S3 bucket that is used to store templates and assets during the deployment process.

You can use the cdk bootstrap command to install the bootstrap stack into an environment:

The CDK CLI requires you to be in the same directory as your cdk.json file.

cdk bootstrap

You will see output like this:

 ⏳⏳  Bootstrapping environment aws://123456789012/eu-west-1...
CDKToolkit: creating CloudFormation changeset...
 0/2 | 3:06:15 PM | CREATE_IN_PROGRESS   | AWS::S3::Bucket | StagingBucket
 0/2 | 3:06:15 PM | CREATE_IN_PROGRESS   | AWS::S3::Bucket | StagingBucket Resource creation Initiated
 1/2 | 3:06:37 PM | CREATE_COMPLETE      | AWS::S3::Bucket | StagingBucket
 2/2 | 3:06:38 PM | CREATE_COMPLETE      | AWS::CloudFormation::Stack | CDKToolkit
 ✅✅  Environment aws://123456789012/eu-west-1 bootstrapped.

Deploy stack

In order to deploy the resources defined in your AWS CDK app to AWS, use cdk deploy command.

cdk deploy

You should see a warning like the following:

This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:

IAM Statement Changes
┌───┬────────────────────┬────────┬─────────────────┬───────────────────────────┬───────────────────────────────────────┐
│   │ Resource           │ Effect │ Action          │ Principal                 │ Condition                             │
├───┼────────────────────┼────────┼─────────────────┼───────────────────────────┼───────────────────────────────────────┤
│ + │ ${CdkLabQueue.Arn} │ Allow  │ sqs:SendMessage │ Service:sns.amazonaws.com │ "ArnEquals": {                        │
│   │                    │        │                 │                           │   "aws:SourceArn": "${CdkLabTopic}"   │
│   │                    │        │                 │                           │ }                                     │
└───┴────────────────────┴────────┴─────────────────┴───────────────────────────┴───────────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)

Do you wish to deploy these changes (y/n)?

This is warning you that deploying the app entails some risk. Since we need to allow the topic to send messages to the queue, enter y to deploy the stack and create the resources.

Output should look like the following:

CdkLabStack: deploying...
CdkLabStack: creating CloudFormation changeset...
 0/6 | 3:13:32 PM | CREATE_IN_PROGRESS   | AWS::SNS::Topic        | CdkLabTopic (CdkLabTopic75C68173)
 0/6 | 3:13:32 PM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata     | CDKMetadata
 0/6 | 3:13:32 PM | CREATE_IN_PROGRESS   | AWS::SQS::Queue        | CdkLabQueue (CdkLabQueue2E31BBF6)
 0/6 | 3:13:32 PM | CREATE_IN_PROGRESS   | AWS::SNS::Topic        | CdkLabTopic (CdkLabTopic75C68173) Resource creation Initiated
 0/6 | 3:13:32 PM | CREATE_IN_PROGRESS   | AWS::SQS::Queue        | CdkLabQueue (CdkLabQueue2E31BBF6) Resource creation Initiated
 0/6 | 3:13:33 PM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata     | CDKMetadata Resource creation Initiated
 1/6 | 3:13:33 PM | CREATE_COMPLETE      | AWS::CDK::Metadata     | CDKMetadata
 2/6 | 3:13:33 PM | CREATE_COMPLETE      | AWS::SQS::Queue        | CdkLabQueue (CdkLabQueue2E31BBF6)
 3/6 | 3:13:43 PM | CREATE_COMPLETE      | AWS::SNS::Topic        | CdkLabTopic (CdkLabTopic75C68173)
 3/6 | 3:13:44 PM | CREATE_IN_PROGRESS   | AWS::SNS::Subscription | CdkLabQueue/CdkLabStackCdkLabTopic1CBA682F (CdkLabQueueCdkLabStackCdkLabTopic1CBA682F3339E555)
 3/6 | 3:13:44 PM | CREATE_IN_PROGRESS   | AWS::SQS::QueuePolicy  | CdkLabQueue/Policy (CdkLabQueuePolicy42EE3F64)
 3/6 | 3:13:45 PM | CREATE_IN_PROGRESS   | AWS::SQS::QueuePolicy  | CdkLabQueue/Policy (CdkLabQueuePolicy42EE3F64) Resource creation Initiated
 3/6 | 3:13:45 PM | CREATE_IN_PROGRESS   | AWS::SNS::Subscription | CdkLabQueue/CdkLabStackCdkLabTopic1CBA682F (CdkLabQueueCdkLabStackCdkLabTopic1CBA682F3339E555) Resource creation Initiated
 4/6 | 3:13:45 PM | CREATE_COMPLETE      | AWS::SQS::QueuePolicy  | CdkLabQueue/Policy (CdkLabQueuePolicy42EE3F64)
 5/6 | 3:13:45 PM | CREATE_COMPLETE      | AWS::SNS::Subscription | CdkLabQueue/CdkLabStackCdkLabTopic1CBA682F (CdkLabQueueCdkLabStackCdkLabTopic1CBA682F3339E555)
 6/6 | 3:13:46 PM | CREATE_COMPLETE      | AWS::CloudFormation::Stack | CdkLabStack

 ✅✅  CdkLabStack

Stack ARN:
arn:aws:cloudformation:<your region>:<your account id>:stack/CdkLabStack/a3dbbb60-5fb4-11ea-b008-021ebf3a767c

The CloudFormation Console

CDK apps are deployed through AWS CloudFormation. Each CDK stack maps 1:1 with CloudFormation stack.

This means that you can use the AWS CloudFormation console in order to manage your stacks.

Let’s take a look at the AWS CloudFormation console. Go to AWS Console and search for CloudFormation.

CDK project

You will see something like this:

CDK project

Select CdkLabStack and open the Resources tab:

CDK project