Skip to content

Commit d1e1850

Browse files
huangzbawsNGL321
authored andcommitted
feat: add VPC, EC2 Python Example (#204)
This pull request is aimed at creating the following Python Examples: - Import VPC - Create new VPC with 2 AZs and 6 Subnets, Public, Private and DataBase. - Sharing VPC between two stacks - Create ALB/EC2/AutoscalingGroup in the VPC - How to define EC2 to specify AMI or auto-selection - Define property override for EC2 Instance with BlockStorageMapping - Define userdata for EC2 to setup httpd - Create NAT GW and Bastion - Chain the Security Groups
1 parent 4ab372e commit d1e1850

File tree

18 files changed

+409
-0
lines changed

18 files changed

+409
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Create EC2 in an existing VPC with AWS CDK Python
2+
3+
This is a project to create a new EC2 in an existing VPC on AWS with the AWS Cloud Development Kit.
4+
5+
This project also demonstrates:
6+
* Using customized user data of EC2
7+
* Customize multiple EBS volume
8+
* Specify AMI id
9+
* Security groups allow SSH access from internet
10+
11+
## Useful commands
12+
13+
* `cdk ls` list all stacks in the app
14+
* `cdk synth` emits the synthesized CloudFormation template
15+
* `cdk deploy` deploy this stack to your default AWS account/region
16+
* `cdk diff` compare deployed stack with current state
17+
* `cdk docs` open CDK documentation
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
3+
from aws_cdk import core
4+
5+
from cdk_vpc_ec2.cdk_vpc_ec2_stack import CdkVpcEc2Stack
6+
7+
# Define your account id to make import vpc work
8+
env_cn = core.Environment(account="YOUR_ACCOUNT_ID_WITHOUT_HYPHEN", region="cn-northwest-1")
9+
10+
app = core.App()
11+
CdkVpcEc2Stack(app, "cdk-vpc-ec2", env=env_cn)
12+
13+
app.synth()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "python3 app.py"
3+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from aws_cdk import core
2+
import aws_cdk.aws_ec2 as ec2
3+
4+
vpc_id = "MY-VPC-ID" # Import an Exist VPC
5+
ec2_type = "t2.micro"
6+
key_name = "id_rsa"
7+
linux_ami = ec2.GenericLinuxImage({
8+
"cn-northwest-1": "AMI-ID-IN-cn-northwest-1-REGION", # Refer to an Exist AMI
9+
"eu-west-1": "AMI-ID-IN-eu-west-1-REGION"
10+
})
11+
with open("./user_data/user_data.sh") as f:
12+
user_data = f.read()
13+
14+
15+
class CdkVpcEc2Stack(core.Stack):
16+
17+
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
18+
super().__init__(scope, id, **kwargs)
19+
20+
# The code that defines your stack goes here
21+
vpc = ec2.Vpc.from_lookup(self, "VPC", vpc_id=vpc_id)
22+
23+
host = ec2.Instance(self, "myEC2",
24+
instance_type=ec2.InstanceType(
25+
instance_type_identifier=ec2_type),
26+
instance_name="mySingleHost",
27+
machine_image=linux_ami,
28+
vpc=vpc,
29+
key_name=key_name,
30+
vpc_subnets=ec2.SubnetSelection(
31+
subnet_type=ec2.SubnetType.PUBLIC),
32+
user_data=ec2.UserData.custom(user_data)
33+
)
34+
# ec2.Instance has no property of BlockDeviceMappings, add via lower layer cdk api:
35+
host.instance.add_property_override("BlockDeviceMappings", [{
36+
"DeviceName": "/dev/xvda",
37+
"Ebs": {
38+
"VolumeSize": "10",
39+
"VolumeType": "io1",
40+
"Iops": "150",
41+
"DeleteOnTermination": "true"
42+
}
43+
}, {
44+
"DeviceName": "/dev/sdb",
45+
"Ebs": {"VolumeSize": "30"}
46+
}
47+
]) # by default VolumeType is gp2, VolumeSize 8GB
48+
host.connections.allow_from_any_ipv4(
49+
ec2.Port.tcp(22), "Allow ssh from internet")
50+
host.connections.allow_from_any_ipv4(
51+
ec2.Port.tcp(80), "Allow ssh from internet")
52+
53+
core.CfnOutput(self, "Output",
54+
value=host.instance_public_ip)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-e .
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import setuptools
2+
3+
4+
with open("README.md") as fp:
5+
long_description = fp.read()
6+
7+
8+
setuptools.setup(
9+
name="Import_VPC_Create_EC2",
10+
version="1.0.0",
11+
12+
description="Import VPC and Create EC2 on it with two EBS and EC2 UserData",
13+
long_description=long_description,
14+
long_description_content_type="text/markdown",
15+
16+
author="Huang, Zhuobin (James)",
17+
18+
package_dir={"": "cdk_vpc_ec2"},
19+
packages=setuptools.find_packages(where="cdk_vpc_ec2"),
20+
21+
install_requires=[
22+
"aws-cdk.core",
23+
"aws-cdk.aws-ec2"
24+
],
25+
26+
python_requires=">=3.6",
27+
28+
classifiers=[
29+
"Development Status :: 4 - Beta",
30+
31+
"Intended Audience :: Developers",
32+
33+
"License :: OSI Approved :: Apache Software License",
34+
35+
"Programming Language :: JavaScript",
36+
"Programming Language :: Python :: 3 :: Only",
37+
"Programming Language :: Python :: 3.6",
38+
"Programming Language :: Python :: 3.7",
39+
"Programming Language :: Python :: 3.8",
40+
41+
"Topic :: Software Development :: Code Generators",
42+
"Topic :: Utilities",
43+
44+
"Typing :: Typed",
45+
],
46+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
sudo yum update -y
3+
sudo yum -y install httpd php
4+
sudo chkconfig httpd on
5+
sudo service httpd start
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Create VPC, EC2 ASG and RDS with AWS CDK Python
2+
3+
This is a project to create a new VPC, EC2 autoscaling group and RDS on AWS with the AWS Cloud Development Kit.
4+
5+
This project also demonstrates:
6+
* Create VPC in 3 tier layers of subnets: PUBLIC, PRIVATE and ISOLATED, you can specify the number of AZ and the CIDR.
7+
* Create Bastion instance, NAT Gateway and S3 endpoint
8+
* Create ALB, EC2 Autoscaling group with scaling policy and customize EBS volume
9+
* Creat RDS MySQL M-AZs Database or Aurora
10+
* Create security group and allow access from the other security group: Internet -> ALB -> EC2ASG -> RDS
11+
* Using customized user data of EC2 and specify generation AMI property and do not need to specify the AMI id in every region
12+
13+
## Architeture
14+
![Architecture](./img_demo_cdk_vpc.png)
15+
16+
This project create the new VPC part of the architeture. For the existing VPC part, please refer to the project in aws-cdk-examples/existing-vpc-new-ec2-ebs-userdata
17+
18+
## Useful commands
19+
20+
* `cdk ls` list all stacks in the app
21+
* `cdk synth` emits the synthesized CloudFormation template
22+
* `cdk deploy` deploy this stack to your default AWS account/region
23+
* `cdk diff` compare deployed stack with current state
24+
* `cdk docs` open CDK documentation
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
3+
from aws_cdk import core
4+
5+
from cdk_vpc_ec2.cdk_vpc_stack import CdkVpcStack
6+
from cdk_vpc_ec2.cdk_ec2_stack import CdkEc2Stack
7+
from cdk_vpc_ec2.cdk_rds_stack import CdkRdsStack
8+
9+
app = core.App()
10+
11+
vpc_stack = CdkVpcStack(app, "cdk-vpc")
12+
ec2_stack = CdkEc2Stack(app, "cdk-ec2",
13+
vpc=vpc_stack.vpc)
14+
rds_stack = CdkRdsStack(app, "cdk-rds",
15+
vpc=vpc_stack.vpc,
16+
asg_security_groups=ec2_stack.asg.connections.security_groups)
17+
18+
app.synth()

0 commit comments

Comments
 (0)