※適時更新
CDK環境の構築
CDK version: 1.83.0 (build 827c5f4)
事前準備
Python3とAWS CLIをインストールする。
AWS CLIは、プロファイルを使って、CDKを実行するAWSアカウントを切り変えるために必要(でなくもないけど)なのでインストールする。
インストール後は必要な分、プロファイルを設定しておく。
brew install python3 awscli
aws configure --profile プロファイル名
CDKのインストールとテンプレートプロジェクトの作成
brew install aws-cdk
ブランクディレクトリ上でコマンドを実行する。
cdk init sample-app --language python
セットアップ
$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt
テンプレートの細かい修正(2021/01/12時点)
TODO
サンプル
ECRへのリポジトリ追加
from aws_cdk import (
aws_iam as iam,
aws_ecr as ecr,
core
)
class RepositoryStack(core.Stack):
def __init__(self, scope: core.Construct, construct_id: str, stage: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# ECRにリポジトリ作成し、ライフサイクルとパーミッションを設定(devアカウントのみ)
if stage == 'dev':
lifecycle_rules = [
ecr.LifecycleRule(
description='Delete old images',
max_image_count=5,
rule_priority=1,
tag_status=ecr.TagStatus.UNTAGGED
)
]
repository = ecr.Repository(
self,
'EcrRepository',
lifecycle_rules=lifecycle_rules,
repository_name='seriwb/nanika'
)
repository.grant_pull(iam.AccountPrincipal('staging account'))
repository.grant_pull(iam.AccountPrincipal('production account'))
関連情報
CDK関連
- https://cdkworkshop.com/
- https://github.com/aws/aws-cdk
- https://github.com/aws-samples/aws-cdk-examples
- https://docs.aws.amazon.com/ja_jp/cdk/latest/guide/work-with-cdk-python.html
- https://docs.aws.amazon.com/ja_jp/cdk/latest/guide/tagging.html
テクニック
- https://qiita.com/mira_cocoa/items/0435cf1cfabaabeb7027
- https://qiita.com/akiraabe/items/86c995496d5ee2786088
TBD
- EC2インスタンスの作成
- AMI
- IAM
- 既存VPC
- 既存サブネット
- セキュリティグループ
- ターゲットグループ
- タグ