-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
171 lines (147 loc) · 4.25 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
locals {
project_name = "ec2-nginx-webservice"
name_context = "${local.project_name}-${terraform.workspace}"
name = "${local.project_name}-${terraform.workspace}"
global_tag = {
Deployment = "Deployment Account 01"
}
project_tag = {
Environment = terraform.workspace
Project = local.project_name
}
tags = merge(local.global_tag, local.project_tag)
region = "eu-central-1"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "${local.name_context}-vpc"
cidr = var.cidr
azs = ["eu-central-1a", "eu-central-1b", ]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", ]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = false
enable_dns_hostnames = true
tags = local.tags
}
################
##security group
################
module "security" {
source = "./modules/security"
# vpc_id = aws_vpc.main.id if used no module vpc
vpc_id = module.vpc.vpc_id
}
#########
#DATA AMI
#########
# TODO put a new Ami to it
data "aws_ami" "ec2_instance" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
# values = ["amzn-ami-hvm*"]
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
############
#AutoScaling
############
resource "aws_launch_configuration" "launch_conf" {
image_id = data.aws_ami.ec2_instance.id
instance_type = var.instance_type
security_groups = [module.security.EC2_sg]
user_data = filebase64("${path.module}/userdata.sh")
iam_instance_profile = aws_iam_instance_profile.dev-resources-iam-profile.name
root_block_device {
volume_type = var.volume_type
volume_size = 10
encrypted = var.encrypted_volume
}
ebs_block_device {
device_name = "/dev/sdf"
volume_type = var.volume_type
volume_size = 5
encrypted = var.encrypted_volume
}
}
resource "aws_autoscaling_group" "AS" {
name = "${local.project_name} - autoscalling"
vpc_zone_identifier = module.vpc.private_subnets
desired_capacity = var.desired_capacity
max_size = var.max_size
min_size = var.min_size
depends_on = [aws_lb.LoadBalancer]
target_group_arns = [aws_lb_target_group.TG.arn]
launch_configuration = aws_launch_configuration.launch_conf.name
}
#######################
#LoadBalancer
#######################
resource "aws_lb" "LoadBalancer" {
name = "${local.name_context}-LB"
internal = false
load_balancer_type = "application"
security_groups = [module.security.alb_sg]
subnets = ["${module.vpc.public_subnets[0]}",
"${module.vpc.public_subnets[1]}"]
}
resource "aws_lb_target_group" "TG" {
name = "${local.name_context}-tg"
port = var.lb_port
protocol = "HTTP"
vpc_id = module.vpc.vpc_id
health_check {
interval = 70
port = 80
path = "/index.html"
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 60
protocol = "HTTP"
matcher = "200,202"
}
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.LoadBalancer.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.TG.arn
}
}
#######################
#IAM & instance profile
#######################
resource "aws_iam_instance_profile" "dev-resources-iam-profile" {
name = "ec2_profile"
role = aws_iam_role.dev-resources-iam-role.name
}
resource "aws_iam_role" "dev-resources-iam-role" {
name = "dev-ssm-role"
description = "The role for the developer resources EC2"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}
}
EOF
}
resource "aws_iam_role_policy_attachment" "dev-resources-ssm-policy" {
role = aws_iam_role.dev-resources-iam-role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}