Skip to content

Commit 4f84937

Browse files
committed
add tf with secrets
1 parent 9fea695 commit 4f84937

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

terraform/secrets-and-yor-ec2.tf

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
resource "aws_instance" "web_host" {
2+
# ec2 have plain text secrets in user data
3+
ami = "ami-091e1eed890c3f1d1"
4+
instance_type = "t2.nano"
5+
6+
vpc_security_group_ids = [
7+
"${aws_security_group.web-node.id}"]
8+
subnet_id = "${aws_subnet.web_subnet.id}"
9+
user_data = <<EOF
10+
#! /bin/bash
11+
sudo apt-get update
12+
sudo apt-get install -y apache2
13+
sudo systemctl start apache2
14+
sudo systemctl enable apache2
15+
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMAAA
16+
export AWS_SECRET_ACCESS_KEY=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
17+
export AWS_DEFAULT_REGION=us-west-2
18+
echo "<h1>Deployed via Terraform</h1>" | sudo tee /var/www/html/index.html
19+
EOF
20+
tags = merge({
21+
Name = "${local.resource_prefix.value}-ec2"
22+
}, {
23+
yor_trace = "347af3cd-4f70-4632-aca3-4d5e30ffc0b1"
24+
})
25+
}
26+
27+
resource "aws_ebs_volume" "web_host_storage" {
28+
# unencrypted volume
29+
availability_zone = "${var.aws_region}a"
30+
#encrypted = false # Setting this causes the volume to be recreated on apply
31+
size = 1
32+
tags = merge({
33+
Name = "${local.resource_prefix.value}-ebs"
34+
}, {
35+
yor_trace = "c5509daf-10f0-46af-9e03-419892125211"
36+
})
37+
}
38+
39+
resource "aws_ebs_snapshot" "example_snapshot" {
40+
# ebs snapshot without encryption
41+
volume_id = "${aws_ebs_volume.web_host_storage.id}"
42+
description = "${local.resource_prefix.value}-ebs-snapshot"
43+
tags = merge({
44+
Name = "${local.resource_prefix.value}-ebs-snapshot"
45+
}, {
46+
47+
yor_trace = "c1008080-ec2f-4512-a0d0-2e9330aa58f1"
48+
})
49+
}
50+
51+
resource "aws_volume_attachment" "ebs_att" {
52+
device_name = "/dev/sdh"
53+
volume_id = "${aws_ebs_volume.web_host_storage.id}"
54+
instance_id = "${aws_instance.web_host.id}"
55+
}
56+
57+
resource "aws_security_group" "web-node" {
58+
# security group is open to the world in SSH port
59+
name = "${local.resource_prefix.value}-sg"
60+
description = "${local.resource_prefix.value} Security Group"
61+
vpc_id = aws_vpc.web_vpc.id
62+
63+
ingress {
64+
from_port = 80
65+
to_port = 80
66+
protocol = "tcp"
67+
cidr_blocks = [
68+
"0.0.0.0/0"]
69+
}
70+
ingress {
71+
from_port = 22
72+
to_port = 22
73+
protocol = "tcp"
74+
cidr_blocks = [
75+
"0.0.0.0/0"]
76+
}
77+
egress {
78+
from_port = 0
79+
to_port = 0
80+
protocol = "-1"
81+
cidr_blocks = [
82+
"0.0.0.0/0"]
83+
}
84+
depends_on = [aws_vpc.web_vpc]
85+
tags = {
86+
yor_trace = "b7af1b40-64eb-4519-a1a0-ab198db4b191"
87+
}
88+
}
89+
90+
resource "aws_vpc" "web_vpc" {
91+
cidr_block = "172.16.0.0/16"
92+
enable_dns_hostnames = true
93+
enable_dns_support = true
94+
tags = merge({
95+
Name = "${local.resource_prefix.value}-vpc"
96+
}, {
97+
yor_trace = "9bf2359b-952e-4570-9595-52eba4c20471"
98+
})
99+
}
100+
101+
resource "aws_subnet" "web_subnet" {
102+
vpc_id = aws_vpc.web_vpc.id
103+
cidr_block = "172.16.10.0/24"
104+
availability_zone = "${var.aws_region}a"
105+
map_public_ip_on_launch = true
106+
107+
tags = merge({
108+
Name = "${local.resource_prefix.value}-subnet"
109+
}, {
110+
yor_trace = "0345f650-d280-4ca8-86c9-c71c38c0eda1"
111+
})
112+
}
113+
114+
resource "aws_subnet" "web_subnet2" {
115+
vpc_id = aws_vpc.web_vpc.id
116+
cidr_block = "172.16.11.0/24"
117+
availability_zone = "${var.aws_region}b"
118+
map_public_ip_on_launch = true
119+
120+
tags = merge({
121+
Name = "${local.resource_prefix.value}-subnet2"
122+
}, {
123+
yor_trace = "224af03a-00e0-4981-be30-14965833c2d1"
124+
})
125+
}
126+
127+
128+
resource "aws_internet_gateway" "web_igw" {
129+
vpc_id = aws_vpc.web_vpc.id
130+
131+
tags = merge({
132+
Name = "${local.resource_prefix.value}-igw"
133+
}, {
134+
yor_trace = "d8e63cb4-2fb5-4726-9c86-5fd05ef03671"
135+
})
136+
}
137+
138+
resource "aws_route_table" "web_rtb" {
139+
vpc_id = aws_vpc.web_vpc.id
140+
141+
tags = merge({
142+
Name = "${local.resource_prefix.value}-rtb"
143+
}, {
144+
yor_trace = "5e4fee6e-a6aa-4b61-a741-47c5efb463e2"
145+
})
146+
}
147+
148+
resource "aws_route_table_association" "rtbassoc" {
149+
subnet_id = aws_subnet.web_subnet.id
150+
route_table_id = aws_route_table.web_rtb.id
151+
}
152+
153+
resource "aws_route_table_association" "rtbassoc2" {
154+
subnet_id = aws_subnet.web_subnet2.id
155+
route_table_id = aws_route_table.web_rtb.id
156+
}
157+
158+
resource "aws_route" "public_internet_gateway" {
159+
route_table_id = aws_route_table.web_rtb.id
160+
destination_cidr_block = "0.0.0.0/0"
161+
gateway_id = aws_internet_gateway.web_igw.id
162+
163+
timeouts {
164+
create = "5m"
165+
}
166+
}
167+
168+
169+
resource "aws_network_interface" "web-eni" {
170+
subnet_id = aws_subnet.web_subnet.id
171+
private_ips = ["172.16.10.100"]
172+
173+
tags = merge({
174+
Name = "${local.resource_prefix.value}-primary_network_interface"
175+
}, {
176+
yor_trace = "7e2ffea8-739f-467d-b57b-53cbc0d7ccb1"
177+
})
178+
}
179+
180+
# VPC Flow Logs to S3
181+
resource "aws_flow_log" "vpcflowlogs" {
182+
log_destination = aws_s3_bucket.flowbucket.arn
183+
log_destination_type = "s3"
184+
traffic_type = "ALL"
185+
vpc_id = aws_vpc.web_vpc.id
186+
187+
tags = merge({
188+
Name = "${local.resource_prefix.value}-flowlogs"
189+
Environment = local.resource_prefix.value
190+
}, {
191+
yor_trace = "6808d4b7-45bc-4d1d-9523-96757a3add31"
192+
})
193+
}
194+
195+
resource "aws_s3_bucket" "flowbucket" {
196+
bucket = "${local.resource_prefix.value}-flowlogs"
197+
force_destroy = true
198+
199+
tags = merge({
200+
Name = "${local.resource_prefix.value}-flowlogs"
201+
Environment = local.resource_prefix.value
202+
}, {
203+
yor_trace = "f058838a-b1e0-4383-b965-7e06e987ffb2"
204+
})
205+
}
206+
207+
output "ec2_public_dns" {
208+
description = "Web Host Public DNS name"
209+
value = aws_instance.web_host.public_dns
210+
}
211+
212+
output "vpc_id" {
213+
description = "The ID of the VPC"
214+
value = aws_vpc.web_vpc.id
215+
}
216+
217+
output "public_subnet" {
218+
description = "The ID of the Public subnet"
219+
value = aws_subnet.web_subnet.id
220+
}
221+
222+
output "public_subnet2" {
223+
description = "The ID of the Public subnet"
224+
value = aws_subnet.web_subnet2.id
225+
}

0 commit comments

Comments
 (0)