Das AWS CDK ist ein sehr nützliches Tool, wenn Sie die AWS-Infrastruktur und EC2 schnell und einfach vollautomatisch als Set erstellen möchten. Dieses Mal werde ich Ihnen zeigen, wie Sie AWS CDK Python verwenden.
--aws Schlüsselpaar: Erstellt. Nennen Sie es nicht Schlüssel '.
--VPC wird bei / 21 geschnitten. 10.0.0.0/21 --Subnetz: 1 öffentlich (/ 24), 1 privat (/ 24) --SecurityGroup: Öffnen Sie Port 22 Inbound, um SSH für EC2 zu verwenden
# npm install -g aws-cdk
# mkdir testcdk
# cd testcdk
# cdk init --language python
Applying project template app for python
Initializing a new git repository...
Executing Creating virtualenv...
# Welcome to your CDK Python project!
This is a blank project for Python development with CDK.
The `cdk.json` file tells the CDK Toolkit how to execute your app.
(Kürzung)
## Useful commands
 * `cdk ls`          list all stacks in the app
 * `cdk synth`       emits the synthesized CloudFormation template
 * `cdk deploy`      deploy this stack to your default AWS account/region
 * `cdk diff`        compare deployed stack with current state
 * `cdk docs`        open CDK documentation
Enjoy!
# tree
.
|-- .env
|-- README.md
|-- app.py
|-- cdk.json
|-- requirements.txt
|-- setup.py
|-- source.bat
`-- testcdk
    |-- __init__.py
    `-- testcdk_stack.py
# source .env/bin/activate
(.env)
Fügen Sie die zum Erstellen von EC2 erforderliche Bibliothek in setup.py hinzu
setup.py
(Kürzung)
install_requires=[
        "aws-cdk.core",
        "aws_cdk.aws_ec2",
],
(Kürzung)
# pip install -e .
(Kürzung)
Successfully installed attrs-19.3.0 aws-cdk.aws-cloudwatch-1.32.1 aws-cdk.aws-ec2-1.32.1 aws-cdk.aws-events-1.32.1 aws-cdk.aws-iam-1.32.1 aws-cdk.aws-kms-1.32.1 aws-cdk.aws-logs-1.32.1 aws-cdk.aws-s3-1.32.1 aws-cdk.aws-ssm-1.32.1 aws-cdk.core-1.32.1 aws-cdk.cx-api-1.32.1 aws-cdk.region-info-1.32.1 cattrs-1.0.0 constructs-2.0.1 jsii-1.1.0 publication-0.0.3 python-dateutil-2.8.1 six-1.14.0 testcdk typing-extensions-3.7.4.2
app.py
#!/usr/bin/env python3
from aws_cdk import core
from testcdk.testcdk_stack import TestcdkStack
app = core.App()
TestcdkStack(app, "testcdk", env=core.Environment(region="ap-northeast-1"))
app.synth()
Schreiben Sie einen Stapel, um VPC, SecurityGroup, EC2 zu erstellen.
testcdk_stack.py
from aws_cdk import (
        core,
        aws_ec2 <=Füge das hinzu
)
class TestcdkStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
        # The code that defines your stack goes here
        #Schreiben Sie den Code darunter
        cidr = '10.0.0.0/21' #Schreiben Sie einen CIDR-Block
        vpc = aws_ec2.Vpc(
            self,
            id='test-vpc',
            cidr=cidr,
            nat_gateways=1,
            subnet_configuration=[
                aws_ec2.SubnetConfiguration(
                    cidr_mask=24,  #Definieren Sie die Netzmaske für das öffentliche Subnetz
                    name='public',
                    subnet_type=aws_ec2.SubnetType.PUBLIC,
                ),
                aws_ec2.SubnetConfiguration(
                    cidr_mask=24, #Definieren Sie die Netzmaske für das private Subnetz
                    name='private',
                    subnet_type=aws_ec2.SubnetType.PRIVATE,
                ),
            ],
        )
        security_group = aws_ec2.SecurityGroup(
            self,
            id='test-security-group',
            vpc=vpc,
            security_group_name='test-security-group'
        )
        security_group.add_ingress_rule(
            peer=aws_ec2.Peer.ipv4(cidr),
            connection=aws_ec2.Port.tcp(22), #Öffnen Sie Port 22 mit Inbound
        )
        image_id = aws_ec2.AmazonLinuxImage(generation=aws_ec2.AmazonLinuxGeneration.AMAZON_LINUX_2).get_image(self).image_id #Geben Sie das EC2-Bild an
        aws_ec2.CfnInstance(
            self,
            id='testec2',
            availability_zone="ap-northeast-1a", #Geben Sie AZ an
            image_id=image_id,
            instance_type="t3.micro", #Geben Sie den Instanztyp an
            key_name='testkey', #Geben Sie das Schlüsselpaar an
            security_group_ids=[security_group.security_group_id],
            subnet_id=vpc.private_subnets[0].subnet_id, #Geben Sie diesmal das private Subnetz an
            tags=[{
                "key": "Name",
                "value": "testec2" #Definieren Sie den Namen, der in der Webkonsole angezeigt werden soll
            }]
        )
# cdk synth
Resources:
  testvpc8985080E:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/21
      EnableDnsHostnames: true
      EnableDnsSupport: true
      InstanceTenancy: default
(Kürzung)
Sie können den Stack auch mit cdk synth in yaml speichern.
cdk synth > cdk.yaml
# cdk ls
testcdk
# cdk deploy testcdk
testcdk: deploying...
testcdk creating CloudFormation changeset
Cloudformation wird in der angegebenen AWS-Kontoregion ausgeführt und VPC und Subnetz, Sicherheitsgruppe und EC2 werden erstellt.
Getting Started With the AWS CDK
Recommended Posts