メインコンテンツまでスキップ

CDKプロジェクトの作成

作成
  • 作成者: moritalous
  • 作成日: 2023/08/14

プロジェクトの作成

  1. プロジェクトフォルダーの作成

    mkdir -p user1/handson-cdk
    cd user1/handson-cdk
  2. プロジェクト作成

    cdk init --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
    * `cdk deploy` deploy this stack to your default AWS account/region
    * `cdk diff` compare deployed stack with current state
    * `cdk synth` emits the synthesized CloudFormation template

    Initializing a new git repository...
    hint: Using 'master' as the name for the initial branch. This default branch name
    hint: is subject to change. To configure the initial branch name to use in all
    hint: of your new repositories, which will suppress this warning, call:
    hint:
    hint: git config --global init.defaultBranch <name>
    hint:
    hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
    hint: 'development'. The just-created branch can be renamed via this command:
    hint:
    hint: git branch -m <name>
    Executing npm install...
    ✅ All done!
    ****************************************************
    *** Newer version of CDK is available [2.91.0] ***
    *** Upgrade recommended (npm install -g aws-cdk) ***
    ****************************************************

作成されるファイル

  • ファイルの一覧

    tree
    .
    ├── bin
    │   └── handson-cdk.ts
    ├── cdk.json
    ├── jest.config.js
    ├── lib
    │   └── handson-cdk-stack.ts
    ├── package.json
    ├── package-lock.json
    ├── README.md
    ├── test
    │   └── handson-cdk.test.ts
    └── tsconfig.json

    3 directories, 9 files
  • bin/handson-cdk.ts

    作成したCDKプロジェクトの大元になるソースで、package.jsonbinとして指定されています。

    Appの定義宣言とStackのインスタンス生成を行います。複数のStackをまとめたものがAppみたいな感じかと思います。

    参照:Apps and stacks

    bin/handson-cdk.ts
    #!/usr/bin/env node
    import 'source-map-support/register';
    import * as cdk from 'aws-cdk-lib';
    import { HandsonCdkStack } from '../lib/handson-cdk-stack';

    const app = new cdk.App();
    new HandsonCdkStack(app, 'HandsonCdkStack', {
    /* 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 */
    });
  • lib/handson-cdk-stack.ts

    こちらは一つのStackに該当します。今回の手順ではスタックが一つですので、このファイルを更新していきます。

    lib/handson-cdk-stack.ts
    import * as cdk from 'aws-cdk-lib';
    import { Construct } from 'constructs';
    // import * as sqs from 'aws-cdk-lib/aws-sqs';

    export class HandsonCdkStack 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, 'HandsonCdkQueue', {
    // visibilityTimeout: cdk.Duration.seconds(300)
    // });
    }
    }