Skip to content

Commit 849a3cb

Browse files
committed
cdk example ec2 in vpc with ssm
1 parent d858266 commit 849a3cb

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ $ cdk destroy
9999
| [lambda-s3-trigger](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/lambda-s3-trigger/) | S3 trigger for Lambda |
100100
| [stepfunctions](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/stepfunctions/) | A simple StepFunctions workflow |
101101
| [url-shortner](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/url-shortener) | Demo from the [Infrastructure ***is*** Code with the AWS CDK](https://youtu.be/ZWCvNFUN-sU) AWS Online Tech Talk |
102+
| [ec2-instance](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/ec2/instance/) | Create EC2 Instance in new VPC with Systems Manager enabled |
103+
102104

103105
## JavaScript examples <a name="JavaScript"></a>
104106

python/ec2/instance/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# Create EC2 Instance in new VPC with Systems Manager enabled
3+
4+
This example includes:
5+
6+
* Own VPC with public subnet (following AWS Defaults for new accounts)
7+
* Based on latest Amazon Linux 2
8+
* System Manager replaces SSH (Remote session available trough the AWS Console or the AWS CLI.)
9+
* Userdata executed from script in S3 (`configuration.sh`).
10+
11+
## Useful commands
12+
13+
* `cdk bootstrap` initialice assets before deploy
14+
* `cdk synth` emits the synthesized CloudFormation template
15+
* `cdk deploy` deploy this stack to your default AWS account/region
16+
* `aws ssm start-session --target i-xxxxxxxxx` remote session for shell access

python/ec2/instance/app.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import os.path
2+
3+
from aws_cdk.aws_s3_assets import Asset
4+
5+
from aws_cdk import (
6+
aws_ec2 as ec2,
7+
aws_iam as iam,
8+
core
9+
)
10+
11+
dirname = os.path.dirname(__file__)
12+
13+
14+
class EC2InstanceStack(core.Stack):
15+
16+
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
17+
super().__init__(scope, id, **kwargs)
18+
19+
# VPC
20+
vpc = ec2.Vpc(self, "VPC",
21+
nat_gateways=0,
22+
subnet_configuration=[ec2.SubnetConfiguration(name="public",subnet_type=ec2.SubnetType.PUBLIC)]
23+
)
24+
25+
# AMI
26+
amzn_linux = ec2.MachineImage.latest_amazon_linux(
27+
generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
28+
edition=ec2.AmazonLinuxEdition.STANDARD,
29+
virtualization=ec2.AmazonLinuxVirt.HVM,
30+
storage=ec2.AmazonLinuxStorage.GENERAL_PURPOSE
31+
)
32+
33+
# Instance Role and SSM Managed Policy
34+
role = iam.Role(self, "InstanceSSM", assumed_by=iam.ServicePrincipal("ec2.amazonaws.com"))
35+
36+
role.add_managed_policy(iam.ManagedPolicy.from_aws_managed_policy_name("service-role/AmazonEC2RoleforSSM"))
37+
38+
# Instance
39+
instance = ec2.Instance(self, "Instance",
40+
instance_type=ec2.InstanceType("t3.nano"),
41+
machine_image=amzn_linux,
42+
vpc = vpc,
43+
role = role
44+
)
45+
46+
# Script in S3 as Asset
47+
asset = Asset(self, "Asset", path=os.path.join(dirname, "configure.sh"))
48+
local_path = instance.user_data.add_s3_download_command(
49+
bucket=asset.bucket,
50+
bucket_key=asset.s3_object_key
51+
)
52+
53+
# Userdata executes script from S3
54+
instance.user_data.add_execute_file_command(
55+
file_path=local_path
56+
)
57+
asset.grant_read(instance.role)
58+
59+
app = core.App()
60+
EC2InstanceStack(app, "ec2-instance")
61+
62+
app.synth()

python/ec2/instance/cdk.json

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+
}

python/ec2/instance/configure.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
# Use this to install software packages
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
aws-cdk.core
2+
aws-cdk.aws_ec2
3+
aws-cdk.aws_iam
4+
aws-cdk.aws_s3_assets

0 commit comments

Comments
 (0)