terraform-aws-autoscaling icon indicating copy to clipboard operation
terraform-aws-autoscaling copied to clipboard

Creating IAM instance profile: The given key does not identify an element in this collection value: the collection has no elements.

Open allexivan opened this issue 1 year ago β€’ 3 comments

Description

When setting create_iam_instance_profile, the module fails.

If your request is for a new feature, please use the Feature request template.

  • [x] βœ‹ I have searched the open/closed issues and my issue is not listed.

Versions

  • Module version [Required]: 7.4.1

  • Terraform version: v1.7.4

  • Provider version(s):

  • provider registry.terraform.io/hashicorp/aws v5.48.0
  • provider registry.terraform.io/hashicorp/null v3.2.2
  • provider registry.terraform.io/hashicorp/random v3.6.2

Reproduction Code [Required]

data "aws_ssm_parameter" "ecs_optimized_ami" {
  name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
}

module "autoscaling" {
  source  = "terraform-aws-modules/autoscaling/aws"
  version = "~> 7.4.1"

  create                 = var.asg_create
  create_launch_template = var.asg_create_launch_template

  for_each = {
    # On-demand instances
    asg_1 = {
      instance_type              = var.asg_instance_type
      use_mixed_instances_policy = false
      mixed_instances_policy     = {}
      user_data                  = <<-EOT
        #!/bin/bash

        cat <<'EOF' >> /etc/ecs/ecs.config
        ECS_CLUSTER=${module.ecs_cluster.name}
        ECS_LOGLEVEL=debug
        ECS_CONTAINER_INSTANCE_TAGS=${jsonencode(var.resource_tags)}
        ECS_ENABLE_TASK_IAM_ROLE=true
        EOF
      EOT
    }
  }

  name                            = "${var.prefix}-${var.environment}-${var.cluster_name}-${each.key}"
  image_id                        = jsondecode(data.aws_ssm_parameter.ecs_optimized_ami.value)["image_id"]
  instance_type                   = each.value.instance_type
  enable_monitoring               = true
  security_groups                 = var.asg_security_groups
  user_data                       = base64encode(each.value.user_data)
  ignore_desired_capacity_changes = true
  create_iam_instance_profile     = true
  iam_role_name                   = "${var.prefix}-${var.environment}-${var.cluster_name}-asg-role"
  iam_role_description            = "ECS ASG role for ${var.prefix}-${var.environment}-${var.cluster_name}"
  iam_role_tags                   = merge(var.tags, var.resource_tags)
  iam_role_policies = {
    AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
    AmazonSSMManagedInstanceCore        = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
  }
  vpc_zone_identifier = var.asg_private_subnets
  health_check_type   = "EC2"
  min_size            = var.asg_min_size
  max_size            = var.asg_max_size
  desired_capacity    = var.asg_desired_capacity

  # https://github.com/hashicorp/terraform-provider-aws/issues/12582
  autoscaling_group_tags = {
    AmazonECSManaged = true
  }

  # Required for  managed_termination_protection = "ENABLED"
  protect_from_scale_in = false

  # Spot instances
  use_mixed_instances_policy = each.value.use_mixed_instances_policy
  mixed_instances_policy     = each.value.mixed_instances_policy

  tags = merge(var.tags, var.resource_tags)
}

vars:

    asg_create                            = true
    asg_create_launch_template            = true
    asg_instance_type                     = "t2.micro"
    asg_min_size                          = 0
    asg_max_size                          = 10
    asg_desired_capacity                  = 0
    asg_security_groups                   = ["asg-sg"]

Steps to reproduce the behavior:

Expected behavior

To create IAM Instance role

Actual behavior

β”‚ Error: Invalid index
β”‚ 
β”‚   on .terraform/modules/ecs_cluster.autoscaling/main.tf line 22, in locals:
β”‚   22:   iam_instance_profile_arn  = var.create_iam_instance_profile ? aws_iam_instance_profile.this[0].arn : var.iam_instance_profile_arn
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ aws_iam_instance_profile.this is empty tuple
β”‚ 
β”‚ The given key does not identify an element in this collection value: the collection has no elements.
β•΅
β•·
β”‚ Error: Invalid index
β”‚ 
β”‚   on .terraform/modules/ecs_cluster.autoscaling/main.tf line 22, in locals:
β”‚   22:   iam_instance_profile_arn  = var.create_iam_instance_profile ? aws_iam_instance_profile.this[0].arn : var.iam_instance_profile_arn
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ aws_iam_instance_profile.this is empty tuple
β”‚ 
β”‚ The given key does not identify an element in this collection value: the collection has no elements.

allexivan avatar May 28 '24 10:05 allexivan

just on a cursory glance, your code looks incorrect with the for_each loop. I would recommend removing this

bryantbiggs avatar May 28 '24 11:05 bryantbiggs

@bryantbiggs Yes, probably. It was from example from here:

https://github.com/terraform-aws-modules/terraform-aws-ecs/blob/84cf54a603c9d976a150ec6aea4ab63eb0efd773/examples/ec2-autoscaling/main.tf#L237

Anyway, it works if I create the instance profile outside of the module and link it with iam_instance_profile_arn = aws_iam_instance_profile.ssm

I will test it without for_each

allexivan avatar May 28 '24 11:05 allexivan

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] avatar Jun 28 '24 00:06 github-actions[bot]

This issue was automatically closed because of stale in 10 days

github-actions[bot] avatar Jul 08 '24 00:07 github-actions[bot]

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Aug 07 '24 02:08 github-actions[bot]