業務の中でAWS CDKに始めて触れることになりました。その学習記録を残します。
環境
% sw_vers
ProductName: macOS
ProductVersion: 14.7.3
BuildVersion: 23H417
% aws --version
aws-cli/2.9.10 Python/3.9.11 Darwin/23.6.0 exe/x86_64 prompt/off
% cdk --version
2.178.2 (build 89c49cc)
% npm --version
11.1.0
% node --version
v20.17.0
CDK プロジェクトの作成
今回はTypescript
を利用するため cdk init app --language typescript
を実行してCDKプロジェクトを初期化した。
% cdk init app --language typescript
Applying project template app for typescript
# Welcome to your CDK TypeScript project
This is a blank project for CDK development with TypeScript.
The `cdk.json` file tells the CDK Toolkit how to execute your app.
## Useful commands
* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `npx cdk deploy` deploy this stack to your default AWS account/region
* `npx cdk diff` compare deployed stack with current state
* `npx cdk synth` emits the synthesized CloudFormation template
Initializing a new git repository...
Executing npm install...
npm warn 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 warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
✅ All done!
NOTICES (What's this? https://github.com/aws/aws-cdk/wiki/CLI-Notices)
32775 (cli): CLI versions and CDK library versions have diverged
Overview: Starting in CDK 2.179.0, CLI versions will no longer be in
lockstep with CDK library versions. CLI versions will now be
released as 2.1000.0 and continue with 2.1001.0, etc.
Affected versions: cli: >=2.0.0 <=2.1005.0
More information at: https://github.com/aws/aws-cdk/issues/32775
If you don’t want to see a notice anymore, use "cdk acknowledge <id>". For example, "cdk acknowledge 32775".
次のように初期化(ファイル生成)されました。
※cdk.out
は何かしらのcdkコマンドを実行すると作成されるようです。
% tree .
.
├── bin
│ └── infrastructure.ts
├── cdk.out
│ └── (省略)
├── lib
│ └── infrastructure-stack.ts
├── node_modules
│ └── (省略)
├── test
│ └── infrastructure.test.ts
├── README.md
├── cdk.json
├── jest.config.js
├── package-lock.json
├── package.json
└── tsconfig.json
作成されたファイルを見ていく
README.md
やpackage.json
など、一般的(?)なファイルは見ません。
bin/infrastructure.ts
bin/infrastructure.ts
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { InfrastructureStack } from '../lib/infrastructure-stack';
const app = new cdk.App();
new InfrastructureStack(app, 'InfrastructureStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */
/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
cdk appを記述するファイルです。
cfnはStackという単位でリソースを構築します。lib
ディレクトリ配下に作成されるStackクラスをインスタンス化していきます。
lib/infrastructure-stack.ts
lib/infrastructure-stack.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
// import * as sqs from 'aws-cdk-lib/aws-sqs';
export class InfrastructureStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// example resource
// const queue = new sqs.Queue(this, 'InfrastructureQueue', {
// visibilityTimeout: cdk.Duration.seconds(300)
// });
}
}
Stackを定義するファイルです。
lib/xxx-stack.ts
を複数作成し、bin/infrastructure.ts
でインスタンス化していきます。
cdk.json
{
"app": "npx ts-node --prefer-ts-exts bin/infrastructure.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
...省略...
}
}
cdkの動作設定のようなものが記載されいるように読めます。
context
セクションの値は、appで読み取ることができるようですが、cdk.context.json
というファイルを作成できるようなので、独自の値はそちらに記述した方が良さそうです。
Next
S3バケットを構築しながら、cdkコマンドの動きを追ってみようかと考えています。
参考