Skip to content

Commit 3de628f

Browse files
committed
refactor(modules)!: remove asg sub-module
1 parent 642efd0 commit 3de628f

File tree

13 files changed

+51
-1004
lines changed

13 files changed

+51
-1004
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Terraform module to deploy production-ready applications and services on an exis
2222
|------|--------|---------|
2323
| <a name="module_acm"></a> [acm](#module\_acm) | ./modules/acm | n/a |
2424
| <a name="module_alb"></a> [alb](#module\_alb) | ./modules/alb | n/a |
25-
| <a name="module_asg"></a> [asg](#module\_asg) | ./modules/asg | n/a |
2625
| <a name="module_capacity_provider"></a> [capacity\_provider](#module\_capacity\_provider) | ./modules/capacity-provider | n/a |
2726

2827
## Resources
@@ -39,13 +38,11 @@ Terraform module to deploy production-ready applications and services on an exis
3938
| <a name="input_acm_amazon_issued_certificates"></a> [acm\_amazon\_issued\_certificates](#input\_acm\_amazon\_issued\_certificates) | Amazon-issued ACM certificates to create | `any` | `{}` | no |
4039
| <a name="input_acm_imported_certificates"></a> [acm\_imported\_certificates](#input\_acm\_imported\_certificates) | Imported ACM certificates to create | `any` | `{}` | no |
4140
| <a name="input_acm_private_ca_issued_certificates"></a> [acm\_private\_ca\_issued\_certificates](#input\_acm\_private\_ca\_issued\_certificates) | Private CA Issued ACM certificates to create | `any` | `{}` | no |
42-
| <a name="input_autoscaling_group"></a> [autoscaling\_group](#input\_autoscaling\_group) | Configuration for Autoscaling Group | `any` | `{}` | no |
4341
| <a name="input_capacity_provider_default_auto_scaling_group_arn"></a> [capacity\_provider\_default\_auto\_scaling\_group\_arn](#input\_capacity\_provider\_default\_auto\_scaling\_group\_arn) | Default Autoscaling Group to use with the Capacity Providers | `string` | `null` | no |
4442
| <a name="input_capacity_providers"></a> [capacity\_providers](#input\_capacity\_providers) | Capacity Providers to create for use within the ECS Cluster | `any` | `{}` | no |
4543
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the ECS Cluster to use with the ECS Service | `string` | n/a | yes |
4644
| <a name="input_create_acm"></a> [create\_acm](#input\_create\_acm) | Creates the ACM certificates to use with the Load Balancer | `bool` | `false` | no |
4745
| <a name="input_create_alb"></a> [create\_alb](#input\_create\_alb) | Creates a new Application Load Balancer to use with the ECS Service | `bool` | `true` | no |
48-
| <a name="input_create_autoscaling_group"></a> [create\_autoscaling\_group](#input\_create\_autoscaling\_group) | Creates a new Autoscaling group to use with the ECS Service | `bool` | `true` | no |
4946
| <a name="input_create_capacity_provider"></a> [create\_capacity\_provider](#input\_create\_capacity\_provider) | Creates a new Capacity Provider to use with the Autoscaling Group | `bool` | `true` | no |
5047
| <a name="input_default_capacity_providers_strategies"></a> [default\_capacity\_providers\_strategies](#input\_default\_capacity\_providers\_strategies) | Default Capacity Provider Strategies to use | `any` | `[]` | no |
5148
| <a name="input_load_balancer"></a> [load\_balancer](#input\_load\_balancer) | Configuration for the Application Load Balancer | `any` | `{}` | no |
@@ -67,13 +64,6 @@ Terraform module to deploy production-ready applications and services on an exis
6764
| <a name="output_alb_zone_id"></a> [alb\_zone\_id](#output\_alb\_zone\_id) | Canonical hosted zone ID of the Load Balancer (to be used in a Route 53 Alias record) |
6865
| <a name="output_amazon_issued_acm_certificates_arns"></a> [amazon\_issued\_acm\_certificates\_arns](#output\_amazon\_issued\_acm\_certificates\_arns) | ARNs of the Amazon issued ACM certificates |
6966
| <a name="output_amazon_issued_acm_certificates_validation_records"></a> [amazon\_issued\_acm\_certificates\_validation\_records](#output\_amazon\_issued\_acm\_certificates\_validation\_records) | Validation Records of the Amazon issued ACM certificates |
70-
| <a name="output_asg_arn"></a> [asg\_arn](#output\_asg\_arn) | ARN of the Autoscaling group |
71-
| <a name="output_asg_iam_instance_profile_arn"></a> [asg\_iam\_instance\_profile\_arn](#output\_asg\_iam\_instance\_profile\_arn) | ARN of the IAM Instance Profile |
72-
| <a name="output_asg_iam_instance_profile_id"></a> [asg\_iam\_instance\_profile\_id](#output\_asg\_iam\_instance\_profile\_id) | Identifier of the IAM Instance Profile |
73-
| <a name="output_asg_iam_role_id"></a> [asg\_iam\_role\_id](#output\_asg\_iam\_role\_id) | Identifier of the IAM Role |
74-
| <a name="output_asg_id"></a> [asg\_id](#output\_asg\_id) | Identifier of the Autoscaling group |
75-
| <a name="output_asg_launch_template_arn"></a> [asg\_launch\_template\_arn](#output\_asg\_launch\_template\_arn) | ARN of the Launch Template |
76-
| <a name="output_asg_launch_template_id"></a> [asg\_launch\_template\_id](#output\_asg\_launch\_template\_id) | Identifier of the Launch Template |
7767
| <a name="output_capacity_provider_arns"></a> [capacity\_provider\_arns](#output\_capacity\_provider\_arns) | ARNs for the ECS Capacity Providers |
7868
| <a name="output_capacity_provider_ecs_cluster_capacity_providers_id"></a> [capacity\_provider\_ecs\_cluster\_capacity\_providers\_id](#output\_capacity\_provider\_ecs\_cluster\_capacity\_providers\_id) | Identifier for the ECS Cluster Capacity Providers |
7969
| <a name="output_capacity_provider_ids"></a> [capacity\_provider\_ids](#output\_capacity\_provider\_ids) | Identifiers for the ECS Capacity Providers |

examples/complete/main.tf

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
provider "aws" {
2-
region = local.region
2+
region = local.region
3+
profile = "dev"
34

45
default_tags {
56
tags = local.default_tags
@@ -108,33 +109,8 @@ module "ecs_deployment" {
108109
]
109110
}
110111

111-
# ASG
112-
autoscaling_group = {
113-
name = "${local.name_prefix}my-asg"
114-
vpc_zone_identifier = module.vpc.private_subnets
115-
protect_from_scale_in = true
116-
117-
desired_capacity = 1
118-
min_size = 1
119-
max_size = 1
120-
121-
launch_template = {
122-
name = "${local.name_prefix}my-launch-template"
123-
image_id = data.aws_ami.ecs_optimized_amzn_linux.id
124-
instance_type = "t2.micro"
125-
vpc_security_group_ids = [aws_security_group.allow_all_within_vpc.id]
126-
user_data = <<-EOT
127-
#!/bin/bash
128-
echo ECS_CLUSTER=${local.cluster_name} >> /etc/ecs/ecs.config
129-
EOT
130-
}
131-
132-
iam_role_policy_attachments = [
133-
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
134-
]
135-
}
136-
137112
# Capacity Provider
113+
capacity_provider_default_auto_scaling_group_arn = module.asg.autoscaling_group_arn
138114
capacity_providers = {
139115
my-capacity-provider = {
140116
name = "${local.name_prefix}my-capacity-provider"
@@ -199,6 +175,8 @@ module "ecs_deployment" {
199175
}
200176
}
201177
}
178+
179+
depends_on = [module.asg]
202180
}
203181

204182
################################################################################
@@ -220,6 +198,44 @@ module "vpc" {
220198
enable_vpn_gateway = false
221199
}
222200

201+
module "asg" {
202+
source = "terraform-aws-modules/autoscaling/aws"
203+
204+
name = "${local.name_prefix}my-asg"
205+
206+
min_size = 1
207+
max_size = 1
208+
desired_capacity = 1
209+
health_check_type = "EC2"
210+
vpc_zone_identifier = module.vpc.private_subnets
211+
212+
# Launch template
213+
launch_template_name = "${local.name_prefix}my-asg"
214+
launch_template_description = "My example Launch template"
215+
image_id = data.aws_ami.ecs_optimized_amzn_linux.image_id
216+
instance_type = "t2.micro"
217+
update_default_version = true
218+
security_groups = [aws_security_group.allow_all_within_vpc.id]
219+
220+
# IAM role & instance profile
221+
create_iam_instance_profile = true
222+
iam_role_name = "${local.name_prefix}my-asg"
223+
iam_role_description = "My example IAM role for ${local.name_prefix}my-asg"
224+
iam_role_policies = {
225+
AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role",
226+
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
227+
}
228+
229+
# This will ensure imdsv2 is enabled, required, and a single hop which is aws security
230+
# best practices
231+
# See https://docs.aws.amazon.com/securityhub/latest/userguide/autoscaling-controls.html#autoscaling-4
232+
metadata_options = {
233+
http_endpoint = "enabled"
234+
http_tokens = "required"
235+
http_put_response_hop_limit = 1
236+
}
237+
}
238+
223239
################################################################################
224240
# # ACM
225241
################################################################################

examples/complete/outputs.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,22 @@ output "vpc_public_subnets_arns" {
6666

6767
output "asg_id" {
6868
description = "Identifier of the Autoscaling group"
69-
value = module.ecs_deployment.asg_id
69+
value = module.asg.autoscaling_group_id
7070
}
7171

7272
output "asg_arn" {
7373
description = "ARN of the Autoscaling group"
74-
value = module.ecs_deployment.asg_arn
74+
value = module.asg.autoscaling_group_arn
7575
}
7676

7777
output "launch_template_id" {
7878
description = "Identifier of the Launch Template"
79-
value = module.ecs_deployment.asg_launch_template_id
79+
value = module.asg.launch_template_id
8080
}
8181

8282
output "launch_template_arn" {
8383
description = "ARN of the Launch Template"
84-
value = module.ecs_deployment.asg_launch_template_arn
84+
value = module.asg.launch_template_arn
8585
}
8686

8787
################################################################################
@@ -90,17 +90,17 @@ output "launch_template_arn" {
9090

9191
output "iam_instance_role_id" {
9292
description = "Identifier of the IAM Instance Role"
93-
value = module.ecs_deployment.asg_iam_role_id
93+
value = module.asg.iam_role_unique_id
9494
}
9595

9696
output "iam_instance_profile_id" {
9797
description = "Identifier of the IAM Instance Profile"
98-
value = module.ecs_deployment.asg_iam_instance_profile_id
98+
value = module.asg.iam_instance_profile_id
9999
}
100100

101101
output "iam_instance_profile_arn" {
102102
description = "ARN of the IAM Instance Profile"
103-
value = module.ecs_deployment.asg_iam_instance_profile_arn
103+
value = module.asg.iam_instance_profile_arn
104104
}
105105

106106
################################################################################

main.tf

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ resource "aws_ecs_service" "this" {
147147
}
148148

149149
tags = try(var.service.tags, {})
150-
151-
depends_on = [module.asg]
152150
}
153151

154152
################################################################################
@@ -277,41 +275,6 @@ resource "aws_ecs_task_definition" "this" {
277275
tags = try(var.task_definition.tags, {})
278276
}
279277

280-
################################################################################
281-
# Autoscaling Group Sub-module
282-
################################################################################
283-
284-
module "asg" {
285-
source = "./modules/asg"
286-
287-
count = var.create_autoscaling_group ? 1 : 0
288-
289-
name = try(var.autoscaling_group.name, null)
290-
vpc_zone_identifier = try(var.autoscaling_group.vpc_zone_identifier, [])
291-
292-
desired_capacity = try(var.autoscaling_group.desired_capacity, null)
293-
min_size = try(var.autoscaling_group.min_size, null)
294-
max_size = try(var.autoscaling_group.max_size, null)
295-
protect_from_scale_in = try(var.autoscaling_group.protect_from_scale_in, null)
296-
297-
# Launch Template
298-
create_launch_template = try(var.autoscaling_group.create_launch_template, true)
299-
launch_template_id = try(var.autoscaling_group.launch_template_id, null)
300-
launch_template = try(var.autoscaling_group.launch_template, {})
301-
302-
# IAM Instance Profile
303-
create_iam_role = try(var.autoscaling_group.create_iam_role, true)
304-
iam_role_name = try(var.autoscaling_group.iam_role_name, null)
305-
iam_role_policy_attachments = try(var.autoscaling_group.iam_role_policy_attachments, [])
306-
iam_role_tags = try(var.autoscaling_group.iam_role_tags, {})
307-
create_iam_instance_profile = try(var.autoscaling_group.create_iam_instance_profile, true)
308-
iam_instance_profile_name = try(var.autoscaling_group.iam_instance_profile_name, null)
309-
iam_instance_profile_tags = try(var.autoscaling_group.iam_instance_profile_tags, {})
310-
311-
instances_tags = try(var.autoscaling_group.instances_tags, {})
312-
tags = try(var.autoscaling_group.tags, {})
313-
}
314-
315278
################################################################################
316279
# Capacity Provider Sub-module
317280
################################################################################
@@ -322,7 +285,7 @@ module "capacity_provider" {
322285
count = var.create_capacity_provider ? 1 : 0
323286

324287
ecs_cluster_name = var.cluster_name
325-
default_auto_scaling_group_arn = var.create_autoscaling_group ? try(module.asg[0].arn, "") : var.capacity_provider_default_auto_scaling_group_arn
288+
default_auto_scaling_group_arn = var.capacity_provider_default_auto_scaling_group_arn
326289

327290
capacity_providers = var.capacity_providers
328291
default_capacity_provider_strategies = var.default_capacity_providers_strategies

modules/asg/.header.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

modules/asg/README.md

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

0 commit comments

Comments
 (0)