Skip to content

Commit 6045ec7

Browse files
committed
feat: EC2 instances in separate availability zones
1 parent e032ab0 commit 6045ec7

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,23 @@ You are expected to provide step-by-step instructions for creating and configuri
2828
Then the RDS database supporting multi availability zones is created in a private subnet to only allow data access from the EC2 instances within the security group.
2929
Http requests a forwarded from the Internet Gateway to the EC2 instances which can access the data from the RDS database and send the response via Internet Gateway to the client.
3030

31-
<br />The manual steps to achieve the same result using the AWS management console are documented [here](https://github.com/huyphamch/terraform-aws-create-web-rds/blob/master/manual/Project1.pdf)
31+
<br />The manual steps to achieve the same result using the AWS management console are documented [here](https://github.com/huyphamch/terraform-aws-create-web-rds/blob/master/manual/Project1.pdf)
32+
33+
## Prerequisites
34+
<br /> You have access to AWS services, for example via Free tier AWS account.
35+
<br /> AWS CLI and Terraform are already installed.
36+
37+
## Usage
38+
<br /> 1. Open terminal
39+
<br /> 2. Before you can execute the terraform script, your need to configure your aws environment first.
40+
<br /> aws configure
41+
<br /> AWS Access Key ID: See IAM > Security credentials > Access keys > Create access key
42+
<br /> AWS Secret Access Key: See IAM > Security credentials > Access keys > Create access key
43+
<br /> Default region name: us-east-1
44+
<br /> Default output format: json
45+
<br /> 3. Now you can apply the terraform changes.
46+
<br /> terraform init
47+
<br /> terraform apply --auto-approve
48+
<br /> Result: Calling the URL from the web browser should display the static web page
49+
<br /> 4. At the end you can cleanup the created AWS resources.
50+
<br /> terraform destroy --auto-approve

main.tf

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ resource "aws_vpc" "vpc-load-balancer" {
1818
resource "aws_subnet" "subnet-public" {
1919
vpc_id = aws_vpc.vpc-load-balancer.id
2020
availability_zone = "us-east-1a"
21-
cidr_block = "10.0.0.0/27"
21+
cidr_block = "10.0.0.0/28"
2222
map_public_ip_on_launch = true
2323
tags = {
2424
Name = "subnet-public"
2525
}
2626
}
2727

28+
resource "aws_subnet" "subnet-public2" {
29+
vpc_id = aws_vpc.vpc-load-balancer.id
30+
availability_zone = "us-east-1b"
31+
cidr_block = "10.0.0.16/28"
32+
map_public_ip_on_launch = true
33+
tags = {
34+
Name = "subnet-public2"
35+
}
36+
}
37+
2838
resource "aws_subnet" "subnet-private" {
2939
vpc_id = aws_vpc.vpc-load-balancer.id
3040
availability_zone = "us-east-1a"
@@ -95,6 +105,17 @@ resource "aws_route_table" "rt-public"{
95105
}
96106
}
97107

108+
resource "aws_route_table" "rt-public2"{
109+
vpc_id = aws_vpc.vpc-load-balancer.id
110+
route {
111+
cidr_block = "0.0.0.0/0"
112+
gateway_id = aws_internet_gateway.igw-web.id
113+
}
114+
tags = {
115+
Name = "rt-public2"
116+
}
117+
}
118+
98119
resource "aws_route_table" "rt-private"{
99120
vpc_id = aws_vpc.vpc-load-balancer.id
100121
route {
@@ -123,15 +144,25 @@ resource "aws_route_table_association" "rta-public"{
123144
route_table_id = aws_route_table.rt-public.id
124145
}
125146

147+
resource "aws_route_table_association" "rta-publi2"{
148+
subnet_id = aws_subnet.subnet-public2.id
149+
route_table_id = aws_route_table.rt-public2.id
150+
}
151+
126152
resource "aws_route_table_association" "rta-private"{
127153
subnet_id = aws_subnet.subnet-private.id
128154
route_table_id = aws_route_table.rt-private.id
129155
}
130156

157+
resource "aws_route_table_association" "rta-private2"{
158+
subnet_id = aws_subnet.subnet-private2.id
159+
route_table_id = aws_route_table.rt-private2.id
160+
}
161+
131162
# 8. Create security group to allow port: Http, Https, SSH, RDP
132163
resource "aws_security_group" "security-group-web" {
133164
name = "Allow_inbound_traffic"
134-
description = "Allow https, http, ssh inbound traffic"
165+
description = "Allow https, http, ssh and rdp inbound traffic"
135166
vpc_id = aws_vpc.vpc-load-balancer.id
136167

137168
ingress {
@@ -158,6 +189,14 @@ resource "aws_security_group" "security-group-web" {
158189
cidr_blocks = ["0.0.0.0/0"]
159190
}
160191

192+
ingress {
193+
description = "RDP"
194+
from_port = 3389
195+
to_port = 3389
196+
protocol = "tcp"
197+
cidr_blocks = ["0.0.0.0/0"]
198+
}
199+
161200
egress {
162201
from_port = 0
163202
to_port = 0
@@ -200,7 +239,6 @@ resource "aws_instance" "web-linux" {
200239
ami = "ami-03a6eaae9938c858c"
201240
instance_type = "t2.micro"
202241
key_name = aws_key_pair.key_pair.key_name
203-
availability_zone = "us-east-1a"
204242
subnet_id = aws_subnet.subnet-public.id
205243
vpc_security_group_ids = [ aws_security_group.security-group-web.id ]
206244

@@ -211,6 +249,7 @@ sudo yum install -y httpd.x86_64
211249
sudo yum install git -y
212250
sudo systemctl start httpd.service
213251
sudo systemctl enable httpd.service
252+
echo "Hello World from $(hostname -f) running on $(uname -r)" > /var/www/html/index.html
214253
EOF
215254

216255
tags = {
@@ -223,14 +262,15 @@ resource "aws_instance" "web-windows" {
223262
ami = "ami-0be0e902919675894"
224263
instance_type = "t2.micro"
225264
key_name = aws_key_pair.key_pair.key_name
226-
availability_zone = "us-east-1a"
227-
subnet_id = aws_subnet.subnet-public.id
265+
subnet_id = aws_subnet.subnet-public2.id
228266
vpc_security_group_ids = [ aws_security_group.security-group-web.id ]
229267

230268
user_data = <<-EOF
231269
<powershell>
232270
Install-WindowsFeature -name Web-Server -IncludeManagementTools
233-
New-Item -Path C:\inetpub\wwwroot\index.html -ItemType File -Value "Hello World Page" -Force
271+
$os = systeminfo | findstr /B /C:"OS Name“
272+
$ipAddress = Resolve-DnsName $env:COMPUTERNAME -Type A | Select -Property IPAddress
273+
New-Item -Path C:\inetpub\wwwroot\index.html -ItemType File -Value "Hello World from $ipAddress running on $os" -Force
234274
</powershell>
235275
EOF
236276

@@ -284,4 +324,4 @@ resource "aws_db_instance" "db-mysql" {
284324
multi_az = true
285325
skip_final_snapshot = true
286326
publicly_accessible = true
287-
}
327+
}

0 commit comments

Comments
 (0)