Skip to content

Commit 058c1c2

Browse files
authored
Merge pull request #58 from AdharaProjects/terraform0.12
Terraform 0.12 upgrade
2 parents 95a867f + 83105f2 commit 058c1c2

File tree

5 files changed

+90
-86
lines changed

5 files changed

+90
-86
lines changed

data.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ data "aws_ami" "amazon-linux-2" {
1010
}
1111

1212
data "aws_subnet" "subnets" {
13-
count = "${length(var.elb_subnets)}"
14-
id = "${var.elb_subnets[count.index]}"
13+
count = length(var.elb_subnets)
14+
id = var.elb_subnets[count.index]
1515
}
16+

locals.tf

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
locals {
2-
tags_asg_format = ["${null_resource.tags_as_list_of_maps.*.triggers}"]
2+
tags_asg_format = null_resource.tags_as_list_of_maps.*.triggers
33

4-
name_prefix = "${var.bastion_launch_configuration_name}"
4+
name_prefix = var.bastion_launch_configuration_name
55
}
66

77
resource "null_resource" "tags_as_list_of_maps" {
8-
count = "${length(keys(var.tags))}"
8+
count = length(keys(var.tags))
99

10-
triggers = "${map(
11-
"key", "${element(keys(var.tags), count.index)}",
12-
"value", "${element(values(var.tags), count.index)}",
13-
"propagate_at_launch", "true"
14-
)}"
10+
triggers = {
11+
"key" = element(keys(var.tags), count.index)
12+
"value" = element(values(var.tags), count.index)
13+
"propagate_at_launch" = "true"
14+
}
1515
}
16+

main.tf

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
11
data "template_file" "user_data" {
2-
template = "${file("${path.module}/user_data.sh")}"
2+
template = file("${path.module}/user_data.sh")
33

4-
vars {
5-
aws_region = "${var.region}"
6-
bucket_name = "${var.bucket_name}"
4+
vars = {
5+
aws_region = var.region
6+
bucket_name = var.bucket_name
77
}
88
}
99

1010
resource "aws_s3_bucket" "bucket" {
11-
bucket = "${var.bucket_name}"
11+
bucket = var.bucket_name
1212
acl = "bucket-owner-full-control"
1313

14-
force_destroy = "${var.bucket_force_destroy}"
14+
force_destroy = var.bucket_force_destroy
1515

1616
versioning {
17-
enabled = "${var.bucket_versioning}"
17+
enabled = var.bucket_versioning
1818
}
1919

2020
lifecycle_rule {
2121
id = "log"
22-
enabled = "${var.log_auto_clean}"
22+
enabled = var.log_auto_clean
2323

2424
prefix = "logs/"
2525

26-
tags {
27-
"rule" = "log"
28-
"autoclean" = "${var.log_auto_clean}"
26+
tags = {
27+
rule = "log"
28+
autoclean = var.log_auto_clean
2929
}
3030

3131
transition {
32-
days = "${var.log_standard_ia_days}"
32+
days = var.log_standard_ia_days
3333
storage_class = "STANDARD_IA"
3434
}
3535

3636
transition {
37-
days = "${var.log_glacier_days}"
37+
days = var.log_glacier_days
3838
storage_class = "GLACIER"
3939
}
4040

4141
expiration {
42-
days = "${var.log_expiry_days}"
42+
days = var.log_expiry_days
4343
}
4444
}
4545

46-
tags = "${merge(var.tags)}"
46+
tags = merge(var.tags)
4747
}
4848

4949
resource "aws_s3_bucket_object" "bucket_public_keys_readme" {
50-
bucket = "${aws_s3_bucket.bucket.id}"
50+
bucket = aws_s3_bucket.bucket.id
5151
key = "public-keys/README.txt"
5252
content = "Drop here the ssh public keys of the instances you want to control"
5353
}
5454

5555
resource "aws_security_group" "bastion_host_security_group" {
5656
description = "Enable SSH access to the bastion host from external via SSH port"
5757
name = "${local.name_prefix}-host"
58-
vpc_id = "${var.vpc_id}"
58+
vpc_id = var.vpc_id
5959

60-
tags = "${merge(var.tags)}"
60+
tags = merge(var.tags)
6161
}
6262

6363
resource "aws_security_group_rule" "ingress_bastion" {
6464
description = "Incoming traffic to bastion"
6565
type = "ingress"
66-
from_port = "${var.public_ssh_port}"
67-
to_port = "${var.public_ssh_port}"
66+
from_port = var.public_ssh_port
67+
to_port = var.public_ssh_port
6868
protocol = "TCP"
69-
cidr_blocks = ["${concat(data.aws_subnet.subnets.*.cidr_block, var.cidrs)}"]
69+
cidr_blocks = concat(data.aws_subnet.subnets.*.cidr_block, var.cidrs)
7070

71-
security_group_id = "${aws_security_group.bastion_host_security_group.id}"
71+
security_group_id = aws_security_group.bastion_host_security_group.id
7272
}
7373

7474
resource "aws_security_group_rule" "egress_bastion" {
@@ -79,27 +79,27 @@ resource "aws_security_group_rule" "egress_bastion" {
7979
protocol = "-1"
8080
cidr_blocks = ["0.0.0.0/0"]
8181

82-
security_group_id = "${aws_security_group.bastion_host_security_group.id}"
82+
security_group_id = aws_security_group.bastion_host_security_group.id
8383
}
8484

8585
resource "aws_security_group" "private_instances_security_group" {
8686
description = "Enable SSH access to the Private instances from the bastion via SSH port"
8787
name = "${local.name_prefix}-priv-instances"
88-
vpc_id = "${var.vpc_id}"
88+
vpc_id = var.vpc_id
8989

90-
tags = "${merge(var.tags)}"
90+
tags = merge(var.tags)
9191
}
9292

9393
resource "aws_security_group_rule" "ingress_instances" {
9494
description = "Incoming traffic from bastion"
9595
type = "ingress"
96-
from_port = "${var.public_ssh_port}"
97-
to_port = "${var.public_ssh_port}"
96+
from_port = var.public_ssh_port
97+
to_port = var.public_ssh_port
9898
protocol = "TCP"
9999

100-
source_security_group_id = "${aws_security_group.bastion_host_security_group.id}"
100+
source_security_group_id = aws_security_group.bastion_host_security_group.id
101101

102-
security_group_id = "${aws_security_group.private_instances_security_group.id}"
102+
security_group_id = aws_security_group.private_instances_security_group.id
103103
}
104104

105105
resource "aws_iam_role" "bastion_host_role" {
@@ -123,10 +123,11 @@ resource "aws_iam_role" "bastion_host_role" {
123123
]
124124
}
125125
EOF
126+
126127
}
127128

128129
resource "aws_iam_role_policy" "bastion_host_role_policy" {
129-
role = "${aws_iam_role.bastion_host_role.id}"
130+
role = aws_iam_role.bastion_host_role.id
130131

131132
policy = <<EOF
132133
{
@@ -158,78 +159,77 @@ resource "aws_iam_role_policy" "bastion_host_role_policy" {
158159
]
159160
}
160161
EOF
162+
161163
}
162164

163165
resource "aws_route53_record" "bastion_record_name" {
164-
name = "${var.bastion_record_name}"
165-
zone_id = "${var.hosted_zone_name}"
166+
name = var.bastion_record_name
167+
zone_id = var.hosted_zone_name
166168
type = "A"
167-
count = "${var.create_dns_record}"
169+
count = var.create_dns_record ? 1 : 0
168170

169171
alias {
170172
evaluate_target_health = true
171-
name = "${aws_lb.bastion_lb.dns_name}"
172-
zone_id = "${aws_lb.bastion_lb.zone_id}"
173+
name = aws_lb.bastion_lb.dns_name
174+
zone_id = aws_lb.bastion_lb.zone_id
173175
}
174176
}
175177

176178
resource "aws_lb" "bastion_lb" {
177-
internal = "${var.is_lb_private}"
179+
internal = var.is_lb_private
178180
name = "${local.name_prefix}-lb"
179181

180-
subnets = [
181-
"${var.elb_subnets}",
182-
]
182+
subnets = var.elb_subnets
183183

184184
load_balancer_type = "network"
185-
tags = "${merge(var.tags)}"
185+
tags = merge(var.tags)
186186
}
187187

188188
resource "aws_lb_target_group" "bastion_lb_target_group" {
189189
name = "${local.name_prefix}-lb-target"
190-
port = "${var.public_ssh_port}"
190+
port = var.public_ssh_port
191191
protocol = "TCP"
192-
vpc_id = "${var.vpc_id}"
192+
vpc_id = var.vpc_id
193193
target_type = "instance"
194194

195195
health_check {
196196
port = "traffic-port"
197197
protocol = "TCP"
198198
}
199199

200-
tags = "${merge(var.tags)}"
200+
tags = merge(var.tags)
201201
}
202202

203203
resource "aws_lb_listener" "bastion_lb_listener_22" {
204-
"default_action" {
205-
target_group_arn = "${aws_lb_target_group.bastion_lb_target_group.arn}"
204+
default_action {
205+
target_group_arn = aws_lb_target_group.bastion_lb_target_group.arn
206206
type = "forward"
207207
}
208208

209-
load_balancer_arn = "${aws_lb.bastion_lb.arn}"
210-
port = "${var.public_ssh_port}"
209+
load_balancer_arn = aws_lb.bastion_lb.arn
210+
port = var.public_ssh_port
211211
protocol = "TCP"
212212
}
213213

214214
resource "aws_iam_instance_profile" "bastion_host_profile" {
215-
role = "${aws_iam_role.bastion_host_role.name}"
215+
role = aws_iam_role.bastion_host_role.name
216216
path = "/"
217217
}
218218

219219
resource "aws_launch_configuration" "bastion_launch_configuration" {
220-
name_prefix = "${var.bastion_launch_configuration_name}"
221-
image_id = "${data.aws_ami.amazon-linux-2.id}"
220+
name_prefix = var.bastion_launch_configuration_name
221+
image_id = data.aws_ami.amazon-linux-2.id
222222
instance_type = "t2.nano"
223-
associate_public_ip_address = "${var.associate_public_ip_address}"
223+
associate_public_ip_address = var.associate_public_ip_address
224224
enable_monitoring = true
225-
iam_instance_profile = "${aws_iam_instance_profile.bastion_host_profile.name}"
226-
key_name = "${var.bastion_host_key_pair}"
225+
iam_instance_profile = aws_iam_instance_profile.bastion_host_profile.name
226+
key_name = var.bastion_host_key_pair
227227

228228
security_groups = [
229-
"${aws_security_group.bastion_host_security_group.id}",
229+
aws_security_group.bastion_host_security_group.id,
230230
]
231231

232-
user_data = "${data.template_file.user_data.rendered}"
232+
user_data = data.template_file.user_data.rendered
233233

234234
lifecycle {
235235
create_before_destroy = true
@@ -238,33 +238,32 @@ resource "aws_launch_configuration" "bastion_launch_configuration" {
238238

239239
resource "aws_autoscaling_group" "bastion_auto_scaling_group" {
240240
name = "ASG-${aws_launch_configuration.bastion_launch_configuration.name}"
241-
launch_configuration = "${aws_launch_configuration.bastion_launch_configuration.name}"
242-
max_size = "${var.bastion_instance_count}"
243-
min_size = "${var.bastion_instance_count}"
244-
desired_capacity = "${var.bastion_instance_count}"
241+
launch_configuration = aws_launch_configuration.bastion_launch_configuration.name
242+
max_size = var.bastion_instance_count
243+
min_size = var.bastion_instance_count
244+
desired_capacity = var.bastion_instance_count
245245

246-
vpc_zone_identifier = [
247-
"${var.auto_scaling_group_subnets}",
248-
]
246+
vpc_zone_identifier = var.auto_scaling_group_subnets
249247

250248
default_cooldown = 180
251249
health_check_grace_period = 180
252250
health_check_type = "EC2"
253251

254252
target_group_arns = [
255-
"${aws_lb_target_group.bastion_lb_target_group.arn}",
253+
aws_lb_target_group.bastion_lb_target_group.arn,
256254
]
257255

258256
termination_policies = [
259257
"OldestLaunchConfiguration",
260258
]
261259

262-
tags = ["${concat(
263-
list(map("key", "Name", "value", "ASG-${aws_launch_configuration.bastion_launch_configuration.name}", "propagate_at_launch", true)),
264-
local.tags_asg_format
265-
)}"]
260+
tags = concat(
261+
list(map("key", "Name", "value", "ASG-${aws_launch_configuration.bastion_launch_configuration.name}", "propagate_at_launch", true)),
262+
local.tags_asg_format
263+
)
266264

267265
lifecycle {
268266
create_before_destroy = true
269267
}
270268
}
269+

outputs.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
output "bucket_name" {
2-
value = "${aws_s3_bucket.bucket.bucket}"
2+
value = aws_s3_bucket.bucket.bucket
33
}
44

55
output "elb_ip" {
6-
value = "${aws_lb.bastion_lb.dns_name}"
6+
value = aws_lb.bastion_lb.dns_name
77
}
88

99
output "bastion_host_security_group" {
10-
value = "${aws_security_group.bastion_host_security_group.id}"
10+
value = aws_security_group.bastion_host_security_group.id
1111
}
1212

1313
output "private_instances_security_group" {
14-
value = "${aws_security_group.private_instances_security_group.id}"
14+
value = aws_security_group.private_instances_security_group.id
1515
}
16+

variables.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ variable "bucket_force_destroy" {
1515
variable "tags" {
1616
description = "A mapping of tags to assign"
1717
default = {}
18-
type = "map"
18+
type = map(string)
1919
}
2020

21-
variable "region" {}
21+
variable "region" {
22+
}
2223

2324
variable "cidrs" {
2425
description = "List of CIDRs than can access to the bastion. Default : 0.0.0.0/0"
25-
type = "list"
26+
type = list(string)
2627

2728
default = [
2829
"0.0.0.0/0",
@@ -57,12 +58,12 @@ variable "bastion_launch_configuration_name" {
5758
}
5859

5960
variable "elb_subnets" {
60-
type = "list"
61+
type = list(string)
6162
description = "List of subnet were the ELB will be deployed"
6263
}
6364

6465
variable "auto_scaling_group_subnets" {
65-
type = "list"
66+
type = list(string)
6667
description = "List of subnet were the Auto Scalling Group will deploy the instances"
6768
}
6869

@@ -107,3 +108,4 @@ variable "private_ssh_port" {
107108
description = "Set the SSH port to use between the bastion and private instance"
108109
default = 22
109110
}
111+

0 commit comments

Comments
 (0)