<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>

CC BY-SA 4.0
2018-07-12T11:51:07.053Z
Maybe you could link to some documentation to complete this answer, it's a bit terse as is
Tensibai
CC BY-SA 4.0
2018-07-18T01:42:09.873Z
`role` is not a valid argument for the terraform resource aws instance -
 https://www.terraform.io/docs/providers/aws/r/instance.html
TechDawg270
CC BY-SA 4.0
2018-07-18T04:52:32.700Z
Please check this role is vaild   https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html
sanjayparmar
CC BY-SA 4.0
2018-07-12T05:51:20.030Z
2018-07-18T04:53:31.757Z
2018-07-18T04:53:31.757Z
sanjayparmar
<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>

CC BY-SA 4.0
2018-05-02T00:41:22.917Z
What error message do you get?
Philipp Claßen
CC BY-SA 4.0
2018-06-17T02:49:47.313Z
That should work, but like @PhilippClaßen said, without knowing what happened it's near impossible to help you out with this.
Tim Malone
CC BY-SA 4.0
2021-09-08T02:42:00.177Z
Look this post for the solution https://stackoverflow.com/a/50224781/3016764
Mohan Sanagapalli
<amazon-ec2><terraform>
How to set an existing IAM role to a new instance while spinning in terraform?
Mohan Sanagapalli
# 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 @PhilippClaßen 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  

---

