LoginSignup
1
1

More than 3 years have passed since last update.

AWS CDK with Python

Last updated at Posted at 2021-01-14

※適時更新

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関連

テクニック

TBD

  • EC2インスタンスの作成
    • AMI
    • IAM
    • 既存VPC
    • 既存サブネット
    • セキュリティグループ
  • ターゲットグループ
  • タグ
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1