Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions backend.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 5 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
locals {
tags = {
Name = "thiagomotta"
}
}
87 changes: 76 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions nginx.sh
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Configure the AWS Provider
provider "aws" {
region = "eu-west-2"
}
Loading