'Creating EC2 instance profile as part of the IAM role for EMR in Terraform

In Terraform, we can create a custom EC2 role (emr-role-001) and we can create an instance profile that references that role. However, if we create it this way, EMR won't accept this instance profile. Instead, it has to be the instance profile created automatically when the custom EC2 role (emr-role-001) is created.

How can I do this in Terraform? Below is a sample of the code I am using.

resource "aws_iam_role" "iam_emr_profile_role" {
  name = "iam_emr_profile_role"

  assume_role_policy = <<EOF
{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}

resource "aws_iam_instance_profile" "emr_profile" {
  name = "emr_profile"
  role = aws_iam_role.iam_emr_profile_role.name
}

The error is that the instance profile is not valid, whenever I try to use it when deploying the EMR cluster.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source