-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Guide: AWS IAM Policy Documents #6016
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS IAM Policy Documents with Terraform" | ||
sidebar_current: "docs-aws-guide-iam-policy-documents" | ||
description: |- | ||
Using Terraform to configure AWS IAM policy documents. | ||
--- | ||
|
||
# AWS IAM Policy Documents with Terraform | ||
|
||
AWS leverages a standard JSON Identity and Access Management (IAM) policy document format across many services to control authorization to resources and API actions. This guide is designed to highlight some recommended configuration patterns with how Terraform and the AWS provider can build these policy documents. | ||
|
||
The example policy documents and resources in this guide are for illustrative purposes only. Full documentation about the IAM policy format and supported elements can be found in the [AWS IAM User Guide](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html). | ||
|
||
~> **NOTE:** Some AWS services only allow a subset of the policy elements or policy variables. For more information, see the AWS User Guide for the service you are configuring. | ||
|
||
~> **NOTE:** [IAM policy variables](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_variables.html), e.g. `${aws:username}`, use the same configuration syntax (`${...}`) as Terraform interpolation. When implementing IAM policy documents with these IAM variables, you may receive syntax errors from Terraform. You can escape the dollar character within your Terraform configration to prevent the error, e.g. `$${aws:username}`. | ||
|
||
<!-- TOC depthFrom:2 --> | ||
|
||
- [Choosing a Configuration Method](#choosing-a-configuration-method) | ||
- [Recommended Configuration Method Examples](#recommended-configuration-method-examples) | ||
- [aws_iam_policy_document Data Source](#aws_iam_policy_document-data-source) | ||
- [Multiple Line Heredoc Syntax](#multiple-line-heredoc-syntax) | ||
- [Other Configuration Method Examples](#other-configuration-method-examples) | ||
- [Single Line String Syntax](#single-line-string-syntax) | ||
- [file() Interpolation Function](#file-interpolation-function) | ||
- [template_file Data Source](#template_file-data-source) | ||
|
||
<!-- /TOC --> | ||
|
||
## Choosing a Configuration Method | ||
|
||
Terraform offers flexibility when creating configurations to match the architectural structure of teams and infrastructure. In most situations, using native functionality within Terraform and its providers will be the simplest to understand, eliminating context switching with other tooling, file sprawl, or differing file formats. Configuration examples of the available methods can be found later in the guide. | ||
|
||
The recommended approach to building AWS IAM policy documents within Terraform is the highly customizable [`aws_iam_policy_document` data source](#aws_iam_policy_document-data-source). A short list of benefits over other methods include: | ||
|
||
- Native Terraform configuration - no need to worry about JSON formatting or syntax | ||
- Policy layering - create policy documents that combine and/or overwrite other policy documents | ||
- Built-in policy error checking | ||
|
||
Otherwise in simple cases, such as a staticly defined assume role policy for an IAM role, Terraform's [multiple line heredoc syntax](#multiple-line-heredoc-syntax) allows the easiest formatting without any indirection of a separate data source configuration or file. | ||
|
||
Additional methods are possible, such [single line string syntax](#single-line-string-syntax), the [file() interpolation function](#file-interpolation-function), and the [template_file data source](#template_file-data-source), however their usage is discouraged due to their complexity. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 that's a good catch |
||
|
||
## Recommended Configuration Method Examples | ||
|
||
These configuration methods are the simplest and most powerful within Terraform. | ||
|
||
### aws_iam_policy_document Data Source | ||
|
||
-> For complete implementation information and examples, see the [`aws_iam_policy_document` data source documentation](/docs/providers/aws/d/iam_policy_document.html). | ||
|
||
```hcl | ||
data "aws_iam_policy_document" "example" { | ||
statement { | ||
actions = ["*"] | ||
resources = ["*"] | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "example" { | ||
# ... other configuration ... | ||
|
||
policy = "${data.aws_iam_policy_document.example.json}" | ||
} | ||
``` | ||
|
||
### Multiple Line Heredoc Syntax | ||
|
||
Interpolation is available within the heredoc string if necessary. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we link to any doc page which explains There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out the 0.12 branch has a whole section on it! https://github.com/hashicorp/terraform/blob/v0.12-dev/website/docs/configuration/expressions.html.md#string-literals Unfortunately the current documentation has it listed as just a bullet point under syntax 😅 https://www.terraform.io/docs/configuration/syntax.html (which will be removed with 0.12 documentation updates) Not sure if we link to the soon-to-be removed place or somewhere else. Open to opinions! |
||
|
||
For example: | ||
|
||
```hcl | ||
resource "aws_iam_policy" "example" { | ||
# ... other configuration ... | ||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": { | ||
"Effect": "Allow", | ||
"Action": "*", | ||
"Resource": "*" | ||
} | ||
} | ||
POLICY | ||
} | ||
``` | ||
|
||
## Other Configuration Method Examples | ||
|
||
These other configuration methods are provided only for reference and not meant to be an authoritative source of information. | ||
|
||
### Single Line String Syntax | ||
|
||
Single line IAM policy documents can be constructed with regular string syntax. Interpolation is available within the string if necessary. Since the double quotes within the IAM policy JSON conflict with Terraform's double quotes for declaring a string, they need to be escaped (`\"`). | ||
|
||
For example: | ||
|
||
```hcl | ||
resource "aws_iam_policy" "example" { | ||
# ... other configuration ... | ||
|
||
policy = "{\"Version\": \"2012-10-17\", \"Statement\": {\"Effect\": \"Allow\", \"Action\": \"*\", \"Resource\": \"*\"}}" | ||
} | ||
``` | ||
|
||
### file() Interpolation Function | ||
|
||
To decouple the IAM policy JSON from the Terraform configuration, Terraform has a built-in [`file()` interpolation function](/docs/configuration/interpolation.html#file-path-), which can read the contents of a local file into the configuration. Interpolation is _not_ available when using the `file()` function by itself. | ||
|
||
For example, in a file called `policy.json`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We seem to be missing a verb here ^ 👓 |
||
|
||
```json | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": { | ||
"Effect": "Allow", | ||
"Action": "*", | ||
"Resource": "*" | ||
} | ||
} | ||
``` | ||
|
||
Then `policy.json` contents can be read into the Terraform configuration via: | ||
|
||
```hcl | ||
resource "aws_iam_policy" "example" { | ||
# ... other configuration ... | ||
|
||
policy = "${file("policy.json")}" | ||
} | ||
``` | ||
|
||
### template_file Data Source | ||
|
||
To enable interpolation in decoupled files, the [`template_file` data source](/docs/providers/template/d/file.html) is available. | ||
|
||
For example, in a file called `policy.json.tpl`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We seem to be missing a verb here ^ 👓 |
||
|
||
```json | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": { | ||
"Effect": "Allow", | ||
"Action": "*", | ||
"Resource": "${resource}" | ||
} | ||
} | ||
``` | ||
|
||
The contents can be read and interpolated into the Terraform configuration via: | ||
|
||
```hcl | ||
data "template_file" "example" { | ||
template = "${file("policy.json.tpl")}" | ||
|
||
vars { | ||
resource = "${aws_vpc.example.arn}" | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "example" { | ||
# ... other configuration ... | ||
|
||
policy = "${data.template_file.example.rendered}" | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
staticly
->statically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it may be worth linking to a document/docs page which explains what is "assume role policy".
AFAIK it often causes some confusion for IAM beginners as they mismatch that with the actual resource policy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catches! Will update with fixed word and https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_permissions-to-switch.html