-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
109 lines (95 loc) · 2.48 KB
/
main.tf
File metadata and controls
109 lines (95 loc) · 2.48 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
109
variable "vpc_cidr_block" {}
variable "subnet_cidr_block" {}
variable "avail_zone" {}
variable "env_prefix" {}
variable "instance_type" {}
variable "instance_name" {}
variable "ami-image" {}
resource "aws_vpc" "myapp-vpc" {
cidr_block = var.vpc_cidr_block
tags = {
Name: "${var.env_prefix}-vpc"
}
}
resource "aws_subnet" "myapp-subnet-1" {
vpc_id = aws_vpc.myapp-vpc.id
cidr_block = var.subnet_cidr_block[0]
availability_zone = var.avail_zone[0]
map_public_ip_on_launch = true
tags = {
Name: "${var.env_prefix}-public-subnet-1"
}
}
resource "aws_subnet" "myapp-subnet-2" {
vpc_id = aws_vpc.myapp-vpc.id
cidr_block = var.subnet_cidr_block[1]
availability_zone = var.avail_zone[1]
map_public_ip_on_launch = true
tags = {
Name: "${var.env_prefix}-public-subnet-2"
}
}
resource "aws_route_table" "myapp-public-rt" {
vpc_id = aws_vpc.myapp-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.myapp-igw.id
}
tags = {
Name: "${var.env_prefix}-public-rt"
}
}
resource "aws_internet_gateway" "myapp-igw" {
vpc_id = aws_vpc.myapp-vpc.id
tags = {
Name: "${var.env_prefix}-igw"
}
}
resource "aws_route_table_association" "myapp-rta-subnet-1" {
subnet_id = aws_subnet.myapp-subnet-1.id
route_table_id = aws_route_table.myapp-public-rt.id
}
resource "aws_route_table_association" "myapp-rta-subnet-2" {
subnet_id = aws_subnet.myapp-subnet-2.id
route_table_id = aws_route_table.myapp-public-rt.id
}
resource "aws_security_group" "myapp-sg" {
name = "myapp-sg"
description = "security group rules for myapp adv.devops project"
vpc_id = aws_vpc.myapp-vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
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 = {
Name: "${var.env_prefix}-sg"
}
}
resource "aws_instance" "myapp-server" {
#ami = "ami-0440d3b780d96b29d"
ami = var.ami-image.ubuntu
instance_type = var.instance_type
subnet_id = aws_subnet.myapp-subnet-1.id
vpc_security_group_ids = [ aws_security_group.myapp-sg.id ]
availability_zone = var.avail_zone[0]
associate_public_ip_address = true
key_name = "b80"
for_each = toset([ var.instance_name[0], var.instance_name[1], var.instance_name[2] ])
tags = {
Name: "${each.key}"
}
}