Skip to content

instance_profile error with invalid count on first apply #194

@wesleung

Description

@wesleung

Describe the Bug

Input instance_profile will throw the below error on apply

│ Error: Invalid count argument
│ 
│   on .terraform/modules/instance/main.tf line 85, in data "aws_iam_instance_profile" "given":
│   85:   count = local.enabled && var.instance_profile_enabled && var.instance_profile != "" ? 1 : 0
│ 
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work
│ around this, use the -target argument to first apply only the resources that the count depends on.
╵
╷
│ Error: Invalid count argument
│ 
│   on .terraform/modules/instance/main.tf line 96, in resource "aws_iam_role" "default":
│   96:   count                = var.instance_profile_enabled ? local.instance_profile_count : 0
│ 
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work
│ around this, use the -target argument to first apply only the resources that the count depends on.

Expected Behavior

Expected behavior is that the instance will use the precreated instance profile

Steps to Reproduce

resource "aws_iam_role" "default" {
  depends_on = [aws_iam_policy.default]
  name = "test-role"
  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "ec2.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}

resource "aws_iam_policy" "default" {
  name        = "test-policy"
  path        = "/"
  description = "IAM  Policy"

  policy = jsonencode({
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "s3:ListBucket",
            "Resource": *,
            "Effect": "Allow"
        },
        {
            "Action": [
                "s3:PutObject*",
                "s3:GetObject*",
                "s3:DeleteObject*"
            ],
            "Resource": *,
            "Effect": "Allow"
        }
    ]
})
}

resource "aws_iam_role_policy_attachment" "default" {
  depends_on = [aws_iam_policy.default, aws_iam_role.default ]
  policy_arn = aws_iam_policy.default.arn
  role       = aws_iam_role.default.name
}

resource "aws_iam_role_policy_attachment" "ssm_attach" {
  role       = aws_iam_role.default.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

resource "aws_iam_instance_profile" "default" {
  name        = "${var.aws_profile}-${local.service}"
  role        = aws_iam_role.default.name
  depends_on  = [aws_iam_policy.default, aws_iam_role.default, aws_iam_role_policy_attachment.default ]
}

Module

module "instance" {
  source  = "cloudposse/ec2-instance/aws"
  version = "1.4.0"

  ssh_key_pair                = var.ssh_key_pair
  instance_type               = var.instance_type
  instance_profile      = aws_iam_instance_profile.default.id
  vpc_id                      = var.vpc_id
  security_groups             = var.security_groups
  subnet                      = var.subnet
  name                        = "ec2"
}

Will throw the earlier Error "Invalid count argument"
Comment instance_profile and it will work

module "instance" {
  source  = "cloudposse/ec2-instance/aws"
  version = "1.4.0"

  ssh_key_pair                = var.ssh_key_pair
  instance_type               = var.instance_type
  #instance_profile      = aws_iam_instance_profile.default.id
  vpc_id                      = var.vpc_id
  security_groups             = var.security_groups
  subnet                      = var.subnet
  name                        = "ec2"
}

After initial apply, you can then un-comment instance_profile and it will apply

module "instance" {
  source  = "cloudposse/ec2-instance/aws"
  version = "1.4.0"

  ssh_key_pair                = var.ssh_key_pair
  instance_type               = var.instance_type
  instance_profile      = aws_iam_instance_profile.default.id
  vpc_id                      = var.vpc_id
  security_groups             = var.security_groups
  subnet                      = var.subnet
  name                        = "ec2"
}

Screenshots

No response

Environment

Mac OS
TF 1.8.2

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug🐛 An issue with the system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions