简体   繁体   中英

Add tag to launch template for ec2 nodes

I am trying to add tags to a launch template so that the ec2 nodes are tagged and named..

When I add the hardcoded tags inside the module it works, but the idea is to have dynamic tags and be able to merge with the local ones.

module
resource "aws_autoscaling_group" "ecs_asg" {
  name                      = var.name_asg
  max_size                  = var.max_size
  min_size                  = var.min_size
  .
  .
  .
  service_linked_role_arn   = var.service_linked_role_arn
  tags = var.asg_tags
  launch_template {
    id      = aws_launch_template.launch_template.id
    version = "$Latest"
  }
}
variables.tf
variable "asg_tags" {
  type = map(string)
  default = {}
}

main.tf

  name_asg                  = "leo-nombre-asg"
  max_size                  = var.max_size
  min_size                  = var.min_size
  .
  .
  .
  asg_tags = merge(
    local.tags,
    {
      propagate_at_launch=true,
    },
  )

locals.tf
locals {
       tags = {
        "Accountable" = "business"
        "Deploy"      = "terraform"
        "Role"        = "services"
    }
}

terraform validate
│ Error: Incorrect attribute value type
│ 
│   on modules\ecs\main.tf line 38, in resource "aws_autoscaling_group" "ecs_asg":
│   38:   tags = var.asg_tags
│     ├────────────────
│     │ var.asg_tags is a map of string
│ 
│ Inappropriate value for attribute "tags": set of map of string required.

The two fixes necessary here are both for the type in the asg_tags parameter argument value:

asg_tags = [merge(local.tags, { "propagate_at_launch" = "true" })]

Here we use the list/set constructor to cast the type set(map(string)) . Terraform will coerce to set instead of list with the constructor as long as the type is specified to be set instead. Since we need to fix the type declaration anyway to be compatible with the resource attribute schema, this is convenient to do:

variable "asg_tags" {
  type = set(map(string))
  default = {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM