AWS CDK with Python

Building a CDK environment

CDK version: 1.83.0 (build 827c5f4)

Advance preparation

Install Python 3 and AWS CLI. Install the AWS CLI because it's needed (but not necessarily) to switch the AWS account that runs the CDK using the profile. After installation, set the required profile.

brew install python3 awscli
aws configure --profile Profile name

Install CDK and create template project

brew install aws-cdk

Execute the command on a blank directory.

cdk init sample-app --language python

setup

$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt

Minor correction of template (as of January 12, 2021)

TODO

sample

Add repository to 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)

        #Create repository in ECR and set life cycle and permissions (dev account only)
        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'))

Related information

CDK related

Technique

TBD --Creating an EC2 instance - AMI - IAM --Existing VPC --Existing subnet --Security group --Target group --Tag

Recommended Posts

AWS CDK with Python
Text extraction with AWS Textract (Python3.6)
[AWS] Build an ECR with AWS CDK
Notify HipChat with AWS Lambda (Python)
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Easily build network infrastructure and EC2 with AWS CDK Python
Excel with Python
Microcomputer with Python
Cast with python
[AWS] Using ini files with Lambda [Python]
I want to play with aws with python
Connect to s3 with AWS Lambda Python
Building an AWS Fargate Service with AWS CDK
Touch AWS with Serverless Framework and Python
Python + Selenium + Headless Chromium with aws lambda
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
Drive WebDriver with python
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Run Blender with python