Terraform correlation to resource with for_each

Viewed 2709

I'm using below code to assign defualt subnets to ASG

resource "aws_autoscaling_group" "ecs_spot_asg" {
  for_each = data.aws_subnet_ids.all_subnets.ids
.... etc...

Subnets done via

data "aws_subnet_ids" "all_subnets" {
  vpc_id = data.aws_vpc.default.id
}

Below I have aws_autoscaling_policy and I'm stuck on how to relate one to the other

resource "aws_autoscaling_policy" "ecs_cluster_scale_policy" {
  autoscaling_group_name = aws_autoscaling_group.ecs_spot_asg.name

Getting error:

Because aws_autoscaling_group.ecs_spot_asg has "for_each" set, its attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use: aws_autoscaling_group.ecs_spot_asg[each.key]

How this should be modified ?

1 Answers

My mistake was adding [] to vpc_zone_identifier = data.aws_subnet_ids.all_subnets.ids

So instead of vpc_zone_identifier = [data.aws_subnet_ids.all_subnets.ids] it should be vpc_zone_identifier = data.aws_subnet_ids.all_subnets.ids

Related