-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
79 lines (69 loc) · 1.68 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
from ipaddress import ip_network
from ipify import get_ip
from troposphere import (
Base64,
ec2,
GetAtt,
Join,
Output,
Parameter,
Ref,
Template,
)
PublicCidr = str(ip_network(get_ip()))
port = "3000"
t = Template()
t.set_description("DevOps in AWS - HelloWorld application")
t.add_parameter(Parameter("KeyPair",
Description="herculano_devops",
Type="AWS::EC2::KeyPair::KeyName",
ConstraintDescription="herculano_devops",
))
t.add_resource(ec2.SecurityGroup(
"SecurityGroup",
GroupDescription="Allow SSH and TCP/ {} access".format(port),
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="22",
ToPort="22",
CidrIp=PublicCidr
),
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort=port,
ToPort=port,
CidrIp="0.0.0.0/0"
),
],
)
)
ud = Base64(Join('\n', [
"#!/bin/bash",
"yum install --enablerepo=epel -y nodejs",
"wget http://bit.ly/2vESNuc -O /home/ec2-user/helloworld.js",
"wget http://bit.ly/2vVvT18 -O /etc/init/helloworld.conf",
"start helloworld"
]))
t.add_resource(ec2.Instance(
"instance",
ImageId="ami-f09dcc9c",
InstanceType="t1.micro",
SecurityGroups=[Ref("SecurityGroup")],
KeyName=Ref("KeyPair"),
UserData=ud,
))
t.add_output(Output(
"Instance",
Description="Public Ip of our instance.",
Value=GetAtt("instance", "PublicIp"),
))
t.add_output(Output(
"WebUrl",
Description="Application endpoint",
Value=Join("", [
"http://", GetAtt("instance", "PublicDnsName"),
":", port]),
))
print(t.to_json())