Answers: <p>you can add role like below </p>
  
  <pre><code>role = "${aws_iam_role.ec2_role_name.name}"
  </code></pre>
  
  <p>Please check - <a href="https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html" rel="nofollow noreferrer">https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2018-07-12T11:51:07.053Z
Answers: Maybe you could link to some documentation to complete this answer, it's a bit terse as is
Answers: Tensibai
Answers: CC BY-SA 4.0
Answers: 2018-07-18T01:42:09.873Z
Answers: `role` is not a valid argument for the terraform resource aws instance -
   https://www.terraform.io/docs/providers/aws/r/instance.html
Answers: TechDawg270
Answers: CC BY-SA 4.0
Answers: 2018-07-18T04:52:32.700Z
Answers: Please check this role is vaild   https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html
Answers: sanjayparmar
Answers: CC BY-SA 4.0
Answers: 2018-07-12T05:51:20.030Z
Answers: 2018-07-18T04:53:31.757Z
Answers: 2018-07-18T04:53:31.757Z
Answers: sanjayparmar
Body: <p>I am trying to attach an existing role created in AWS, but I am not able to add it in Terraform Code. I tried to add the role in instance profile but it didn't work either for me.</p>
  
  <p>Is there any direct way to add it in the resource in Terraform Code ? </p>
  
  <p><em><code>iam_instance_profile  = "my-role"</code></em></p>
  
  <blockquote>
    <p><strong>Note:</strong> my-role is having the full access of ec2.</p>
  </blockquote>
  
Comments: CC BY-SA 4.0
Comments: 2018-05-02T00:41:22.917Z
Comments: What error message do you get?
Comments: Philipp Claßen
Comments: CC BY-SA 4.0
Comments: 2018-06-17T02:49:47.313Z
Comments: That should work, but like @PhilippClaßen said, without knowing what happened it's near impossible to help you out with this.
Comments: Tim Malone
Comments: CC BY-SA 4.0
Comments: 2021-09-08T02:42:00.177Z
Comments: Look this post for the solution https://stackoverflow.com/a/50224781/3016764
Comments: Mohan Sanagapalli
Tags: <amazon-ec2><terraform>
Title: How to set an existing IAM role to a new instance while spinning in terraform?
UserDisplayName: Mohan Sanagapalli
ThreadText: # How to set an existing IAM role to a new instance while spinning in terraform?
  
  (Asked by: username_0 on 2018-05-01)
  
  I am trying to attach an existing role created in AWS, but I am not able to add it in Terraform Code. I tried to add the role in instance profile but it didn't work either for me.
  
  Is there any direct way to add it in the resource in Terraform Code ?
  
  *`iam_instance_profile = "my-role"`*
  
  > **Note:** my-role is having the full access of ec2.
  
  **Tags:** amazon-ec2, terraform
  
  > **Comments:**
  > **username_1**: What error message do you get?  
  > **username_2**: That should work, but like @username_1 said, without knowing what happened it's near impossible to help you out with this.  
  > **username_0**: Look this post for the solution https://stackoverflow.com/a/50224781/3016764  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-07-12. Score: 1
  
  you can add role like below
  
  ```
  role = "${aws_iam_role.ec2_role_name.name}"
  
  ```
  
  Please check - <https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html>
  
  > **Comments:**
  > **username_4**: Maybe you could link to some documentation to complete this answer, it's a bit terse as is  
  > **username_5**: `role` is not a valid argument for the terraform resource aws instance -  https://www.terraform.io/docs/providers/aws/r/instance.html  
  > **username_3**: Please check this role is vaild   https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html  
  
  ---
  
  