From f44de1d2aa9862a2b25adf47920e8736b376a4b0 Mon Sep 17 00:00:00 2001 From: Henrique Date: Fri, 2 May 2025 16:32:36 -0300 Subject: [PATCH 1/5] ec2.tf file --- ec2.tf | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ec2.tf diff --git a/ec2.tf b/ec2.tf new file mode 100644 index 0000000..0b9861d --- /dev/null +++ b/ec2.tf @@ -0,0 +1,64 @@ +resource "aws_vpc" "my_vpc" { + cidr_block = "172.16.0.0/16" + + tags = { + Name = "tf-example" + } +} + +resource "aws_subnet" "my_subnet" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "172.16.10.0/24" + availability_zone = "eu-west-2" + + tags = { + Name = "tf-example" + } +} + +resource "aws_internet_gateway" "gw" { + vpc_id = aws_vpc.main.id +} + +resource "aws_route_table" "public" { + vpc_id = aws_vpc.main.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.gw.id + } + + tags = { + Name = "public-rt" + } +} + +resource "aws_route_table_association" "public_assoc" { + subnet_id = aws_subnet.public.id + route_table_id = aws_route_table.public.id +} + +resource "aws_instance" "foo" { + ami = "ami-005e54dee72cc1d00" # us-west-2 + instance_type = "t2.micro" + associate_public_ip_address = true + subnet_id = aws_subnet.my_subnet.id + + user_data = << EOF + + #!/bin/bash + + yum update –y + amazon-linux-extras install nginx1.12 + nginx -v + systemctl start nginx + systemctl enable nginx + chmod 2775 /usr/share/nginx/html + find /usr/share/nginx/html -type d -exec chmod 2775 {} \; + find /usr/share/nginx/html -type f -exec chmod 0664 {} \; + echo "

Welcome to my NGINX web server! User data instillation was a SUCCESS!

" > /usr/share/nginx/html/index.html + + EOF + + +} \ No newline at end of file From e8369dd5c4e013eecee7a34cda0f40cc4deb8c21 Mon Sep 17 00:00:00 2001 From: Henrique Date: Fri, 2 May 2025 16:37:48 -0300 Subject: [PATCH 2/5] correcting user data syntax --- ec2.tf | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ec2.tf b/ec2.tf index 0b9861d..bb45162 100644 --- a/ec2.tf +++ b/ec2.tf @@ -39,12 +39,12 @@ resource "aws_route_table_association" "public_assoc" { } resource "aws_instance" "foo" { - ami = "ami-005e54dee72cc1d00" # us-west-2 - instance_type = "t2.micro" + ami = "ami-005e54dee72cc1d00" # us-west-2 + instance_type = "t2.micro" associate_public_ip_address = true - subnet_id = aws_subnet.my_subnet.id + subnet_id = aws_subnet.my_subnet.id - user_data = << EOF + user_data = < Date: Fri, 2 May 2025 16:39:39 -0300 Subject: [PATCH 3/5] correcting vpc name~ --- ec2.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ec2.tf b/ec2.tf index bb45162..1cceb5c 100644 --- a/ec2.tf +++ b/ec2.tf @@ -17,11 +17,11 @@ resource "aws_subnet" "my_subnet" { } resource "aws_internet_gateway" "gw" { - vpc_id = aws_vpc.main.id + vpc_id = aws_vpc.my_vpc.id } resource "aws_route_table" "public" { - vpc_id = aws_vpc.main.id + vpc_id = aws_vpc.my_vpc.id route { cidr_block = "0.0.0.0/0" From f12a101c240da5b57d2b5d32c2b6cb4ca544d9aa Mon Sep 17 00:00:00 2001 From: Henrique Date: Fri, 2 May 2025 16:41:35 -0300 Subject: [PATCH 4/5] validate correction --- .terraform.lock.hcl | 25 +++++++++++++++++++++++++ ec2.tf | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .terraform.lock.hcl diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..02d6424 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.97.0" + constraints = "~> 5.0" + hashes = [ + "h1:lI0I9GziJsdymNBcj+MJloqwD8fbogJw3EiR60j5FYU=", + "zh:02790ad98b767d8f24d28e8be623f348bcb45590205708334d52de2fb14f5a95", + "zh:088b4398a161e45762dc28784fcc41c4fa95bd6549cb708b82de577f2d39ffc7", + "zh:0c381a457b7af391c43fc0167919443f6105ad2702bde4d02ddea9fd7c9d3539", + "zh:1a4b57a5043dcca64d8b8bae8b30ef4f6b98ed2144f792f39c4e816d3f1e2c56", + "zh:1bf00a67f39e67664337bde065180d41d952242801ebcd1c777061d4ffaa1cc1", + "zh:24c549f53d6bd022af31426d3e78f21264d8a72409821669e7fd41966ae68b2b", + "zh:3abda50bbddb35d86081fe39522e995280aea7f004582c4af22112c03ac8b375", + "zh:7388ed7f21ce2eb46bd9066626ce5f3e2a5705f67f643acce8ae71972f66eaf6", + "zh:96740f2ff94e5df2b2d29a5035a1a1026fe821f61712b2099b224fb2c2277663", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:9f399f8e8683a3a3a6d63a41c7c3a5a5f266eedef40ea69eba75bacf03699879", + "zh:bcf2b288d4706ebd198f75d2159663d657535483331107f2cdef381f10688baf", + "zh:cc76c8a9fc3bad05a8779c1f80fe8c388734f1ec1dd0affa863343490527b466", + "zh:de4359cf1b057bfe7a563be93829ec64bf72e7a2b85a72d075238081ef5eb1db", + "zh:e208fa77051a1f9fa1eff6c5c58aabdcab0de1695b97cdea7b8dd81df3e0ed73", + ] +} diff --git a/ec2.tf b/ec2.tf index 1cceb5c..10f59c1 100644 --- a/ec2.tf +++ b/ec2.tf @@ -34,7 +34,7 @@ resource "aws_route_table" "public" { } resource "aws_route_table_association" "public_assoc" { - subnet_id = aws_subnet.public.id + subnet_id = aws_subnet.my_subnet.id route_table_id = aws_route_table.public.id } From 8635bf1f3832ac93fea6b9ef106e6a4be321afef Mon Sep 17 00:00:00 2001 From: Henrique Date: Fri, 2 May 2025 16:43:30 -0300 Subject: [PATCH 5/5] subnet eu-west-2a --- ec2.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2.tf b/ec2.tf index 10f59c1..1d6e913 100644 --- a/ec2.tf +++ b/ec2.tf @@ -9,7 +9,7 @@ resource "aws_vpc" "my_vpc" { resource "aws_subnet" "my_subnet" { vpc_id = aws_vpc.my_vpc.id cidr_block = "172.16.10.0/24" - availability_zone = "eu-west-2" + availability_zone = "eu-west-2a" tags = { Name = "tf-example"