-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
108 lines (87 loc) · 2.29 KB
/
main.tf
File metadata and controls
108 lines (87 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
provider "aws" {
region = "eu-central-1"
}
locals {
common_tags = {
repository = "terraform-aws-nginx"
environment = "dev"
}
}
# VPC Configuration
resource "aws_vpc" "nginx_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = merge(local.common_tags, {
Name = "nginx-vpc"
})
}
resource "aws_subnet" "nginx_public_subnet_1a" {
vpc_id = aws_vpc.nginx_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "eu-central-1a"
map_public_ip_on_launch = true
tags = merge(local.common_tags, {
Name = "nginx-public-subnet-1a"
})
}
resource "aws_internet_gateway" "nginx_igw" {
vpc_id = aws_vpc.nginx_vpc.id
tags = merge(local.common_tags, {
Name = "nginx-igw"
})
}
resource "aws_route_table" "nginx_public_route_table" {
vpc_id = aws_vpc.nginx_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.nginx_igw.id
}
tags = merge(local.common_tags, {
Name = "nginx-public-route-table"
})
}
resource "aws_route_table_association" "nginx_public_rta" {
subnet_id = aws_subnet.nginx_public_subnet_1a.id
route_table_id = aws_route_table.nginx_public_route_table.id
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_security_group" "nginx_sg" {
vpc_id = aws_vpc.nginx_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(local.common_tags, {
Name = "nginx_sg"
})
}
resource "aws_instance" "nginx_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
subnet_id = aws_subnet.nginx_public_subnet_1a.id
vpc_security_group_ids = [aws_security_group.nginx_sg.id]
associate_public_ip_address = true
user_data = file("${path.module}/install_nginx.sh")
tags = merge(local.common_tags, {
Name = "nginx-server"
})
}
output "nginx_url" {
value = "http://${aws_instance.nginx_server.public_dns}"
}