-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
105 lines (93 loc) · 2.15 KB
/
main.tf
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
provider "aws" {
version = "~> 2.32"
region = var.region
}
resource "aws_vpc" "main" {
cidr_block = var.subnet
tags = {
Name = var.project_name
}
enable_dns_hostnames = "true"
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet
availability_zone = "${var.region}${var.zone}"
tags = {
Name = "Main subnet (${var.project_name})"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${var.project_name}-gateway"
}
}
resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "${var.project_name}-route-table"
}
}
resource "aws_route_table_association" "default" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.main.id
}
resource "aws_security_group" "ec2_instance" {
name = "${var.project_name}-ec2-instance"
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.ssh_access_ip_range_of_clients}"]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.project_name}-instance-sg"
}
}
resource "aws_key_pair" "deployer" {
key_name = "${var.project_name}-key"
public_key = var.ssh_public_key
}
data "aws_ami" "latest_ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "aws_instance" "server" {
ami = data.aws_ami.latest_ubuntu.id
instance_type = var.instance_type
key_name = aws_key_pair.deployer.key_name
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.ec2_instance.id]
associate_public_ip_address = true
root_block_device {
delete_on_termination = true
volume_type = "gp2"
volume_size = 30
}
tags = {
Name = "${var.project_name}-server"
}
}
output "instance_ip_addr" {
value = aws_instance.server.public_ip
}