traveloka / terraform-aws-autoscaling Goto Github PK
View Code? Open in Web Editor NEWA terraform module which provisions an auto scaling group along with its launch template
License: Apache License 2.0
A terraform module which provisions an auto scaling group along with its launch template
License: Apache License 2.0
using slow_start
parameter on aws alb terraform produce huge traffic degradation on asg, since our green environment hasn't yet to be ready for serving actual traffic, whereas blue environment has been terminated. Means, all the traffic will goes to canary cluster (in which 1 instance only)
Affected :
New :
resource "time_sleep" "wait_before_destroy" {
destroy_duration = var.asg_time_sleep_before_destroy
triggers = {
asg_id = aws_autoscaling_group.main.id
}
lifecycle {
create_before_destroy = true
}
}
provider "aws" {
region = "ap-southeast-1"
}
module "asg" {
source = "../.."
service_name = "fprbe"
environment = "production"
product_domain = "fpr"
description = "Instances of fprbe-app"
application = "java-8"
security_groups = []
instance_profile = "myinstance-profile"
cpu_credits = "standard"
image_owners = ["123456789012"]
image_filters = [
# See https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html for complete filter options
{
name = "name"
values = ["traveloka-fprbe-app-*"]
},
# If you want to directly specify the image ID
{
name = "image-id"
values = ["ami-91920591023019"]
},
]
launch_template_overrides = [
{
"instance_type" = "m5.large"
},
{
"instance_type" = "m4.large"
},
]
user_data = "echo starting fprbe"
key_name = ""
asg_vpc_zone_identifier = ["subnet-a2b50c9d", "subnet-718c9efe"]
asg_lb_target_group_arns = []
asg_wait_for_capacity_timeout = "1m"
}
Error: Error creating AutoScaling Group: InvalidQueryParameter: Invalid launch template: When a network interface is provided, the security groups must be a part of it.
status code: 400, request id: 229df5f1-621b-4c53-af50-e27106d772e2
on .terraform/modules/asg/main.tf line 100, in resource "aws_autoscaling_group" "main":
100: resource "aws_autoscaling_group" "main" {
Similiar Issue: hashicorp/terraform-provider-aws#4570
Aws Docs: https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-launch-template.html
v0.3.1
Terraform v0.12.26
+ provider.aws v2.68
same as examples/simple
terraform apply successfuly
terraform apply return below error
Error: Error creating AutoScaling Group: InvalidQueryParameter: Invalid launch template: When a network interface is provided, the security groups must be a part of it.
status code: 400, request id: 229df5f1-621b-4c53-af50-e27106d772e2
on .terraform/modules/asg/main.tf line 100, in resource "aws_autoscaling_group" "main":
100: resource "aws_autoscaling_group" "main" {
terraform apply
Terraform v0.12.6
+ provider.aws v2.22.0
provider "aws" {
region = "ap-southeast-1"
}
module "asg" {
source = "../.."
service_name = "fprbe"
environment = "production"
product_domain = "fpr"
description = "Instances of fprbe-app"
application = "java-8"
security_groups = []
instance_profile = "myinstance-profile"
image_owners = ["123456789012"]
image_filters = [
# See https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html for complete filter options
{
name = "name"
values = ["traveloka-fprbe-app-*"]
},
# If you want to directly specify the image ID
{
name = "image-id"
values = ["ami-91920591023019"]
},
]
instance_type = "m5.large"
user_data = "echo starting fprbe"
key_name = ""
asg_vpc_zone_identifier = ["subnet-a2b50c9d", "subnet-718c9efe"]
asg_lb_target_group_arns = []
asg_wait_for_capacity_timeout = "1m"
}
Terraform has been successfully initialized!
There are some problems with the configuration, described below.
The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
Error: Invalid argument name
on .terraform/modules/asg.asg_name/main.tf line 32, in resource "null_resource" "unsupported_resource_type":
32: "\n\nCurrently supported resource types: \n* ${join("\n* ", keys(local.max_character_length))}" = true
Argument names must not be quoted.
Error: Invalid argument name
on .terraform/modules/asg.launch_template_name/main.tf line 32, in resource "null_resource" "unsupported_resource_type":
32: "\n\nCurrently supported resource types: \n* ${join("\n* ", keys(local.max_character_length))}" = true
cd example/simple/
terraform init
The issue fixed in traveloka/terraform-aws-resource-naming@d6f091a (v0.17.0)
When i used this module to lookup AMI in same account, it thrown this error
Error: Error refreshing state: 1 error occurred:
* module.asg.data.aws_ami.latest_service_image: 1 error occurred:
* module.asg.data.aws_ami.latest_service_image: data.aws_ami.latest_service_image: Your query returned no results. Please change your search criteria and try again.
Terraform v0.11.14
+ provider.aws v2.22.0
+ provider.null v2.1.2
+ provider.random v2.1.2
+ provider.template v2.1.2
image_owners = ["${data.aws_caller_identity.current.account_id}"]
image_filters = [
{
name = "image-id"
values = ["${var.ami_id}"]
},
]
query returned an AMI ID.
query returned no results.
terraform plan
resource "aws_launch_template" "main" {}
# Copy-paste your Terraform configurations here.
# For large Terraform configs, please use a service like Google Drive and share a link to the ZIP file.
network_interfaces {
associate_public_ip_address = "${var.associate_public_ip}"
}
variable "associate_public_ip" {
description = "Whether to associate public IP to the instance"
default = "false"
}
Current terraform code (release/v0.2.x)
doesn't has additional_tags
, it's only has asg_tags
which is only can tag ASG and EC2 resources. With asg_tags
we can't add more custom tags for ebs volume.
I need an additional_tags
to add Team
tag to ebs volume resources from this module, so I will have a better cost attribution for volume across sub product domain in fpr
. LaunchTemplate
also will get benefit from additional_tags
.
tags = "${merge(var.additional_tags, map(
"Name", module.launch_template_name.name,
"Service", var.service_name,
"ProductDomain", var.product_domain,
"Environment", var.environment,
"ManagedBy", "terraform",
))}"
tag_specifications = [
{
resource_type = "instance"
tags = "${merge(var.additional_tags, map(
"Name", "${var.service_name}-${var.cluster_role}",
"Service", var.service_name,
"Cluster", "${var.service_name}-${var.cluster_role}",
"ProductDomain", var.product_domain,
"Application", var.application,
"Environment", var.environment,
"Description", var.description,
"ManagedBy", "terraform"
))}"
},
{
resource_type = "volume"
tags = "${merge(var.additional_tags, map(
"Service", var.service_name,
"ProductDomain", var.product_domain,
"Environment", var.environment,
"ManagedBy", "terraform"
))}"
},
]
When adding capability for associate public IP (#34), we use network_interfaces
block in the aws_launch_template
. This block has default value false
for delete_on_termination
. It causes the ENI isn't deleted when we destroy the launch template and makes the security group attached to it can't be deleted.
v0.3.2
Terraform v0.12.29
+ provider.aws v2.68.0
resource "aws_launch_template" "main" {
name = module.launch_template_name.name
image_id = data.aws_ami.latest_service_image.id
instance_type = var.use_mixed_instances_policy == true ? null : var.launch_template_overrides[0].instance_type
iam_instance_profile {
name = var.instance_profile_name
}
credit_specification {
cpu_credits = var.cpu_credits
}
key_name = var.key_name
user_data = base64encode(var.user_data)
network_interfaces {
associate_public_ip_address = var.associate_public_ip
security_groups = var.security_groups
delete_on_termination = var.delete_network_interface_on_termination
}
monitoring {
enabled = var.monitoring
}
ebs_optimized = var.ebs_optimized
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = var.volume_size
volume_type = var.volume_type
delete_on_termination = var.delete_on_termination
encrypted = var.ebs_encryption
}
}
tags = {
Name = module.launch_template_name.name
Service = var.service_name
ProductDomain = var.product_domain
Environment = var.environment
ManagedBy = "terraform"
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.service_name}-${var.cluster_role}"
Service = var.service_name
Cluster = "${var.service_name}-${var.cluster_role}"
ProductDomain = var.product_domain
Application = var.application
Environment = var.environment
Description = var.description
ManagedBy = "terraform"
}
}
tag_specifications {
resource_type = "volume"
tags = {
Service = var.service_name
ProductDomain = var.product_domain
Environment = var.environment
ManagedBy = "terraform"
}
}
}
When we destroy the cluster, we can destroy the security group attached to it
We can't destroy the security group because it's still attached to the ENI
terraform apply
terraform destroy
Current version did not have support for asg desired capacity that is supported on 0.2.8. We need this since on release normally we need additional instances before gradually move to min baseline
desired_capacity = var.asg_desired_capacity
I got this error when applying this module. But when i re-apply the code, it create resources successfully
module.aws-autoscaling_app.aws_autoscaling_group.main: aws_autoscaling_group.main: diffs didn't match during apply. This is a bug with Terraform and should be reported as a GitHub Issue.
Please include the following information in your report:
Terraform Version: 0.11.14
Resource ID: aws_autoscaling_group.main
Mismatch reason: extra attributes: tags.126663611.key, tags.126663611.%, tags.126663611.propagate_at_launch, tags.126663611.value
I think this one caused by the tag "Name" that lookup from another module as its value.
terraform-aws-autoscaling/main.tf
Lines 125 to 129 in 6556e79
v0.2.3
$ terraform -v
Terraform v0.11.14
+ provider.aws v2.70.0
+ provider.null v1.0.0
+ provider.random v1.2.0
+ provider.template v2.1.2
module "aws-autoscaling_app" {
source = "traveloka/autoscaling/aws"
version = "0.2.3"
service_name = "${local.service_name}"
cluster_role = "${local.service_role}"
environment = "${var.environment}"
application = "${var.application}"
product_domain = "${local.product_domain}"
description = "${local.service_description}"
asg_vpc_zone_identifier = [
"${data.aws_subnet_ids.subnet_app_ids.ids}",
]
asg_lb_target_group_arns = [
"${module.alb-single-listener_lbint.tg_arn}",
]
asg_min_capacity = "${var.asg_min_capacity}"
asg_max_capacity = "${var.asg_max_capacity}"
asg_health_check_type = "${var.asg_health_check_type}"
asg_health_check_grace_period = "${var.asg_health_check_grace_period}"
asg_default_cooldown = "${var.asg_default_cooldown}"
asg_wait_for_capacity_timeout = "${var.asg_wait_for_capacity_timeout}"
security_groups = [
"${aws_security_group.payepg-app.id}",
"${var.asg_additional_security_groups}",
]
instance_profile_name = "${module.aws-iam-role_app.instance_profile_name}"
user_data = "${var.asg_user_data}"
image_owners = "${var.asg_image_owner_ids}"
mixed_instances_distribution = "${var.asg_mixed_instances_distribution}"
image_filters = "${local.ami_search_filter}"
launch_template_overrides = "${var.asg_launch_template_override}"
asg_tags = [
{
key = "AmiId"
value = "${var.asg_ami_id}"
propagate_at_launch = true
},
{
key = "ServiceVersion"
value = "${local.app_service_version}"
propagate_at_launch = true
},
]
}
Resources created at first attempt.
Got error at first attemp, and need to re-execute terraform apply to provision the resources.
terraform apply
=> errorterraform apply
=> successAt first attempt, the plan output look like this (only for ASG tags)
tags.#: "7"
tags.0.%: <computed>
tags.1141493681.%: "3"
tags.1141493681.key: "Environment"
tags.1141493681.propagate_at_launch: "0"
tags.1141493681.value: "staging"
tags.1549330624.%: "3"
tags.1549330624.key: "ManagedBy"
tags.1549330624.propagate_at_launch: "0"
tags.1549330624.value: "terraform"
tags.1627912306.%: "3"
tags.1627912306.key: "AmiId"
tags.1627912306.propagate_at_launch: "1"
tags.1627912306.value: "ami-02959f2ed559ac605"
tags.2091551781.%: "3"
tags.2091551781.key: "Service"
tags.2091551781.propagate_at_launch: "0"
tags.2091551781.value: "payepg"
tags.2209837006.%: "3"
tags.2209837006.key: "ProductDomain"
tags.2209837006.propagate_at_launch: "0"
tags.2209837006.value: "pay"
tags.2664787174.%: "3"
tags.2664787174.key: "Description"
tags.2664787174.propagate_at_launch: "0"
tags.2664787174.value: "ASG of the payepg-app cluster"
I try to apply naming module first
terraform apply --target=module.aws-autoscaling_app.module.asg_name.random_id.this
After naming module created, the plan output looks different
tags.#: "7"
tags.1141493681.%: "3"
tags.1141493681.key: "Environment"
tags.1141493681.propagate_at_launch: "0"
tags.1141493681.value: "staging"
tags.1549330624.%: "3"
tags.1549330624.key: "ManagedBy"
tags.1549330624.propagate_at_launch: "0"
tags.1549330624.value: "terraform"
tags.1627912306.%: "3"
tags.1627912306.key: "AmiId"
tags.1627912306.propagate_at_launch: "1"
tags.1627912306.value: "ami-02959f2ed559ac605"
tags.2091551781.%: "3"
tags.2091551781.key: "Service"
tags.2091551781.propagate_at_launch: "0"
tags.2091551781.value: "payepg"
tags.2209837006.%: "3"
tags.2209837006.key: "ProductDomain"
tags.2209837006.propagate_at_launch: "0"
tags.2209837006.value: "pay"
tags.2625938796.%: "3"
tags.2625938796.key: "Name" ==> added after module naming created
tags.2625938796.propagate_at_launch: "0" ==> added after module naming created
tags.2625938796.value: "payepg-app-527ccb7255b9e158" ==> added after module naming created
tags.2664787174.%: "3"
tags.2664787174.key: "Description"
tags.2664787174.propagate_at_launch: "0"
tags.2664787174.value: "ASG of the payepg-app cluster"
In order to increase collaboration and contribution on the development of this module, it is a good idea to start with enhancing the README file. We do have a template repository which contains several files that will become a standard: https://github.com/traveloka/terraform-aws-modules-template
On that repository you will find a README file that can be used as an example. What points should be written on the README.
The example README was created by @Rukaan and reviewed by @rafikurnia, @sumodirjo, and also other @traveloka/site-infra members. See traveloka/terraform-aws-modules-template#9 for more information.
AMI That is being created using the Amazon Linux 2 as a base Image failed to boot.
0.3.8
Terraform v1.0.10 linux_amd64
+ provider.aws v3.64.2 linux_amd64
# Copy-paste your Terraform configurations here.
# For large Terraform configs, please use a service like Google Drive and share a link to the ZIP file.
terraform apply
<any_command_you_executed>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.