Skip to content

Commit 18e5819

Browse files
committed
feat(modules): autoscaling group
1 parent c8aa7ee commit 18e5819

File tree

6 files changed

+323
-0
lines changed

6 files changed

+323
-0
lines changed

modules/asg/.header.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# asg

modules/asg/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
# asg
3+
4+
## Requirements
5+
6+
| Name | Version |
7+
|------|---------|
8+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.6.0 |
9+
10+
## Providers
11+
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.58.0 |
15+
16+
## Modules
17+
18+
No modules.
19+
20+
## Resources
21+
22+
| Name | Type |
23+
|------|------|
24+
| [aws_autoscaling_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group) | resource |
25+
| [aws_iam_instance_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
26+
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
27+
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
28+
| [aws_launch_template.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
29+
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
30+
31+
## Inputs
32+
33+
| Name | Description | Type | Default | Required |
34+
|------|-------------|------|---------|:--------:|
35+
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the ECS Cluster to associate with the Autoscaling Group | `string` | n/a | yes |
36+
| <a name="input_create_launch_template"></a> [create\_launch\_template](#input\_create\_launch\_template) | Either to create a Launch Template to associate with the Autoscaling group | `bool` | `true` | no |
37+
| <a name="input_desired_capacity"></a> [desired\_capacity](#input\_desired\_capacity) | Desired capacity for the Autoscaling group | `number` | n/a | yes |
38+
| <a name="input_iam_instance_profile_name"></a> [iam\_instance\_profile\_name](#input\_iam\_instance\_profile\_name) | Name of the IAM Instance Profile | `string` | `null` | no |
39+
| <a name="input_iam_instance_profile_tags"></a> [iam\_instance\_profile\_tags](#input\_iam\_instance\_profile\_tags) | Resource Tags for the IAM Instance Profile | `map(any)` | `{}` | no |
40+
| <a name="input_iam_role_name"></a> [iam\_role\_name](#input\_iam\_role\_name) | Name for the IAM Role | `string` | `null` | no |
41+
| <a name="input_iam_role_policy_attachments"></a> [iam\_role\_policy\_attachments](#input\_iam\_role\_policy\_attachments) | Policy ARNs to attach to the IAM Role | `list(string)` | <pre>[<br> "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"<br>]</pre> | no |
42+
| <a name="input_iam_role_tags"></a> [iam\_role\_tags](#input\_iam\_role\_tags) | Resource Tags for IAM Role | `map(any)` | `{}` | no |
43+
| <a name="input_instances_tags"></a> [instances\_tags](#input\_instances\_tags) | Resources Tags to propagate to the Instances | `map(any)` | `{}` | no |
44+
| <a name="input_launch_template"></a> [launch\_template](#input\_launch\_template) | Launch Template to use with the Autoscaling group | <pre>object({<br> name = optional(string, null)<br> image_id = optional(string, null)<br> instance_type = optional(string, null)<br> vpc_security_group_ids = optional(list(string), [])<br> key_name = optional(string, null)<br> user_data = optional(string, null)<br> tags = optional(map(any), {})<br> })</pre> | `{}` | no |
45+
| <a name="input_launch_template_id"></a> [launch\_template\_id](#input\_launch\_template\_id) | Identifier of the Launch Template | `string` | `null` | no |
46+
| <a name="input_max_size"></a> [max\_size](#input\_max\_size) | Max. size for the Autoscaling group | `number` | n/a | yes |
47+
| <a name="input_min_size"></a> [min\_size](#input\_min\_size) | Min. size for the Autoscaling group | `number` | n/a | yes |
48+
| <a name="input_name"></a> [name](#input\_name) | Name of the Autoscaling Group | `string` | n/a | yes |
49+
| <a name="input_tags"></a> [tags](#input\_tags) | Resources Tags for Autoscaling group | `map(any)` | `{}` | no |
50+
| <a name="input_vpc_zone_identifier"></a> [vpc\_zone\_identifier](#input\_vpc\_zone\_identifier) | Identifiers of the VPC Subnets | `list(string)` | n/a | yes |
51+
52+
## Outputs
53+
54+
| Name | Description |
55+
|------|-------------|
56+
| <a name="output_arn"></a> [arn](#output\_arn) | ARN of the Autoscaling group |
57+
| <a name="output_iam_instance_profile_arn"></a> [iam\_instance\_profile\_arn](#output\_iam\_instance\_profile\_arn) | ARN of the IAM Instance Profile |
58+
| <a name="output_iam_instance_profile_id"></a> [iam\_instance\_profile\_id](#output\_iam\_instance\_profile\_id) | Identifier of the IAM Instance Profile |
59+
| <a name="output_iam_role_id"></a> [iam\_role\_id](#output\_iam\_role\_id) | Identifier of the IAM Role |
60+
| <a name="output_id"></a> [id](#output\_id) | Identifier of the Autoscaling group |
61+
| <a name="output_launch_template_arn"></a> [launch\_template\_arn](#output\_launch\_template\_arn) | ARN of the Launch Template |
62+
| <a name="output_launch_template_id"></a> [launch\_template\_id](#output\_launch\_template\_id) | Identifier of the Launch Template |
63+
<!-- END_TF_DOCS -->

modules/asg/main.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
resource "aws_autoscaling_group" "this" {
2+
name = var.name
3+
vpc_zone_identifier = var.vpc_zone_identifier
4+
5+
desired_capacity = var.desired_capacity
6+
min_size = var.min_size
7+
max_size = var.max_size
8+
9+
protect_from_scale_in = false
10+
health_check_type = "EC2"
11+
12+
launch_template {
13+
id = var.create_launch_template ? aws_launch_template.this[0].id : var.launch_template_id
14+
}
15+
16+
dynamic "tag" {
17+
for_each = var.instances_tags
18+
iterator = tag
19+
20+
content {
21+
key = tag.key
22+
value = tag.value
23+
propagate_at_launch = true
24+
}
25+
}
26+
27+
dynamic "tag" {
28+
for_each = var.tags
29+
iterator = tag
30+
31+
content {
32+
key = tag.key
33+
value = tag.value
34+
propagate_at_launch = false
35+
}
36+
}
37+
}
38+
39+
################################################################################
40+
# Launch Template
41+
################################################################################
42+
43+
resource "aws_launch_template" "this" {
44+
count = var.create_launch_template ? 1 : 0
45+
46+
name = var.launch_template.name
47+
image_id = var.launch_template.image_id
48+
instance_type = var.launch_template.instance_type
49+
vpc_security_group_ids = var.launch_template.vpc_security_group_ids
50+
key_name = var.launch_template.key_name
51+
52+
iam_instance_profile {
53+
name = aws_iam_instance_profile.this.name
54+
}
55+
56+
user_data = base64encode(
57+
try(
58+
var.launch_template.user_data,
59+
<<-USERDATA
60+
#!/bin/bash
61+
echo ECS_CLUSTER="${var.cluster_name}" >> /etc/ecs/ecs.config
62+
USERDATA
63+
)
64+
)
65+
66+
tags = var.launch_template.tags
67+
}
68+
69+
################################################################################
70+
# IAM Instance Profile
71+
################################################################################
72+
73+
data "aws_iam_policy_document" "this" {
74+
statement {
75+
actions = ["sts:AssumeRole"]
76+
77+
principals {
78+
type = "Service"
79+
identifiers = ["ec2.amazonaws.com"]
80+
}
81+
}
82+
}
83+
84+
resource "aws_iam_role" "this" {
85+
name = var.iam_role_name
86+
assume_role_policy = data.aws_iam_policy_document.this.json
87+
88+
tags = var.iam_role_tags
89+
}
90+
91+
resource "aws_iam_role_policy_attachment" "this" {
92+
count = length(var.iam_role_policy_attachments)
93+
94+
role = aws_iam_role.this.name
95+
policy_arn = element(var.iam_role_policy_attachments, count.index)
96+
}
97+
98+
resource "aws_iam_instance_profile" "this" {
99+
name = var.iam_instance_profile_name
100+
role = aws_iam_role.this.name
101+
102+
tags = var.iam_instance_profile_tags
103+
}

modules/asg/outputs.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
output "id" {
2+
description = "Identifier of the Autoscaling group"
3+
value = aws_autoscaling_group.this.id
4+
}
5+
6+
output "arn" {
7+
description = "ARN of the Autoscaling group"
8+
value = aws_autoscaling_group.this.arn
9+
}
10+
11+
################################################################################
12+
# Launch Template
13+
################################################################################
14+
15+
output "launch_template_id" {
16+
description = "Identifier of the Launch Template"
17+
value = var.create_launch_template ? aws_launch_template.this[0].id : var.launch_template_id
18+
}
19+
20+
output "launch_template_arn" {
21+
description = "ARN of the Launch Template"
22+
value = var.create_launch_template ? aws_launch_template.this[0].arn : null
23+
}
24+
25+
################################################################################
26+
# IAM Instance Profile
27+
################################################################################
28+
29+
output "iam_role_id" {
30+
description = "Identifier of the IAM Role"
31+
value = aws_iam_role.this.id
32+
}
33+
34+
output "iam_instance_profile_id" {
35+
description = "Identifier of the IAM Instance Profile"
36+
value = aws_iam_instance_profile.this.id
37+
}
38+
39+
output "iam_instance_profile_arn" {
40+
description = "ARN of the IAM Instance Profile"
41+
value = aws_iam_instance_profile.this.arn
42+
}

modules/asg/variables.tf

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
variable "cluster_name" {
2+
description = "Name of the ECS Cluster to associate with the Autoscaling Group"
3+
type = string
4+
}
5+
6+
variable "name" {
7+
description = "Name of the Autoscaling Group"
8+
type = string
9+
}
10+
11+
variable "vpc_zone_identifier" {
12+
description = "Identifiers of the VPC Subnets"
13+
type = list(string)
14+
}
15+
16+
variable "desired_capacity" {
17+
description = "Desired capacity for the Autoscaling group"
18+
type = number
19+
}
20+
21+
variable "min_size" {
22+
description = "Min. size for the Autoscaling group"
23+
type = number
24+
}
25+
26+
variable "max_size" {
27+
description = "Max. size for the Autoscaling group"
28+
type = number
29+
}
30+
31+
variable "instances_tags" {
32+
description = "Resources Tags to propagate to the Instances"
33+
type = map(any)
34+
default = {}
35+
}
36+
37+
variable "tags" {
38+
description = "Resources Tags for Autoscaling group"
39+
type = map(any)
40+
default = {}
41+
}
42+
43+
################################################################################
44+
# Launch Template
45+
################################################################################
46+
47+
variable "launch_template" {
48+
description = "Launch Template to use with the Autoscaling group"
49+
type = object({
50+
name = optional(string, null)
51+
image_id = optional(string, null)
52+
instance_type = optional(string, null)
53+
vpc_security_group_ids = optional(list(string), [])
54+
key_name = optional(string, null)
55+
user_data = optional(string, null)
56+
tags = optional(map(any), {})
57+
})
58+
default = {}
59+
}
60+
61+
variable "create_launch_template" {
62+
description = "Either to create a Launch Template to associate with the Autoscaling group"
63+
type = bool
64+
default = true
65+
}
66+
67+
variable "launch_template_id" {
68+
description = "Identifier of the Launch Template"
69+
type = string
70+
default = null
71+
}
72+
73+
################################################################################
74+
# IAM Role
75+
################################################################################
76+
77+
variable "iam_role_name" {
78+
description = "Name for the IAM Role"
79+
type = string
80+
default = null
81+
}
82+
83+
variable "iam_role_policy_attachments" {
84+
description = "Policy ARNs to attach to the IAM Role"
85+
type = list(string)
86+
default = [
87+
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
88+
]
89+
}
90+
91+
variable "iam_role_tags" {
92+
description = "Resource Tags for IAM Role"
93+
type = map(any)
94+
default = {}
95+
}
96+
97+
################################################################################
98+
# IAM Instance Profile
99+
################################################################################
100+
101+
variable "iam_instance_profile_name" {
102+
description = "Name of the IAM Instance Profile"
103+
type = string
104+
default = null
105+
}
106+
107+
variable "iam_instance_profile_tags" {
108+
description = "Resource Tags for the IAM Instance Profile"
109+
type = map(any)
110+
default = {}
111+
}

modules/asg/version.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 1.6.0"
3+
}

0 commit comments

Comments
 (0)