-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab267.yaml
181 lines (162 loc) · 4.23 KB
/
lab267.yaml
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for creating VPC, subnets, gateways, routes, and an EC2 instance with a key pair.
Parameters:
VpcCidr:
Type: String
Description: CIDR range for the VPC
Default: '10.0.0.0/16'
PublicSubnet1Cidr:
Type: String
Description: CIDR range for Public Subnet 1
Default: '10.0.0.0/24'
PrivateSubnet1Cidr:
Type: String
Description: CIDR range for Private Subnet 1
Default: '10.0.1.0/24'
PublicSubnet2Cidr:
Type: String
Description: CIDR range for Public Subnet 2
Default: '10.0.2.0/24'
PrivateSubnet2Cidr:
Type: String
Description: CIDR range for Private Subnet 2
Default: '10.0.3.0/24'
LinuxAmiId:
Type: String
Description: ID of the Linux AMI for EC2 instance
Default: 'ami-00970f57473724c10'
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
Tags:
- Key: Name
Value: "Jingru's VPC"
InternetGateway:
Type: AWS::EC2::InternetGateway
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: 'us-west-2a'
CidrBlock: !Ref PublicSubnet1Cidr
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: "Public Subnet 1"
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: 'us-west-2a'
CidrBlock: !Ref PrivateSubnet1Cidr
Tags:
- Key: Name
Value: "Private Subnet 1"
PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: 'us-west-2b'
CidrBlock: !Ref PublicSubnet2Cidr
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: "Public Subnet 2"
PrivateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: 'us-west-2b'
CidrBlock: !Ref PrivateSubnet2Cidr
Tags:
- Key: Name
Value: "Private Subnet 2"
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: "Public Route Table"
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: "Private Route Table"
PublicRoute:
Type: AWS::EC2::Route
DependsOn: VPCGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref InternetGateway
NATGatewayEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NATGateway:
Type: AWS::EC2::NatGateway
Properties:
SubnetId: !Ref PublicSubnet1
AllocationId: !GetAtt NATGatewayEIP.AllocationId
PrivateRoute1:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: '0.0.0.0/0'
NatGatewayId: !Ref NATGateway
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Enable SSH and HTTP access"
VpcId: !Ref VPC
SecurityGroupIngress:
- CidrIp: '0.0.0.0/0'
IpProtocol: tcp
FromPort: 22
ToPort: 22
- CidrIp: '0.0.0.0/0'
IpProtocol: tcp
FromPort: 80
ToPort: 80
EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: !Ref LinuxAmiId
SubnetId: !Ref PublicSubnet2
SecurityGroupIds:
- !Ref InstanceSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
sudo su
yum update -y
yum install -y httpd
echo "<p> My Instance! </p>" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
Outputs:
VpcId:
Value: !Ref VPC
PublicSubnet1Id:
Value: !Ref PublicSubnet1
PrivateSubnet1Id:
Value: !Ref PrivateSubnet1
PublicSubnet2Id:
Value: !Ref PublicSubnet2
PrivateSubnet2Id:
Value: !Ref PrivateSubnet2
InstanceId:
Value: !Ref EC2Instance
InstancePublicIp:
Value: !GetAtt EC2Instance.PublicIp