If you want to build AWS infrastructure and EC2 as a set quickly and easily, AWS CDK is a very useful tool. This time, I will show you how to use AWS CDK Python.
--aws keypair: Created. Name it'testkey'.
--VPC is cut at / 21. 10.0.0.0/21 --Subnet: 1 Public (/24), 1 Private (/24) --SecurityGroup: Open port 22 inbound to use SSH for EC2
# 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.
(abridgement)
## 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)
Add libraries required for EC2 creation in setup.py
setup.py
(abridgement)
install_requires=[
"aws-cdk.core",
"aws_cdk.aws_ec2",
],
(abridgement)
# pip install -e .
(abridgement)
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()
Write a stack to create a VPC, SecurityGroup, EC2.
testcdk_stack.py
from aws_cdk import (
core,
aws_ec2 <=Add this
)
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
#Write the code below this
cidr = '10.0.0.0/21' #write cidr block
vpc = aws_ec2.Vpc(
self,
id='test-vpc',
cidr=cidr,
nat_gateways=1,
subnet_configuration=[
aws_ec2.SubnetConfiguration(
cidr_mask=24, #Define netmask for Public Subnet
name='public',
subnet_type=aws_ec2.SubnetType.PUBLIC,
),
aws_ec2.SubnetConfiguration(
cidr_mask=24, #Define netmask for Private Subnet
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), #Open Port 22 with Inbound
)
image_id = aws_ec2.AmazonLinuxImage(generation=aws_ec2.AmazonLinuxGeneration.AMAZON_LINUX_2).get_image(self).image_id #Specify EC2 image
aws_ec2.CfnInstance(
self,
id='testec2',
availability_zone="ap-northeast-1a", #Specify AZ
image_id=image_id,
instance_type="t3.micro", #Specify Instance Type
key_name='testkey', #Specify Key Pair
security_group_ids=[security_group.security_group_id],
subnet_id=vpc.private_subnets[0].subnet_id, #Specify Private Subnet this time
tags=[{
"key": "Name",
"value": "testec2" #Define the name to display in the web console
}]
)
# cdk synth
Resources:
testvpc8985080E:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/21
EnableDnsHostnames: true
EnableDnsSupport: true
InstanceTenancy: default
(abridgement)
You can also save the stack to yaml with cdk synth.
cdk synth > cdk.yaml
# cdk ls
testcdk
# cdk deploy testcdk
testcdk: deploying...
testcdk creating CloudFormation changeset
Cloudformation will run in the specified AWS account region, and VPC and Subnet, Security Group, and EC2 will be created.
Getting Started With the AWS CDK
Recommended Posts