Skip to content

Commit 7ec06c7

Browse files
authored
Merge ASG and LC submodules into one, added conditional creation (terraform-aws-modules#10)
* Merge ASG and LC submodules into one, added conditional creation * Merged ASG and LC into single module
1 parent b5c431d commit 7ec06c7

File tree

12 files changed

+113
-402
lines changed

12 files changed

+113
-402
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ These types of resources are supported:
88
* [Launch Configuration](https://www.terraform.io/docs/providers/aws/r/launch_configuration.html)
99
* [Auto Scaling Group](https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html)
1010

11-
Root module calls these modules which can also be used separately to create independent resources:
12-
13-
* [launch_configuration](https://github.com/terraform-aws-modules/terraform-aws-autoscaling/tree/master/modules/launch_configuration) - creates Launch Configuration
14-
* [autoscaling_group](https://github.com/terraform-aws-modules/terraform-aws-autoscaling/tree/master/modules/autoscaling_group) - creates Auto Scaling Group
15-
1611
Usage
1712
-----
1813

1914
```hcl
2015
module "asg" {
2116
source = "terraform-aws-modules/autoscaling/aws"
2217
18+
name = "service"
19+
2320
# Launch configuration
2421
lc_name = "example-lc"
2522
@@ -67,6 +64,25 @@ module "asg" {
6764
}
6865
```
6966

67+
Conditional creation
68+
--------------------
69+
70+
Normally this module creates both Auto Scaling Group (ASG) and Launch Configuration (LC), and connect them together.
71+
It is possible to customize this behaviour passing different parameters to this module:
72+
1. To create ASG, but not LC. Associate ASG with an existing LC:
73+
```hcl
74+
create_lc = false
75+
launch_configuration = "existing-launch-configuration"
76+
```
77+
78+
1. To create LC, but not ASG. Outputs may produce errors.
79+
```hcl
80+
create_asg = false
81+
```
82+
83+
1. To disable creation of both resources (LC and ASG) you can specify both arguments `create_lc = false` and `create_asg = false`. Sometimes you need to use this way to create resources in modules conditionally but Terraform does not allow to use `count` inside `module` block.
84+
85+
7086
Examples
7187
--------
7288

examples/asg_ec2/main.tf

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
provider "aws" {
22
region = "eu-west-1"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
310
}
411

512
##############################################################
@@ -44,15 +51,18 @@ data "aws_ami" "amazon_linux" {
4451
module "example" {
4552
source = "../../"
4653

54+
name = "example-with-ec2"
55+
4756
# Launch configuration
4857
#
49-
# Uncomment the value below and provide value of existing launch configuration to use it instead of making new one
50-
# existing_launch_configuration = "aa"
58+
# launch_configuration = "my-existing-launch-configuration" # Use the existing launch configuration
59+
# create_lc = false # disables creation of launch configuration
5160
lc_name = "example-lc"
5261

53-
image_id = "${data.aws_ami.amazon_linux.id}"
54-
instance_type = "t2.micro"
55-
security_groups = ["${data.aws_security_group.default.id}"]
62+
image_id = "${data.aws_ami.amazon_linux.id}"
63+
instance_type = "t2.micro"
64+
security_groups = ["${data.aws_security_group.default.id}"]
65+
associate_public_ip_address = true
5666

5767
ebs_block_device = [
5868
{
@@ -67,6 +77,7 @@ module "example" {
6777
{
6878
volume_size = "50"
6979
volume_type = "gp2"
80+
delete_on_termination = true
7081
},
7182
]
7283

examples/asg_elb/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ data "aws_ami" "amazon_linux" {
4444
module "example_asg" {
4545
source = "../../"
4646

47+
name = "example-with-elb"
48+
4749
# Launch configuration
4850
#
49-
# Uncomment the value below and provide value of existing launch configuration to use it instead of making new one
50-
# existing_launch_configuration = "aa"
51+
# launch_configuration = "my-existing-launch-configuration" # Use the existing launch configuration
52+
# create_lc = false # disables creation of launch configuration
5153
lc_name = "example-lc"
5254

5355
image_id = "${data.aws_ami.amazon_linux.id}"

main.tf

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#######################
22
# Launch configuration
33
#######################
4-
module "launch_configuration" {
5-
source = "./modules/launch_configuration"
4+
resource "aws_launch_configuration" "this" {
5+
count = "${var.create_lc}"
66

7-
count = "${var.existing_launch_configuration != "" ? 0 : 1}"
8-
9-
name = "${var.lc_name}"
7+
name_prefix = "${coalesce(var.lc_name, var.name)}-"
108
image_id = "${var.image_id}"
119
instance_type = "${var.instance_type}"
1210
iam_instance_profile = "${var.iam_instance_profile}"
@@ -15,28 +13,31 @@ module "launch_configuration" {
1513
associate_public_ip_address = "${var.associate_public_ip_address}"
1614
user_data = "${var.user_data}"
1715
enable_monitoring = "${var.enable_monitoring}"
18-
spot_price = "${var.spot_price}"
1916
placement_tenancy = "${var.placement_tenancy}"
17+
ebs_optimized = "${var.ebs_optimized}"
18+
ebs_block_device = "${var.ebs_block_device}"
19+
ephemeral_block_device = "${var.ephemeral_block_device}"
20+
root_block_device = "${var.root_block_device}"
21+
22+
lifecycle {
23+
create_before_destroy = true
24+
}
2025

21-
ebs_optimized = "${var.ebs_optimized}"
22-
ebs_block_device = "${var.ebs_block_device}"
23-
ephemeral_block_device = "${var.ephemeral_block_device}"
24-
root_block_device = "${var.root_block_device}"
26+
# spot_price = "${var.spot_price}" // placement_tenancy does not work with spot_price
2527
}
2628

2729
####################
28-
# Autoscaling Group
30+
# Autoscaling group
2931
####################
30-
module "autoscaling_group" {
31-
source = "./modules/autoscaling_group"
32+
resource "aws_autoscaling_group" "this" {
33+
count = "${var.create_asg}"
3234

33-
name = "${var.asg_name}"
34-
launch_configuration = "${var.existing_launch_configuration != "" ? var.existing_launch_configuration : module.launch_configuration.this_launch_configuration_id}"
35+
name_prefix = "${coalesce(var.asg_name, var.name)}-"
36+
launch_configuration = "${var.create_lc ? element(concat(aws_launch_configuration.this.*.id, list(var.launch_configuration)), 0) : var.launch_configuration}"
3537
vpc_zone_identifier = ["${var.vpc_zone_identifier}"]
36-
37-
max_size = "${var.max_size}"
38-
min_size = "${var.min_size}"
39-
desired_capacity = "${var.desired_capacity}"
38+
max_size = "${var.max_size}"
39+
min_size = "${var.min_size}"
40+
desired_capacity = "${var.desired_capacity}"
4041

4142
load_balancers = ["${var.load_balancers}"]
4243
health_check_grace_period = "${var.health_check_grace_period}"
@@ -55,5 +56,8 @@ module "autoscaling_group" {
5556
wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}"
5657
protect_from_scale_in = "${var.protect_from_scale_in}"
5758

58-
tags = "${var.tags}"
59+
tags = ["${concat(
60+
var.tags,
61+
list(map("key", "Name", "value", var.name, "propagate_at_launch", true))
62+
)}"]
5963
}

modules/autoscaling_group/main.tf

Lines changed: 0 additions & 38 deletions
This file was deleted.

modules/autoscaling_group/outputs.tf

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)