AWS IAM Policy Attachment

This page shows how to write Terraform for IAM Policy Attachment and write them securely.

aws_organizations_policy_attachment (Terraform)

The Policy Attachment in IAM can be configured in Terraform with the resource name aws_organizations_policy_attachment. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L24
resource "aws_organizations_policy_attachment" "deny_root_account" {
  count = length(var.deny_root_account_target_ids)

  policy_id = aws_organizations_policy.deny_root_account.id
  target_id = element(var.deny_root_account_target_ids.*, count.index)
}
main.tf#L64
resource "aws_organizations_policy_attachment" "root" {
  policy_id = aws_organizations_policy.preventLeaveorganizations.id
  target_id = "r-xu7c"
}

#Organization Unit
main.tf#L22
resource "aws_organizations_policy_attachment" "deny_leaving_orgs" {
  count     = length(var.associate-acct-ids)
  policy_id = aws_organizations_policy.deny_leaving_orgs.id
  target_id = element(var.associate-acct-ids.*, count.index)
}

policies.tf#L9
resource "aws_organizations_policy_attachment" "regions-east-west-only" {
  policy_id = aws_organizations_policy.regions-east-west-only.id
  target_id = aws_organizations_organizational_unit.this.id
}

// Do not allow accounts to be removed from the Org
org_policies.tf#L22
resource "aws_organizations_policy_attachment" "root" {
  policy_id = aws_organizations_policy.policy1.id
  target_id = aws_organizations_organization.org.roots[0].id
}


Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Provides a resource to attach an AWS Organizations policy to an organization account, root, or unit.

Tips: Best Practices for The Other AWS IAM Resources

In addition to the aws_iam_account_password_policy, AWS IAM has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_iam_account_password_policy

Ensure AWS IAM account password policies requires long passwords

It's better to enforce the use of long and complex passwords to reduce the risk of bruteforce attacks.

Review your AWS IAM settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

CloudFormation Example

CloudFormation code does not have the related resource.

Frequently asked questions

What is AWS IAM Policy Attachment?

AWS IAM Policy Attachment is a resource for IAM of Amazon Web Service. Settings can be wrote in Terraform.

Where can I find the example code for the AWS IAM Policy Attachment?

For Terraform, the trussworks/terraform-aws-org-scp, fabiodsilva/terraform-scp and dev-minds/tf_modules source code examples are useful. See the Terraform Example section for further details.