Open6

CDKを触ってみた

モッモッ
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:*",
                "iam:PassRole",
                "iam:GetRole",
                "iam:CreateRole",
                "iam:DeleteRole",
                "iam:AttachRolePolicy",
                "iam:DetachRolePolicy",
                "iam:PutRolePolicy",
                "iam:DeleteRolePolicy",
                "iam:ListRoles",
                "iam:TagRole",
                "ecr:*",
                "s3:*",
                "ssm:*"
            ],
            "Resource": "*"
        }
    ]
}

もう少し絞れそうなんだけどbootstrapで失敗すると面倒なので一発でキめたい。

適当なEC2にポリシーを追加: CDKTestRoleForEC2

$ aws sts get-caller-identity
{
    "UserId": "A****:i-03c3596f0861ae9b6",
    "Account": "****",
    "Arn": "arn:aws:sts::****:assumed-role/CDKTestRoleForEC2/i-03c3596f0861ae9b6"
}
sudo apt update && sudo apt install -y npm
sudo npm install -g aws-cdk

システムの中枢に入れるかどうかは議論がわかれる所だが、まあ使い捨てEC2なので

admin@ip-172-31-0-139:~$ type cdk
cdk is /usr/local/bin/cdk
admin@ip-172-31-0-139:~$ cdk --version
2.1000.2 (build bc82193)
モッモッ
cdk bootstrap aws://****/ap-northeast-1

具体的に cdk bootstrap で行われたこと

  • S3 バケットの作成 (StagingBucket)
  • アセット(Lambda コードやコンテナイメージ)をアップロードするための一時的なストレージ
  • ECR リポジトリの作成 (ContainerAssetsRepository)
  • コンテナを CDK で扱うためのリポジトリ

IAM ロールの作成

  • CloudFormationExecutionRole: CDK スタックをデプロイするためのロール
  • LookupRole: CDK が AWS の既存リソースを参照するためのロール
  • FilePublishingRole: S3 にデプロイするためのロール
  • ImagePublishingRole: コンテナをデプロイするためのロール
  • DeploymentActionRole: デプロイアクションを実行するためのロール

IAM ポリシーのアタッチ

  • 作成された IAM ロールに適切なアクセス権限を付与

SSM パラメータの作成 (CdkBootstrapVersion)

  • AWS Systems Manager (SSM) に、Bootstrap のバージョン情報を記録
    • 例: /cdk-bootstrap/hnb659fds/version
モッモッ

使う

working

mkdir cdk-iam-test
cd cdk-iam-test
cdk init app --language=typescript
$ mkdir cdk-iam-test
cd cdk-iam-test
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...
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>
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'admin@ip-172-31-0-139.(none)')

Unable to initialize git repository for your project.
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!
モッモッ
  • bin/cdk-iam-test.ts CDK アプリのエントリーポイント
  • cdk.json CDK の実行設定
  • jest.config.js Jest(テストフレームワーク)の設定
  • lib/cdk-iam-test-stack.ts AWS リソースを定義するファイル(メインの CDK コード)
  • test/cdk-iam-test.test.ts CDK スタックのテストを書くファイル
  • tsconfig.json TypeScript のコンパイル設定
モッモッ
$ npm run build

> cdk-iam-test@0.1.0 build
> tsc
$ npx cdk synth
Resources:
  S3ReadOnlyPolicyE083D854:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: ""
      ManagedPolicyName: S3ReadOnlyPolicy
...
$ npx cdk destroy
モッモッ

バケットを作成してみる

当然バケット名とポリシーの設定は変数で合致させたいところだ