From 90385888a59cf7f6bcef5aad7fd2b7b17864cd3d Mon Sep 17 00:00:00 2001 From: Thiago Motta Date: Thu, 1 May 2025 11:27:54 -0300 Subject: [PATCH 1/2] feat(TerraformConfig): Adds Terraform Configuration --- .terraform.lock.hcl | 24 +++++++++++++ backend.tf | 11 ++++++ locals.tf | 5 +++ main.tf | 87 +++++++++++++++++++++++++++++++++++++++------ nginx.sh | 8 +++++ provider.tf | 4 +++ 6 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 .terraform.lock.hcl create mode 100644 backend.tf create mode 100644 locals.tf create mode 100644 nginx.sh create mode 100644 provider.tf diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..42a9cf9 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.96.0" + hashes = [ + "h1:a/VEUu6BGQSPlUAzbN+zqaDCdi0QGh/VzBgo2gCran0=", + "zh:3f7e734abb9d647c851f5cb987837d7c073c9cbf1f520a031027d827f93d3b68", + "zh:5ca9400360a803a11cf432ca203be9f09da8fff9c96110a83c9029102b18c9d5", + "zh:5d421f475d467af182a527b7a61d50105dc63394316edf1c775ef736f84b941c", + "zh:68f2328e7f3e7666835d6815b39b46b08954a91204f82a6f648c928a0b09a744", + "zh:6a4170e7e2764df2968d1df65efebda55273dfc36dc6741207afb5e4b7e85448", + "zh:73f2a15bee21f7c92a071e2520216d0a40041aca52c0f6682e540da8ffcfada4", + "zh:9843d6973aedfd4cbaafd7110420d0c4c1d7ef4a2eeff508294c3adcc3613145", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:9d1abd6be717c42f2a6257ee227d3e9548c31f01c976ed7b32b2745a63659a67", + "zh:a70d642e323021d54a92f0daa81d096cb5067cb99ce116047a42eb1cb1d579a0", + "zh:b9a2b293208d5a0449275fae463319e0998c841e0bcd4014594a49ba54bb70d6", + "zh:ce0b0eb7ac24ff58c20efcb526c3f792a95be3617c795b45bbeea9f302903ae7", + "zh:dbbf98b3cd8003833c472bdb89321c17a9bbdc1b785e7e3d75f8af924ee5a0e4", + "zh:df86cf9311a4be8bb4a251196650653f97e01fbf5fe72deecc8f28a35a5352ae", + "zh:f92992881afd9339f3e539fcd90cfc1e9ed1356b5e760bbcc804314c3cd6837f", + ] +} diff --git a/backend.tf b/backend.tf new file mode 100644 index 0000000..111e41c --- /dev/null +++ b/backend.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } + backend "s3" { + # These key and value pairs are passed with -backend-config + } +} \ No newline at end of file diff --git a/locals.tf b/locals.tf new file mode 100644 index 0000000..9a1bc76 --- /dev/null +++ b/locals.tf @@ -0,0 +1,5 @@ +locals { + tags = { + Name = "thiagomotta" + } +} \ No newline at end of file diff --git a/main.tf b/main.tf index 059e758..155d0ec 100644 --- a/main.tf +++ b/main.tf @@ -1,16 +1,81 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 5.0" - } + +data "aws_ami" "ubuntu" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] } - backend "s3" { - # These key and value pairs are passed with -backend-config + + owners = ["099720109477"] +} + +resource "aws_vpc" "thiagomotta" { + cidr_block = "172.12.0.0/16" + + tags = local.tags +} + +resource "aws_internet_gateway" "gw" { + vpc_id = aws_vpc.thiagomotta.id + + tags = local.tags +} + +resource "aws_subnet" "thiagomotta" { + vpc_id = aws_vpc.thiagomotta.id + cidr_block = "172.12.0.0/24" + + tags = local.tags +} + +resource "aws_route_table" "thiagomotta" { + vpc_id = aws_vpc.thiagomotta.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.gw.id } } -# Configure the AWS Provider -provider "aws" { - region = "eu-west-2" +resource "aws_route_table_association" "thiagomotta" { + subnet_id = aws_subnet.thiagomotta.id + route_table_id = aws_route_table.thiagomotta.id +} + +resource "aws_security_group" "thiagomotta" { + name = "allow_tls" + vpc_id = aws_vpc.thiagomotta.id + + tags = local.tags } + +resource "aws_vpc_security_group_ingress_rule" "allow_tls_ipv4" { + security_group_id = aws_security_group.thiagomotta.id + cidr_ipv4 = "0.0.0.0/0" + from_port = 80 + ip_protocol = "tcp" + to_port = 80 +} + +resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv4" { + security_group_id = aws_security_group.thiagomotta.id + cidr_ipv4 = "0.0.0.0/0" + ip_protocol = "-1" +} + +resource "aws_instance" "thiagomotta" { + ami = data.aws_ami.ubuntu.id + subnet_id = aws_subnet.thiagomotta.id + instance_type = "t3.micro" + security_groups = [aws_security_group.thiagomotta.id] + associate_public_ip_address = true + user_data = file("${path.module}/nginx.sh") # check path + + tags = local.tags +} \ No newline at end of file diff --git a/nginx.sh b/nginx.sh new file mode 100644 index 0000000..6eb4f89 --- /dev/null +++ b/nginx.sh @@ -0,0 +1,8 @@ +#!/bin/bash +apt-get update +apt-get install nginx -y + +systemctl enable nginx +systemctl start nginx + +echo "Nginx installed and started" diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..e0fc2ce --- /dev/null +++ b/provider.tf @@ -0,0 +1,4 @@ +# Configure the AWS Provider +provider "aws" { + region = "eu-west-2" +} \ No newline at end of file From 22df2311d3a80edba40f7c3cca2adac00332cca2 Mon Sep 17 00:00:00 2001 From: Thiago Motta Date: Thu, 1 May 2025 11:29:07 -0300 Subject: [PATCH 2/2] feat(TerraformConfig): Terraform fmt --- main.tf | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.tf b/main.tf index 155d0ec..0ff7fac 100644 --- a/main.tf +++ b/main.tf @@ -38,7 +38,7 @@ resource "aws_route_table" "thiagomotta" { vpc_id = aws_vpc.thiagomotta.id route { - cidr_block = "0.0.0.0/0" + cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.gw.id } } @@ -70,12 +70,12 @@ resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv4" { } resource "aws_instance" "thiagomotta" { - ami = data.aws_ami.ubuntu.id - subnet_id = aws_subnet.thiagomotta.id - instance_type = "t3.micro" - security_groups = [aws_security_group.thiagomotta.id] + ami = data.aws_ami.ubuntu.id + subnet_id = aws_subnet.thiagomotta.id + instance_type = "t3.micro" + security_groups = [aws_security_group.thiagomotta.id] associate_public_ip_address = true - user_data = file("${path.module}/nginx.sh") # check path + user_data = file("${path.module}/nginx.sh") # check path tags = local.tags } \ No newline at end of file