Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: correct way to attach AWS managed policies to a role?

I want to attach one of the pre-existing AWS managed roles to a policy, here's my current code:

resource "aws_iam_role_policy_attachment" "sto-readonly-role-policy-attach" {   role       = "${aws_iam_role.sto-test-role.name}"   policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess" } 

Is there a better way to model the managed policy and then reference it instead of hardcoding the ARN? It just seems like whenever I hardcode ARNs / paths or other stuff like this, I usually find out later there was a better way.

Is there something already existing in Terraform that models managed policies? Or is hardcoding the ARN the "right" way to do it?

like image 962
Shorn Avatar asked Jul 10 '17 00:07

Shorn


People also ask

How do you create an IAM role and policy terraform?

Create an IAM Role using the Terraform configuration files In our case it is AWS. The second command to be used is 'terraform plan'. This command is used to see the changes that will take place on the infrastructure. 'terraform apply' command will create the resources on the AWS mentioned in the main.tf file.


1 Answers

The IAM Policy data source is great for this. A data resource is used to describe data or resources that are not actively managed by Terraform, but are referenced by Terraform.

For your example, you would create a data resource for the managed policy as follows:

data "aws_iam_policy" "ReadOnlyAccess" {   arn = "arn:aws:iam::aws:policy/ReadOnlyAccess" } 

The name of the data source, ReadOnlyAccess in this case, is entirely up to you. For managed policies I use the same name as the policy name for the sake of consistency, but you could just as easily name it readonly if that suits you.

You would then attach the IAM policy to your role as follows:

resource "aws_iam_role_policy_attachment" "sto-readonly-role-policy-attach" {   role       = "${aws_iam_role.sto-test-role.name}"   policy_arn = "${data.aws_iam_policy.ReadOnlyAccess.arn}" } 
like image 148
jorelli Avatar answered Oct 12 '22 17:10

jorelli