Skip to content

Commit 6f31239

Browse files
author
Steven Nemetz
committed
Rework to use boolean and label modules. Cleanup output names
1 parent 5b8e75c commit 6f31239

File tree

8 files changed

+209
-153
lines changed

8 files changed

+209
-153
lines changed

examples/asg_ec2/main.tf

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
provider "aws" {
2-
region = "eu-west-1"
2+
region = "${var.region}"
33

44
# Make it faster by skipping something
55
skip_get_ec2_platforms = true
@@ -12,16 +12,18 @@ provider "aws" {
1212
##############################################################
1313
# Data sources to get VPC, subnets and security group details
1414
##############################################################
15-
data "aws_vpc" "default" {
16-
default = true
15+
data "aws_vpc" "vpc" {
16+
tags {
17+
Env = "${var.environment}"
18+
}
1719
}
1820

1921
data "aws_subnet_ids" "all" {
20-
vpc_id = "${data.aws_vpc.default.id}"
22+
vpc_id = "${data.aws_vpc.vpc.id}"
2123
}
2224

2325
data "aws_security_group" "default" {
24-
vpc_id = "${data.aws_vpc.default.id}"
26+
vpc_id = "${data.aws_vpc.vpc.id}"
2527
name = "default"
2628
}
2729

@@ -57,7 +59,8 @@ module "example" {
5759
#
5860
# launch_configuration = "my-existing-launch-configuration" # Use the existing launch configuration
5961
# create_lc = false # disables creation of launch configuration
60-
lc_name = "example-lc"
62+
#lc_name = "example-lc"
63+
environment = "${var.environment}"
6164

6265
image_id = "${data.aws_ami.amazon_linux.id}"
6366
instance_type = "t2.micro"
@@ -82,20 +85,15 @@ module "example" {
8285
]
8386

8487
# Auto scaling group
85-
asg_name = "example-asg"
88+
#asg_name = "example-asg"
8689
vpc_zone_identifier = ["${data.aws_subnet_ids.all.ids}"]
8790
health_check_type = "EC2"
8891
min_size = 0
8992
max_size = 1
9093
desired_capacity = 1
9194
wait_for_capacity_timeout = 0
9295

93-
tags = [
94-
{
95-
key = "Environment"
96-
value = "dev"
97-
propagate_at_launch = true
98-
},
96+
tags_ag = [
9997
{
10098
key = "Project"
10199
value = "megasecret"

examples/asg_ec2/outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Launch configuration
2-
output "this_launch_configuration_id" {
2+
output "launch_configuration_id" {
33
description = "The ID of the launch configuration"
4-
value = "${module.example.this_launch_configuration_id}"
4+
value = "${module.example.launch_configuration_id}"
55
}
66

77
# Autoscaling group
8-
output "this_autoscaling_group_id" {
8+
output "autoscaling_group_id" {
99
description = "The autoscaling group id"
10-
value = "${module.example.this_autoscaling_group_id}"
10+
value = "${module.example.autoscaling_group_id}"
1111
}

examples/asg_ec2/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
variable "environment" {
2+
default = "one"
3+
}
4+
variable "region" {
5+
default = "us-west-2"
6+
}

examples/asg_elb/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
provider "aws" {
2-
region = "eu-west-1"
2+
region = "us-west-2"
33
}
44

55
##############################################################

examples/asg_elb/outputs.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Launch configuration
2-
output "this_launch_configuration_id" {
2+
output "launch_configuration_id" {
33
description = "The ID of the launch configuration"
4-
value = "${module.example_asg.this_launch_configuration_id}"
4+
value = "${module.example_asg.launch_configuration_id}"
55
}
66

77
# Autoscaling group
8-
output "this_autoscaling_group_id" {
8+
output "autoscaling_group_id" {
99
description = "The autoscaling group id"
10-
value = "${module.example_asg.this_autoscaling_group_id}"
10+
value = "${module.example_asg.autoscaling_group_id}"
1111
}
1212

1313
# ELB DNS name
14-
output "this_elb_dns_name" {
14+
output "elb_dns_name" {
1515
description = "DNS Name of the ELB"
1616
value = "${module.elb.this_elb_dns_name}"
1717
}

main.tf

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
1+
2+
###
3+
### Terraform AWS Autoscaling
4+
###
5+
6+
# Documentation references:
7+
# https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html
8+
# https://www.terraform.io/docs/providers/aws/r/autoscaling_attachment.html
9+
# https://www.terraform.io/docs/providers/aws/r/autoscaling_lifecycle_hooks.html
10+
# https://www.terraform.io/docs/providers/aws/r/autoscaling_notification.html
11+
# https://www.terraform.io/docs/providers/aws/r/autoscaling_policy.html
12+
# https://www.terraform.io/docs/providers/aws/r/autoscaling_schedule.html
13+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
14+
15+
module "enabled" {
16+
source = "devops-workflow/boolean/local"
17+
version = "0.1.1"
18+
value = "${var.enabled}"
19+
}
20+
21+
# Define composite variables for resources
22+
module "label" {
23+
source = "devops-workflow/label/local"
24+
version = "0.1.3"
25+
organization = "${var.organization}"
26+
name = "${var.name}"
27+
namespace-env = "${var.namespace-env}"
28+
namespace-org = "${var.namespace-org}"
29+
environment = "${var.environment}"
30+
delimiter = "${var.delimiter}"
31+
attributes = "${var.attributes}"
32+
tags = "${var.tags}"
33+
}
34+
135
#######################
236
# Launch configuration
337
#######################
438
resource "aws_launch_configuration" "this" {
5-
count = "${var.create_lc}"
39+
count = "${module.enabled.value && var.launch_configuration == "" ? 1 : 0 }"
640

7-
name_prefix = "${coalesce(var.lc_name, var.name)}-"
41+
name_prefix = "${coalesce(var.lc_name, module.label.id)}-"
842
image_id = "${var.image_id}"
943
instance_type = "${var.instance_type}"
1044
iam_instance_profile = "${var.iam_instance_profile}"
@@ -14,50 +48,74 @@ resource "aws_launch_configuration" "this" {
1448
user_data = "${var.user_data}"
1549
enable_monitoring = "${var.enable_monitoring}"
1650
placement_tenancy = "${var.placement_tenancy}"
17-
ebs_optimized = "${var.ebs_optimized}"
1851
ebs_block_device = "${var.ebs_block_device}"
52+
ebs_optimized = "${var.ebs_optimized}"
1953
ephemeral_block_device = "${var.ephemeral_block_device}"
2054
root_block_device = "${var.root_block_device}"
21-
2255
lifecycle {
2356
create_before_destroy = true
2457
}
25-
58+
#spot_price = "${var.spot_price == "0" ? "" : var.spot_price}"
2659
# spot_price = "${var.spot_price}" // placement_tenancy does not work with spot_price
2760
}
61+
/*
62+
# Attempt at improving the issue where it cannot delete the old LC on changes
63+
resource "null_resource" "delay" {
64+
# count = 10
65+
depends_on = [
66+
"aws_launch_configuration.this"
67+
]
68+
triggers {
69+
delay = "${aws_launch_configuration.this.name}"
70+
}
71+
lifecycle {
72+
create_before_destroy = true
73+
}
74+
}
75+
*/
2876

2977
####################
3078
# Autoscaling group
3179
####################
3280
resource "aws_autoscaling_group" "this" {
33-
count = "${var.create_asg}"
34-
35-
name_prefix = "${coalesce(var.asg_name, var.name)}-"
36-
launch_configuration = "${var.create_lc ? element(aws_launch_configuration.this.*.name, 0) : var.launch_configuration}"
37-
vpc_zone_identifier = ["${var.vpc_zone_identifier}"]
38-
max_size = "${var.max_size}"
39-
min_size = "${var.min_size}"
40-
desired_capacity = "${var.desired_capacity}"
81+
/*depends_on = [
82+
"null_resource.delay"
83+
]*/
84+
count = "${module.enabled.value}"
85+
name_prefix = "${coalesce(var.asg_name, module.label.id)}-"
86+
launch_configuration = "${var.launch_configuration == "" ? element(aws_launch_configuration.this.*.name, 0) : var.launch_configuration}"
87+
vpc_zone_identifier = ["${var.vpc_zone_identifier}"]
88+
max_size = "${var.max_size}"
89+
min_size = "${var.min_size}"
90+
desired_capacity = "${var.desired_capacity}"
4191

4292
load_balancers = ["${var.load_balancers}"]
4393
health_check_grace_period = "${var.health_check_grace_period}"
4494
health_check_type = "${var.health_check_type}"
4595

46-
min_elb_capacity = "${var.min_elb_capacity}"
47-
wait_for_elb_capacity = "${var.wait_for_elb_capacity}"
48-
target_group_arns = ["${var.target_group_arns}"]
96+
#availability_zones = ["${var.availability_zones}"]
4997
default_cooldown = "${var.default_cooldown}"
50-
force_delete = "${var.force_delete}"
51-
termination_policies = "${var.termination_policies}"
52-
suspended_processes = "${var.suspended_processes}"
53-
placement_group = "${var.placement_group}"
5498
enabled_metrics = ["${var.enabled_metrics}"]
99+
force_delete = "${var.force_delete}"
55100
metrics_granularity = "${var.metrics_granularity}"
56-
wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}"
101+
min_elb_capacity = "${var.min_elb_capacity}"
102+
placement_group = "${var.placement_group}"
57103
protect_from_scale_in = "${var.protect_from_scale_in}"
104+
suspended_processes = "${var.suspended_processes}"
105+
target_group_arns = ["${var.target_group_arns}"]
106+
termination_policies = "${var.termination_policies}"
107+
wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}"
108+
wait_for_elb_capacity = "${var.wait_for_elb_capacity}"
58109

59-
tags = ["${concat(
60-
list(map("key", "Name", "value", var.name, "propagate_at_launch", true)),
61-
var.tags
62-
)}"]
110+
tags = ["${ concat(
111+
list(
112+
map("key", "Name", "value", module.label.id, "propagate_at_launch", true),
113+
map("key", "Environment", "value", module.label.environment, "propagate_at_launch", true),
114+
map("key", "Terraform", "value", "true", "propagate_at_launch", true)
115+
),
116+
var.tags_ag
117+
)}"]
118+
lifecycle {
119+
create_before_destroy = true
120+
}
63121
}

outputs.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
11
# Launch configuration
2-
output "this_launch_configuration_id" {
2+
output "launch_configuration_id" {
33
description = "The ID of the launch configuration"
4-
value = "${var.launch_configuration == "" && var.create_lc ? element(concat(aws_launch_configuration.this.*.id, list("")), 0) : var.launch_configuration}"
4+
value = "${var.launch_configuration == "" ? element(concat(aws_launch_configuration.this.*.id, list("")), 0) : var.launch_configuration}"
55
}
66

7-
output "this_launch_configuration_name" {
7+
output "launch_configuration_name" {
88
description = "The name of the launch configuration"
9-
value = "${var.launch_configuration == "" && var.create_lc ? element(concat(aws_launch_configuration.this.*.name, list("")), 0) : ""}"
9+
value = "${var.launch_configuration == "" ? element(concat(aws_launch_configuration.this.*.name, list("")), 0) : ""}"
1010
}
1111

1212
# Autoscaling group
13-
output "this_autoscaling_group_id" {
13+
output "autoscaling_group_id" {
1414
description = "The autoscaling group id"
1515
value = "${element(concat(aws_autoscaling_group.this.*.id, list("")), 0)}"
1616
}
1717

18-
output "this_autoscaling_group_name" {
18+
output "autoscaling_group_name" {
1919
description = "The autoscaling group name"
2020
value = "${element(concat(aws_autoscaling_group.this.*.name, list("")), 0)}"
2121
}
2222

23-
output "this_autoscaling_group_arn" {
23+
output "autoscaling_group_arn" {
2424
description = "The ARN for this AutoScaling Group"
2525
value = "${element(concat(aws_autoscaling_group.this.*.arn, list("")), 0)}"
2626
}
2727

28-
output "this_autoscaling_group_min_size" {
28+
output "autoscaling_group_min_size" {
2929
description = "The minimum size of the autoscale group"
3030
value = "${element(concat(aws_autoscaling_group.this.*.min_size, list("")), 0)}"
3131
}
3232

33-
output "this_autoscaling_group_max_size" {
33+
output "autoscaling_group_max_size" {
3434
description = "The maximum size of the autoscale group"
3535
value = "${element(concat(aws_autoscaling_group.this.*.max_size, list("")), 0)}"
3636
}
3737

38-
output "this_autoscaling_group_desired_capacity" {
38+
output "autoscaling_group_desired_capacity" {
3939
description = "The number of Amazon EC2 instances that should be running in the group"
4040
value = "${element(concat(aws_autoscaling_group.this.*.desired_capacity, list("")), 0)}"
4141
}
4242

43-
output "this_autoscaling_group_default_cooldown" {
43+
output "autoscaling_group_default_cooldown" {
4444
description = "Time between a scaling activity and the succeeding scaling activity"
4545
value = "${element(concat(aws_autoscaling_group.this.*.default_cooldown, list("")), 0)}"
4646
}
4747

48-
output "this_autoscaling_group_health_check_grace_period" {
48+
output "autoscaling_group_health_check_grace_period" {
4949
description = "Time after instance comes into service before checking health"
5050
value = "${element(concat(aws_autoscaling_group.this.*.health_check_grace_period, list("")), 0)}"
5151
}
5252

53-
output "this_autoscaling_group_health_check_type" {
53+
output "autoscaling_group_health_check_type" {
5454
description = "EC2 or ELB. Controls how health checking is done"
5555
value = "${element(concat(aws_autoscaling_group.this.*.health_check_type, list("")), 0)}"
5656
}
5757

58-
//output "this_autoscaling_group_vpc_zone_identifier" {
58+
//output "autoscaling_group_vpc_zone_identifier" {
5959
// description = "The VPC zone identifier"
6060
// value = "${element(concat(aws_autoscaling_group.this.vpc_zone_identifier, list("")), 0)}"
6161
//}
6262
//
63-
//output "this_autoscaling_group_load_balancers" {
63+
//output "autoscaling_group_load_balancers" {
6464
// description = "The load balancer names associated with the autoscaling group"
6565
// value = "${aws_autoscaling_group.this.load_balancers}"
6666
//}
6767
//
68-
//output "this_autoscaling_group_target_group_arns" {
68+
//output "autoscaling_group_target_group_arns" {
6969
// description = "List of Target Group ARNs that apply to this AutoScaling Group"
7070
// value = "${aws_autoscaling_group.this.target_group_arns}"
7171
//}

0 commit comments

Comments
 (0)