When an EC2 instance launched using Launch configuration of AutoScaling Group(ASG). It will tag only EC2 instances but not EBS volumes attached to instances. Look like launch configuration is not supporting tagging EBS volumes and ASG propagate tags to only EC2 instance.
If you want to tag both EC2 instance and attached EBS volume then use Launch Template resource and link it to ASG.
Below given sample terraform code:
## Launch template
resource "aws_launch_template" "app_launch_template" {
name_prefix = "app-template-prod"
image_id = "<ami-id>"
instance_type = "<instance-type>"
vpc_security_group_ids = ["<Securitygroups-ids>"]
iam_instance_profile {
name ="<instance-profile>"
}
user_data = "${base64encode(data.template_file.app-user_data.rendered)}"
tag_specifications {
resource_type = "instance"
tags = "${merge(
module.global.resource_tag,
map(
"Name", "app-${module.global.env_workspace}"
)
)}"
}
tag_specifications {
resource_type = "volume"
tags = "${merge(
module.global.resource_tag,
map(
"Name", "app-${module.global.env_workspace}-ebs"
)
)}"
}
tags = "${merge(
module.global.resource_tag,
map(
"Name", "app-${module.global.env_workspace}"
)
)}"
}
## ASG
resource "aws_autoscaling_group" "app_asg" {
name = "app-${module.global.env_workspace}-asg"
launch_template = {
id = "${aws_launch_template.app_launch_template.id}"
}
}
If you want to tag both EC2 instance and attached EBS volume then use Launch Template resource and link it to ASG.
Below given sample terraform code:
## Launch template
resource "aws_launch_template" "app_launch_template" {
name_prefix = "app-template-prod"
image_id = "<ami-id>"
instance_type = "<instance-type>"
vpc_security_group_ids = ["<Securitygroups-ids>"]
iam_instance_profile {
name ="<instance-profile>"
}
user_data = "${base64encode(data.template_file.app-user_data.rendered)}"
tag_specifications {
resource_type = "instance"
tags = "${merge(
module.global.resource_tag,
map(
"Name", "app-${module.global.env_workspace}"
)
)}"
}
tag_specifications {
resource_type = "volume"
tags = "${merge(
module.global.resource_tag,
map(
"Name", "app-${module.global.env_workspace}-ebs"
)
)}"
}
tags = "${merge(
module.global.resource_tag,
map(
"Name", "app-${module.global.env_workspace}"
)
)}"
}
## ASG
resource "aws_autoscaling_group" "app_asg" {
name = "app-${module.global.env_workspace}-asg"
launch_template = {
id = "${aws_launch_template.app_launch_template.id}"
}
}