CDK initializaion:
- familiarize with the generated CDK app files and directories layout
#
# always initialize the CDK project as "infra" to work with BMO GitHub Actions CDK workflow:
#
kaau@OCLT71159526 ~$ mkdir -p ${HOME}/myproj/helloworld/infra
kaau@OCLT71159526 ~$ ls -ld ${HOME}/myproj/helloworld/infra
drwxr-xr-x 1 kaau 1049089 0 Jun 6 14:55 /c/Users/kaau/myproj/helloworld/infra
kaau@OCLT71159526 ~$ cd ${HOME}/myproj/helloworld/infra
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -l
total 0
#
# initialize a new CDK project:
#
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk init --language=typescript
Applying project template [[green:app]] for [[blue:typescript]]
[[green:# Welcome to your CDK TypeScript project]]
[[green:This is a blank project for CDK development with TypeScript.]]
[[green:The `cdk.json` file tells the CDK Toolkit how to execute your app.]]
[[green:## Useful commands]]
[[green:* `npm run build` compile typescript to js]]
[[green:* `npm run watch` watch for changes and compile]]
[[green:* `npm run test` perform the jest unit tests]]
[[green:* `npx cdk deploy` deploy this stack to your default AWS account/region]]
[[green:* `npx cdk diff` compare deployed stack with current state]]
[[green:* `npx cdk synth` emits the synthesized CloudFormation template]]
Initializing a new git repository...
Executing [[green:npm install]]...
npm [[yellow:warn]] [[blue:deprecated]] inflight@1.0.6: This module is not supported, and leaks memory. Do not use it.
Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm [[yellow:warn]] [[blue:deprecated]] glob@7.2.3: Old versions of glob are not supported, and contain widely publicized security vulnerabilities,
which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me
npm [[yellow:warn]] [[blue:deprecated]] glob@10.5.0: Old versions of glob are not supported, and contain widely publicized security vulnerabilities,
which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me
[[green:✅]] All done!
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -la
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:17 .
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:15 ..
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:16 .git
-rw-r--r-- 1 kaau 1049089 93 Jul 25 16:16 .gitignore
-rw-r--r-- 1 kaau 1049089 65 Jul 25 16:16 .npmignore
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:16 bin ---#1: CDK entry point
-rw-r--r-- 1 kaau 1049089 5926 Jul 25 16:16 cdk.json
-rw-r--r-- 1 kaau 1049089 228 Jul 25 15:24 jest.config.js
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:16 lib ---#2: CDK stack definition (implementation)
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:17 node_modules
-rw-r--r-- 1 kaau 1049089 464 Jul 25 16:16 package.json ---npm package manifest
-rw-r--r-- 1 kaau 1049089 225565 Jul 25 16:17 package-lock.json
-rw-r--r-- 1 kaau 1049089 537 Jul 25 16:16 README.md
drwxr-xr-x 1 kaau 1049089 0 Jul 25 16:16 test
-rw-r--r-- 1 kaau 1049089 810 Jul 25 15:24 tsconfig.json ---TypeScript configuration
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat package.json
[[blue:{]]
[[blue: "name": "infra",]]
[[blue: "version": "0.1.0",]]
[[blue: "scripts": {]]
[[blue: "build": "tsc",]]
[[blue: "watch": "tsc -w",]]
[[blue: "test": "jest",]]
[[blue: "cdk": "cdk"]]
[[blue: },]]
[[blue: "devDependencies": {]]
[[blue: "@swc/core": "^1.15.0",]]
[[blue: "@swc/jest": "^0.2.39",]]
[[blue: "@types/jest": "^30",]]
[[blue: "@types/node": "^24.10.1",]]
[[blue: "jest": "^30",]]
[[blue: "aws-cdk": "2.1132.0",]] ---BMO CDK project does not need aws-cdk
[[blue: "tsx": "^4.23.0",]]
[[blue: "typescript": "~7.0.2"]] ---latest TypeScript version is 7.x.x
[[blue: },]]
[[blue: "dependencies": {]]
[[blue: "aws-cdk-lib": "^2.261.0",]]
[[blue: "constructs": "^10.5.0"]]
[[blue: }]]
[[blue:}]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ du -sh node_modules
286M node_modules ---CDK dependencies are quite large for a simple "Hello World" project
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat .gitignore
[[blue:*.js]]
[[blue:!jest.config.js]]
[[blue:*.d.ts]]
[[blue:node_modules]] ---GitHub Actions workflow will install dependencies based on package.json
[[blue:]]
[[blue:## CDK asset staging directory]]
[[blue:.cdk.staging]]
[[blue:cdk.out]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -l bin
-rwxr-xr-x 1 kaau 1049089 908 Jul 25 16:16 infra.ts
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -l lib
-rw-r--r-- 1 kaau 1049089 476 Jul 25 16:16 infra-stack.ts
#
# glance through the generated CDK app files:
#
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat bin/infra.ts
[[blue:#!/usr/bin/env node]]
[[blue:import * as cdk from 'aws-cdk-lib/core';]]
[[blue:import { InfraStack } from '../lib/infra-stack';]]
[[blue:]]
[[blue:const app = new cdk.App();]]
[[blue:new InfraStack(app, 'InfraStack', {]]
[[blue: /* If you don't specify 'env', this stack will be environment-agnostic.]]
[[blue: * Account/Region-dependent features and context lookups will not work,]]
[[blue: * but a single synthesized template can be deployed anywhere. */]]
[[blue:]]
[[blue: /* Uncomment the next line to specialize this stack for the AWS Account]]
[[blue: * and Region that are implied by the current CLI configuration. */]]
[[blue: // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },]]
[[blue:]]
[[blue: /* Uncomment the next line if you know exactly what Account and Region you]]
[[blue: * want to deploy the stack to. */]]
[[blue: env: { account:]] [[blue:"123456789012']][[blue:, region: 'us-east-1' },]] ---remove the comment to specify the AWS account and region for the stack
[[blue:]]
[[blue: /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */]]
[[blue:});]]
#
# this stack does nothing but it is a valid CDK stack that can be synthesized and deployed to AWS CloudFormation:
#
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat lib/infra-stack.ts
[[blue:import * as cdk from 'aws-cdk-lib/core';]]
[[blue:import { Construct } from 'constructs';]]
[[blue:// import * as sqs from 'aws-cdk-lib/aws-sqs';]]
[[blue:]]
[[blue:export class InfraStack extends cdk.Stack {]]
[[blue: constructor(scope: Construct, id: string, props?: cdk.StackProps) {]]
[[blue: super(scope, id, props);]]
[[blue:]]
[[blue: // The code that defines your stack goes here]]
[[blue:]]
[[blue: // example resource]]
[[blue: // const queue = new sqs.Queue(this, 'InfraQueue', {]]
[[blue: // visibilityTimeout: cdk.Duration.seconds(300)]]
[[blue: // });]]
[[blue: }]]
[[blue:}]]
tsc compilation:
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat tsconfig.json
[[blue:{]]
[[blue: "compilerOptions": {]]
[[blue: "target": "ES2022",]] ---ES2023?
[[blue: "module": "NodeNext",]]
[[blue: "moduleResolution": "NodeNext",]]
[[blue: "lib": []]
[[blue: "es2022"]]
[[blue: ],]]
[[blue: "declaration": true,]]
[[blue: "strict": true,]]
[[blue: "noImplicitAny": true,]]
[[blue: "strictNullChecks": true,]]
[[blue: "noImplicitThis": true,]]
[[blue: "alwaysStrict": true,]]
[[blue: "noUnusedLocals": false,]]
[[blue: "noUnusedParameters": false,]]
[[blue: "noImplicitReturns": true,]]
[[blue: "noFallthroughCasesInSwitch": false,]]
[[blue: "inlineSourceMap": true,]]
[[blue: "inlineSources": true,]]
[[blue: "experimentalDecorators": true,]]
[[blue: "strictPropertyInitialization": false,]]
[[blue: "skipLibCheck": true,]] ---Cloud Blog Post 3055 BMO CDK v10.2.0-AWS CDK Library Upgrade
[[blue: "typeRoots": []]
[[blue: "./node_modules/@types"]]
[[blue: ],]]
[[blue: "noEmit": true,]] ---no produce JS output?
[[blue: "isolatedModules": true,]]
[[blue: "types": []]
[[blue: "jest",]]
[[blue: "node"]]
[[blue: ]]]
[[blue: },]]
[[blue: "exclude": []]
[[blue: "node_modules",]]
[[blue: "cdk.out"]]
[[blue: ]]]
[[blue:}]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ tsc
(....no error! Viola!)
cdk operations:
- cdk bootstrap: CDK Bootstrap All AWS Accounts-Legacy and CFCT
- cdk list
- cdk synth: during workflow “diff”
- cdk deploy: during workflow “deploy”
- cdk destroy: Self-Serve Stack Deletion Feature, aka “delete-stack”
#
# expected to see the following error when running `cdk bootstrap` with dummy AWS credentials:
#
kaau@OCLT71159526 ~/myproj/helloworld/infra$ export AWS_ACCESS_KEY_ID=123456789012
kaau@OCLT71159526 ~/myproj/helloworld/infra$ export AWS_SECRET_ACCESS_KEY=dummy_secret
kaau@OCLT71159526 ~/myproj/helloworld/infra$ export AWS_REGION=us-east-1
kaau@OCLT71159526 ~/myproj/helloworld/infra$ export | grep AWS_
declare -x AWS_ACCESS_KEY_ID="123456789012"
declare -x AWS_REGION="us-east-1"
declare -x AWS_SECRET_ACCESS_KEY="dummy_secret"
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -ld cdk.out
[[red:ls: cannot access 'cdk.out': No such file or directory]]
#
# this is OK.....
#
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk bootstrap
⏳ Bootstrapping environment aws://123456789012/us-east-1...
[[red: ❌ Environment aws://123456789012/us-east-1 failed bootstrapping: NoCredentials: Need to perform AWS calls for account 123456789012, but no credentials have been configured]]
[[red: at SdkProvider.forEnvironment (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:387229:17)]]
[[red: at async BootstrapStack.lookup (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:157197:22)]]
[[red: at async Bootstrapper.modernBootstrap (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:157465:25)]]
[[red: at async C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:410245:29 {]]
[[red: type: 'authentication',]]
[[red: source: 'user',]]
[[red: cause: undefined]]
[[red:}]]
[[red:Need to perform AWS calls for account 123456789012, but no credentials have been configured]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -la
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:56 .
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:47 ..
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:48 .git
-rw-r--r-- 1 kaau 1049089 93 Jun 6 15:47 .gitignore
-rw-r--r-- 1 kaau 1049089 65 Jun 6 15:47 .npmignore
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:47 bin
-rw-r--r-- 1 kaau 1049089 5833 Jun 6 15:47 cdk.json
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:56 cdk.out
-rw-r--r-- 1 kaau 1049089 224 Jun 6 01:00 jest.config.js
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:47 lib
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:49 node_modules
-rw-r--r-- 1 kaau 1049089 434 Jun 6 15:47 package.json
-rw-r--r-- 1 kaau 1049089 203539 Jun 6 15:49 package-lock.json
-rw-r--r-- 1 kaau 1049089 536 Jun 6 15:47 README.md
drwxr-xr-x 1 kaau 1049089 0 Jun 6 15:47 test
-rw-r--r-- 1 kaau 1049089 712 Jun 6 01:00 tsconfig.json
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -l cdk.out
-rw-r--r-- 1 kaau 1049089 20 Jun 6 15:56 cdk.out
-rw-r--r-- 1 kaau 1049089 707 Jun 6 15:56 InfraStack.assets.json
-rw-r--r-- 1 kaau 1049089 1506 Jun 6 15:56 InfraStack.metadata.json
-rw-r--r-- 1 kaau 1049089 5070 Jun 6 15:56 InfraStack.template.json
-rw-r--r-- 1 kaau 1049089 29405 Jun 6 15:56 manifest.json
-rw-r--r-- 1 kaau 1049089 1101 Jun 6 15:56 tree.json
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk list
✨ Synthesis time: 9.46s
InfraStack
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk synth
Resources:
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/zPSMzI11zNQTCwv1k1OydbNyUzSCy5JTM7WyctPSdXLKtYvMzLSMzTRM1DMKs7M1C0qzSvJzE3VC4LQAPBm8EQ/AAAA
Metadata:
aws:cdk:path: InfraStack/CDKMetadata/Default
Condition: CDKMetadataAvailable
Conditions:
CDKMetadataAvailable:
Fn::Or:
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- af-south-1
- Fn::Equals:
- Ref: AWS::Region
- ap-east-1
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-1
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-2
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-3
- Fn::Equals:
- Ref: AWS::Region
- ap-south-1
- Fn::Equals:
- Ref: AWS::Region
- ap-south-2
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-1
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-2
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-3
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-4
- Fn::Equals:
- Ref: AWS::Region
- ca-central-1
- Fn::Equals:
- Ref: AWS::Region
- ca-west-1
- Fn::Equals:
- Ref: AWS::Region
- cn-north-1
- Fn::Equals:
- Ref: AWS::Region
- cn-northwest-1
- Fn::Equals:
- Ref: AWS::Region
- eu-central-1
- Fn::Equals:
- Ref: AWS::Region
- eu-central-2
- Fn::Equals:
- Ref: AWS::Region
- eu-north-1
- Fn::Equals:
- Ref: AWS::Region
- eu-south-1
- Fn::Equals:
- Ref: AWS::Region
- eu-south-2
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- eu-west-1
- Fn::Equals:
- Ref: AWS::Region
- eu-west-2
- Fn::Equals:
- Ref: AWS::Region
- eu-west-3
- Fn::Equals:
- Ref: AWS::Region
- il-central-1
- Fn::Equals:
- Ref: AWS::Region
- me-central-1
- Fn::Equals:
- Ref: AWS::Region
- me-south-1
- Fn::Equals:
- Ref: AWS::Region
- sa-east-1
- Fn::Equals:
- Ref: AWS::Region
- us-east-1
- Fn::Equals:
- Ref: AWS::Region
- us-east-2
- Fn::Equals:
- Ref: AWS::Region
- us-west-1
- Fn::Equals:
- Ref: AWS::Region
- us-west-2
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]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk deploy
✨ Synthesis time: 13.72s
[[red:Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment]]
Hello World:
kaau@OCLT71159526 ~/myproj/helloworld/infra$ vi bin/infra.ts
::
::
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat bin/infra.ts
[[blue:#!/usr/bin/env node]]
[[blue:import * as cdk from 'aws-cdk-lib/core';]]
[[blue:import { InfraStack } from '../lib/infra-stack';]]
[[blue:]]
[[blue:const app = new cdk.App();]]
[[blue:new InfraStack(app, 'InfraStack', {]]
[[blue: env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },]]
[[blue:});]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ vi lib/infra-stack.ts
::
::
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cat lib/infra-stack.ts
[[blue:import * as cdk from 'aws-cdk-lib/core';]]
[[blue:import { Construct } from 'constructs';]]
[[blue:]]
[[blue:export class InfraStack extends cdk.Stack {]]
[[blue: constructor(scope: Construct, id: string, props?: cdk.StackProps) {]]
[[blue: super(scope, id, props);]]
[[blue: // Hello world: doing nothing]]
[[blue: }]]
[[blue:}]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk list
✨ Synthesis time: 9.58s
InfraStack
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk synth
Resources:
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/zPSMzI11zNQTCwv1k1OydbNyUzSCy5JTM7WyctPSdXLKtYvMzLSMzTRM1DMKs7M1C0qzSvJzE3VC4LQAPBm8EQ/AAAA
Metadata:
aws:cdk:path: InfraStack/CDKMetadata/Default
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]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ ls -l cdk.out
-rw-r--r-- 1 kaau 1049089 20 Jun 6 16:07 cdk.out
-rw-r--r-- 1 kaau 1049089 725 Jun 6 16:07 InfraStack.assets.json
-rw-r--r-- 1 kaau 1049089 1176 Jun 6 16:07 InfraStack.metadata.json
-rw-r--r-- 1 kaau 1049089 1083 Jun 6 16:07 InfraStack.template.json ---CloudFormation template generated by CDK
-rw-r--r-- 1 kaau 1049089 29380 Jun 6 16:07 manifest.json
-rw-r--r-- 1 kaau 1049089 957 Jun 6 16:07 tree.json
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk deploy
✨ Synthesis time: 14.17s
[[red:Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment]]
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cdk destroy
Are you sure you want to delete: InfraStack (y/n) [[yellow:y]]
InfraStack: destroying... [1/1]
[[red: ❌ InfraStack: destroy failed UnresolvedAccount: Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment]]
[[red: at SdkProvider.resolveEnvironment (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:423087:17)]]
[[red: at process.processTicksAndRejections (node:internal/process/task_queues:105:5)]]
[[red: at async EnvironmentAccess.prepareSdk (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:192150:37)]]
[[red: at async Deployments.destroyStack (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:192514:22)]]
[[red: at async destroyStack (C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:445367:13)]]
[[red: at async C:\Users\kaau\cdk\node_modules\aws-cdk\lib\index.js:430731:11 {]]
[[red: type: 'authentication',]]
[[red: source: 'user',]]
[[red: cause: undefined]]
[[red:}]]
[[red:Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment]]
Clean up:
kaau@OCLT71159526 ~/myproj/helloworld/infra$ unset AWS_ACCESS_KEY_ID
kaau@OCLT71159526 ~/myproj/helloworld/infra$ unset AWS_SECRET_ACCESS_KEY
kaau@OCLT71159526 ~/myproj/helloworld/infra$ unset AWS_REGION
kaau@OCLT71159526 ~/myproj/helloworld/infra$ export | grep AWS_
(.....nothing!)
kaau@OCLT71159526 ~/myproj/helloworld/infra$ cd ${HOME}/myproj
kaau@OCLT71159526 ~/myproj$ rm -rf helloworld