SourceFuse's AWS Reference Architecture Terraform module leverages the terraform-aws-modules/terraform-aws-arc-ec2-autoscale-group GitHub repository to enable streamlined provisioning and management of EC2 Auto Scaling Groups. This module facilitates dynamic scaling of EC2 instances based on demand, ensuring high availability and cost efficiency for workloads. It supports customizable launch templates, instance profiles, and scaling policies, allowing flexible configuration tailored to diverse application needs. Integrated with CloudWatch monitoring and load balancing options, the module ensures resilient and performant infrastructure on AWS.
Before using this module, ensure you have the following:
- AWS credentials configured.
- Terraform installed.
- A working knowledge of Terraform.
- Network
- Define the Module
Initially, it's essential to define a Terraform module, which is organized as a distinct directory encompassing Terraform configuration files. Within this module directory, input variables and output values must be defined in the variables.tf and outputs.tf files, respectively. The following illustrates an example directory structure:
asg/
|-- main.tf
|-- variables.tf
|-- outputs.tf
- Define Input Variables
Inside the variables.tf
or in *.tfvars
file, you should define values for the variables that the module requires.
- Use the Module in Your Main Configuration In your main Terraform configuration file (e.g., main.tf), you can use the module. Specify the source of the module, and version, For Example
module "asg" {
source = "sourcefuse/arc-ec2-autoscale-group/aws"
version = "0.0.1"
launch_template = local.launch_template
asg = local.asg_config
security_group_data = local.security_group_data
security_group_name = local.security_group_name
vpc_id = data.aws_vpc.default.id
autoscaling_notification_enabled = local.autoscaling_notification_enabled
autoscaling_notification_types = local.autoscaling_notification_types
autoscaling_sns_topic_arn = local.autoscaling_sns_topic_arn
schedules = local.schedules
autoscaling_policy = local.autoscaling_policy
predictive_scaling_configuration = local.predictive_scaling_configuration
create_autoscaling_attachment = local.create_autoscaling_attachment
autoscaling_attachments = local.autoscaling_attachments
instance_profile_name = local.instance_profile_name
tags = module.tags.tags
}
- Output Values
Inside the outputs.tf
file of the module, you can define output values that can be referenced in the main configuration. For example:
output "name" {
description = "Name of the Auto Scaling Group"
value = module.asg.name
}
output "asg_arn" {
description = "ARN of the Auto Scaling Group"
value = module.asg.asg_arn
}
- Execute Terraform Commands
After defining your main configuration, navigate to the directory containing your Terraform files and run the following commands:
terraform init
terraform apply
- Review and Confirm
Terraform will display a plan showing the changes it intends to make. Review the plan and confirm by typing 'yes' when prompted.
Name | Version |
---|---|
terraform | >= 1.5.0 |
aws | ~> 5.85 |
Name | Version |
---|---|
aws | 5.96.0 |
Name | Source | Version |
---|---|---|
arc_security_group | sourcefuse/arc-security-group/aws | 0.0.1 |
Name | Type |
---|---|
aws_autoscaling_attachment.this | resource |
aws_autoscaling_group.this | resource |
aws_autoscaling_notification.this | resource |
aws_autoscaling_policy.this | resource |
aws_autoscaling_schedule.this | resource |
aws_autoscaling_traffic_source_attachment.this | resource |
aws_iam_instance_profile.this | resource |
aws_iam_role.this | resource |
aws_launch_template.this | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
asg | Configuration map for Auto Scaling Group | object({ |
n/a | yes |
autoscaling_attachments | Map of autoscaling attachment configurations | map(object({ |
{} |
no |
autoscaling_notification_enabled | Boolean flag to enable or disable autoscaling notifications | bool |
false |
no |
autoscaling_notification_types | List of notification types for the Auto Scaling group | list(string) |
[ |
no |
autoscaling_policy | Configuration for the autoscaling policy | object({ |
{} |
no |
autoscaling_sns_topic_arn | ARN of the SNS topic to send Auto Scaling notifications to | string |
n/a | yes |
create_autoscaling_attachment | Whether to create autoscaling attachments | bool |
false |
no |
create_traffic_source_attachment | Whether to create traffic source attachment for the auto scaling group | bool |
false |
no |
instance_profile_name | The name of the IAM instance profile | string |
"asg-instance-profile" |
no |
launch_template | Configuration for the EC2 Launch Template | object({ |
n/a | yes |
predictive_scaling_configuration | Predictive scaling configuration | object({ |
null |
no |
schedules | List of Auto Scaling schedules | list(object({ |
[] |
no |
security_group_data | (optional) Security Group data | object({ |
{ |
no |
security_group_name | alb security group name | string |
n/a | yes |
security_groups | n/a | list(string) |
[] |
no |
tags | Tags to apply to resources | map(string) |
{ |
no |
traffic_sources | List of traffic sources to attach to the Auto Scaling group | list(object({ |
[] |
no |
vpc_id | The VPC ID for the resources | string |
n/a | yes |
Name | Description |
---|---|
asg_arn | ARN of the Auto Scaling Group |
asg_id | ID of the Auto Scaling Group |
availability_zones | Availability Zones used by the Auto Scaling Group |
desired_capacity | Desired capacity of the Auto Scaling Group |
enabled_metrics | List of enabled metrics for the ASG |
iam_instance_profile_arn | ARN of the IAM instance profile |
iam_role_name | The name of the IAM role attached to the instance profile |
launch_template_arn | The ARN of the launch template |
launch_template_default_version | The default version of the launch template |
launch_template_id | The ID of the launch template |
launch_template_latest_version | The latest version of the launch template |
launch_template_name | The name of the launch template |
name | Name of the Auto Scaling Group |
This project uses a .version
file at the root of the repo which the pipeline reads from and does a git tag.
When you intend to commit to main
, you will need to increment this version. Once the project is merged,
the pipeline will kick off and tag the latest git commit.
- Configure pre-commit hooks
pre-commit install
while Contributing or doing git commit please specify the breaking change in your commit message whether its major,minor or patch
For Example
git commit -m "your commit message #major"
By specifying this , it will bump the version and if you don't specify this in your commit message then by default it will consider patch and will bump that accordingly
- Tests are available in
test
directory - Configure the dependencies
cd test/ go mod init github.com/sourcefuse/terraform-aws-refarch-ec2-autoscale-group go get github.com/gruntwork-io/terratest/modules/terraform
- Now execute the test
go test -timeout 30m
This project is authored by:
- SourceFuse ARC Team