-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.tf
More file actions
189 lines (152 loc) · 4.42 KB
/
Copy pathmain.tf
File metadata and controls
189 lines (152 loc) · 4.42 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#TODO list:
# * add new az and new subnet so we can run ALB
# *
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "${var.vpc_name}example_1"
cidr = "10.0.0.0/16"
azs = ["eu-west-1a", "eu-west-1b"]
private_subnets = ["10.0.1.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_dns_hostnames = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
module "instance_sg" {
source = "terraform-aws-modules/security-group/aws"
name = "user-service-sg"
description = "Security group for user-service with custom ports open within VPC, and PostgreSQL publicly open"
vpc_id = module.vpc.vpc_id
ingress_with_cidr_blocks = [
{
rule = "http-80-tcp"
cidr_blocks = "0.0.0.0/0"
},
{
rule = "ssh-tcp"
cidr_blocks = "0.0.0.0/0"
},
{
rule = "http-8080-tcp",
cidr_blocks = "0.0.0.0/0"
}
]
egress_with_cidr_blocks = [
{
rule = "all-all"
cidr_blocks = "0.0.0.0/0"
}
]
}
module "efs_sg" {
#* EFS Security Group
source = "terraform-aws-modules/security-group/aws"
name = "user-service-sg-efs"
description = "Security group for user-service with custom ports open within VPC, and PostgreSQL publicly open"
vpc_id = module.vpc.vpc_id
number_of_computed_ingress_with_source_security_group_id = 1
computed_ingress_with_source_security_group_id = [
{
rule = "nfs-tcp"
source_security_group_id = module.instance_sg.this_security_group_id
}
]
egress_with_cidr_blocks = [
{
rule = "all-all"
cidr_blocks = "0.0.0.0/0"
}
]
}
resource "aws_key_pair" "ec2key" {
key_name = var.public_key_name
public_key = file(var.public_key_path)
}
resource "aws_efs_file_system" "efs" {
creation_token = "${var.vpc_name}efs"
encrypted = true
tags = {
Name = "${var.vpc_name}efs"
}
}
resource "aws_efs_mount_target" "efs_mount" {
file_system_id = aws_efs_file_system.efs.id
subnet_id = module.vpc.public_subnets[0]
security_groups = [module.efs_sg.this_security_group_id]
}
resource "aws_efs_access_point" "efs_access_point" {
file_system_id = aws_efs_file_system.efs.id
}
#* script to setup the instance
data "template_file" "init" {
template = file("script.tpl")
vars = {
efs_id = aws_efs_file_system.efs.id
efs_mount_id = aws_efs_mount_target.efs_mount.id
efs_access_point_id = aws_efs_access_point.efs_access_point.id
}
}
module "auto_scaling" {
source = "terraform-aws-modules/autoscaling/aws"
version = "~> 3.0"
name = "${var.vpc_name}auto_scaling"
# Launch configuration
lc_name = "${var.vpc_name}auto_scaling_lc"
image_id = var.instance_ami
instance_type = var.instance_type
security_groups = [module.instance_sg.this_security_group_id]
ebs_block_device = [
{
device_name = "/dev/xvdz"
volume_type = "gp2"
volume_size = "8"
delete_on_termination = true
},
]
# Auto scaling group
asg_name = "${var.vpc_name}auto_scaling_lc"
vpc_zone_identifier = [module.vpc.public_subnets[0]]
health_check_type = "EC2"
min_size = 1
max_size = 1
desired_capacity = 1
wait_for_capacity_timeout = 0
user_data = data.template_file.init.rendered
key_name = aws_key_pair.ec2key.key_name
tags = [
{
key = "Environment"
value = "Dev"
propagate_at_launch = true
}
]
}
module "elb_http" {
source = "terraform-aws-modules/elb/aws"
version = "~> 2.0"
name = "elb-example"
subnets = [module.vpc.public_subnets[0], module.vpc.public_subnets[1]]
security_groups = [module.instance_sg.this_security_group_id]
internal = false
listener = [
{
instance_port = "80"
instance_protocol = "HTTP"
lb_port = "80"
lb_protocol = "HTTP"
}
]
health_check = {
target = "HTTP:80/"
interval = 300
healthy_threshold = 2
unhealthy_threshold = 5
timeout = 60
}
}
resource "aws_autoscaling_attachment" "asg_attachment_bar" {
autoscaling_group_name = module.auto_scaling.this_autoscaling_group_id
elb = module.elb_http.this_elb_id
}