Home > front end >  Error while creating aws_launch_template using Terraform
Error while creating aws_launch_template using Terraform

Time:10-31

I am receiving an error while creating aws_launch_template using Terraform, below the error message I am using an existing Role which is pre-created in the account

Error message - An argument named "iam_instance_profile" is not expected here. Did you mean to define a block of type "iam_instance_profile"?

Code

data "aws_iam_roles" "seamless_domain_join_role" {
  name = "seamless-domain-join-role"
}

data "aws_iam_instance_profile" "autoscale-instance-profile" {
  name = "seamless-domain-join-role"
}

resource "aws_launch_template" "Windows-instance" {
  name_prefix   = "Windows_Instance"
  image_id      = "ami-0526b9747c2c87a0b"
  iam_instance_profile = {
              arn = data.aws_iam_instance_profile.autoscale-instance-profile.arn
          }
  instance_type = "t2.medium"
  tag_specifications {
    resource_type = "instance"
    tags = {
      Name : "sk-autoscaling-dj"
    }
  }
}

I am receiving same error with **name ** as well.

  iam_instance_profile = {
              name= data.aws_iam_instance_profile.autoscale-instance-profile.name
          }

Any suggestions will be appreciated.

Any suggestions on how to fix this issue?

CodePudding user response:

You don't need the = sign as that is a block not an argument:

iam_instance_profile {
  arn = data.aws_iam_instance_profile.autoscale-instance-profile.arn
}

This should really be clear from the provider documentation for aws_launch_template resource [1].


[1] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template#example-usage

CodePudding user response:

I see two mistakes in the code-

  • The first one is in-
data "aws_iam_roles" "seamless_domain_join_role" {
  name = "seamless-domain-join-role"
}

It should be aws_iam_role , rather than aws_iam_roles .

  • The second mistake is in-
  iam_instance_profile = {
              arn = data.aws_iam_instance_profile.autoscale-instance-profile.arn
          }

It doesn't need a = as its a block.

The correct code is -

data "aws_iam_role" "seamless_domain_join_role" {
  name = "seamless-domain-join-role"
}

data "aws_iam_instance_profile" "autoscale-instance-profile" {
  name = "seamless-domain-join-role"
}

resource "aws_launch_template" "Windows-instance" {
  name_prefix = "Windows_Instance"
  image_id    = "ami-0526b9747c2c87a0b"
  iam_instance_profile {
    arn = data.aws_iam_instance_profile.autoscale-instance-profile.arn
  }
  instance_type = "t2.medium"
  tag_specifications {
    resource_type = "instance"
    tags = {
      Name : "sk-autoscaling-dj"
    }
  }
}

terraform validate

Success! The configuration is valid.

  • Related