Terraform – The instance profile associated with the environment does not exist

I am trying to create an Elastic Beanstalk application but I am coming across the error:

The instance profile iam_for_beanstalk associated with the environment does not exist.

The role does exist as you can see here:

enter image description here

and it is being created via Terraform through the following code:

resource "aws_iam_role" "beanstalk" {
  name = "iam_for_beanstalk"
  assume_role_policy = file("${path.module}/assumerole.json")
}

assumerole.json looks like this:

{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Principal": {
            "Service": "elasticbeanstalk.amazonaws.com"
        },
        "Action": "sts:AssumeRole",
        "Condition": {
          "StringEquals": {
            "sts:ExternalId": "elasticbeanstalk"
          }
        }
    }]
}

And here is how I try to associate it to the newly created application:

resource "aws_elastic_beanstalk_environment" "nodejs" {
  application = aws_elastic_beanstalk_application.nodejs.name
  name = "stackoverflow"
  version_label = var.app_version
  solution_stack_name = "64bit Amazon Linux 2 v5.4.6 running Node.js 14"

  setting {
    namespace = "aws:autoscaling:launchconfiguration"
    name      = "IamInstanceProfile"
    value     = "iam_for_beanstalk"
  }...

I also tried to assign the name like below, but no success:

value     = aws_iam_role.beanstalk.name

>Solution :

You also need to create an aws_iam_instance_profile resource:

resource "aws_iam_instance_profile" "beanstalk_instance_profile" {
  name = "stack-overflow-example"
  role = aws_iam_role.beanstalk.name
}

Leave a Reply