1+ AWSTemplateFormatVersion : 2010-09-09
2+ Description : Creates EC2
3+ Parameters :
4+ VpcCIDR :
5+ Description : " VPC range"
6+ Type : String
7+ Default : " "
8+
9+ PublicSubnetCIDR :
10+ Description : " Public subnet CIDR block"
11+ Type : String
12+ Default : " "
13+
14+ InstanceType :
15+ Type : String
16+ Description : " EC2 instance type"
17+ AllowedValues :
18+ - t2.micro
19+ - t3.micro
20+ - t3.medium
21+
22+ KeyPairName :
23+ Description : " AWS key pair in us-east-1, stored in SSM Parameter Store"
24+ Type : String
25+ Default : " my-keypair"
26+
27+ AMItoUse :
28+ Description : AMI to use for our base image - Canonical, Ubuntu, 22.04 LTS, amd64 jammy image build on 2023-05-16
29+ Type : String
30+ Default : " ami-053b0d53c279acc90"
31+
32+ Resources :
33+ VPC :
34+ Type : AWS::EC2::VPC
35+ Properties :
36+ CidrBlock : !Ref VpcCIDR
37+ EnableDnsHostnames : true
38+ EnableDnsSupport : true
39+ Tags :
40+ - Key : Name
41+ Value : " Main VPC"
42+
43+ InternetGateway :
44+ Type : AWS::EC2::InternetGateway
45+ Properties :
46+ Tags :
47+ - Key : Name
48+ Value : Internet Gateway
49+
50+ AttachGateway :
51+ Type : AWS::EC2::VPCGatewayAttachment
52+ Properties :
53+ VpcId : !Ref VPC
54+ InternetGatewayId : !Ref InternetGateway
55+
56+ PublicSubnet :
57+ Type : AWS::EC2::Subnet
58+ Properties :
59+ AvailabilityZone : ' us-east-1a'
60+ VpcId : !Ref VPC
61+ CidrBlock : !Ref PublicSubnetCIDR
62+ Tags :
63+ - Key : Name
64+ Value : " Public Subnet AZ1"
65+
66+ PublicRouteTable :
67+ Type : AWS::EC2::RouteTable
68+ Properties :
69+ VpcId : !Ref VPC
70+ Tags :
71+ - Key : Name
72+ Value : " Public Route Table"
73+
74+ SubnetRouteTableAssociationPub :
75+ Type : AWS::EC2::SubnetRouteTableAssociation
76+ Properties :
77+ SubnetId : !Ref PublicSubnet
78+ RouteTableId : !Ref PublicRouteTable
79+
80+ PublicRouteNATGateway :
81+ Type : AWS::EC2::Route
82+ DependsOn : AttachGateway
83+ Properties :
84+ RouteTableId : !Ref PublicRouteTable
85+ DestinationCidrBlock : ' 0.0.0.0/0'
86+ GatewayId : !Ref InternetGateway
87+
88+ SecurityGroup :
89+ Type : AWS::EC2::SecurityGroup
90+ Properties :
91+ GroupName : mySecurityGroup
92+ GroupDescription : Allow http to client host
93+ VpcId : !Ref VPC
94+ SecurityGroupIngress :
95+ - IpProtocol : tcp
96+ FromPort : 80
97+ ToPort : 80
98+ CidrIp : 0.0.0.0/0
99+ - IpProtocol : tcp
100+ FromPort : 22
101+ ToPort : 22
102+ CidrIp : 0.0.0.0/0
103+ - IpProtocol : tcp
104+ FromPort : 5000
105+ ToPort : 5000
106+ CidrIp : 0.0.0.0/0
107+ - IpProtocol : tcp
108+ FromPort : 3000
109+ ToPort : 3000
110+ CidrIp : 0.0.0.0/0
111+ - IpProtocol : tcp
112+ FromPort : 5040
113+ ToPort : 5040
114+ CidrIp : 0.0.0.0/0
115+ SecurityGroupEgress :
116+ - IpProtocol : " -1"
117+ FromPort : -1
118+ ToPort : -1
119+ CidrIp : 0.0.0.0/0
120+ Tags :
121+ - Key : Name
122+ Value : SecurityGroup
123+
124+ # Server EC2 Instance
125+ ServerInstance :
126+ Type : AWS::EC2::Instance
127+ Properties :
128+ InstanceType : !Ref InstanceType
129+ ImageId : !Ref AMItoUse
130+ KeyName : !Ref KeyPairName
131+ NetworkInterfaces :
132+ - AssociatePublicIpAddress : true
133+ DeviceIndex : " 0"
134+ GroupSet :
135+ - !Ref SecurityGroup
136+ SubnetId : !Ref PublicSubnet
137+ BlockDeviceMappings :
138+ - DeviceName : " /dev/sdk"
139+ Ebs :
140+ VolumeSize : 20
141+ # UserData:
142+ # Fn::Base64: !Sub |
143+ # #!/bin/bash
144+ # sudo apt-get update -y
145+ # sudo apt-get install ca-certificates curl gnupg
146+ # sudo install -m 0755 -d /etc/apt/keyrings
147+ # curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
148+ # sudo chmod a+r /etc/apt/keyrings/docker.gpg
149+ # echo \
150+ # "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
151+ # "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
152+ # sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
153+ # sudo apt-get update -y
154+ # apt-get install -y docker-ce
155+ # docker run -p 80:8080 tomcat:8.0
156+ Tags :
157+ - Key : Name
158+ Value : ServerInstance
159+
160+ Outputs :
161+ EC2InstanceConnection :
162+ Description : The connection for the EC2 instance
163+ Value : !Join
164+ - " "
165+ - - "ubuntu@"
166+ - !GetAtt ServerInstance.PublicIp
167+ EC2InstancePublicDNS :
168+ Description : The instance public DNS name
169+ Value : !GetAtt ServerInstance.PublicDnsName
170+ Export :
171+ Name : !Join
172+ - " "
173+ - - !Ref AWS::StackName
174+ - " -PublicDNS"
0 commit comments