Skip to content

Commit 740c996

Browse files
committed
Update for_each examples
1 parent 8370372 commit 740c996

File tree

6 files changed

+70
-45
lines changed

6 files changed

+70
-45
lines changed

code/terraform/05-tips-and-tricks/loops-and-if-statements/live/global/multiple-s3-buckets/main.tf

Lines changed: 0 additions & 24 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# IAM user for_each example
2+
3+
This folder contains example [Terraform](https://www.terraform.io/) configuration that create several
4+
[IAM](https://aws.amazon.com/iam/) users in an [Amazon Web Services (AWS) account](http://aws.amazon.com/).
5+
6+
For more info, please see Chapter 5, "Terraform Tips & Tricks: Loops, If-Statements, Deployment, and Gotchas", of
7+
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
8+
9+
## Pre-requisites
10+
11+
* You must have [Terraform](https://www.terraform.io/) installed on your computer.
12+
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).
13+
14+
Please note that this code was written for Terraform 0.12.x.
15+
16+
## Quick start
17+
18+
**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
19+
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
20+
charges you may incur.**
21+
22+
Configure your [AWS access
23+
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
24+
environment variables:
25+
26+
```
27+
export AWS_ACCESS_KEY_ID=(your access key id)
28+
export AWS_SECRET_ACCESS_KEY=(your secret access key)
29+
```
30+
31+
Deploy the code:
32+
33+
```
34+
terraform init
35+
terraform apply
36+
```
37+
38+
Clean up when you're done:
39+
40+
```
41+
terraform destroy
42+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_version = ">= 0.12, < 0.13"
3+
}
4+
5+
provider "aws" {
6+
region = "us-east-2"
7+
8+
# Allow any 2.x version of the AWS provider
9+
version = "~> 2.0"
10+
}
11+
12+
resource "aws_iam_user" "example" {
13+
for_each = toset(var.user_names)
14+
name = each.value
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "all_arns" {
2+
value = values(aws_iam_user.example)[*].arn
3+
}
4+
5+
output "all_users" {
6+
value = aws_iam_user.example
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "user_names" {
2+
description = "Create IAM users with these names"
3+
type = list(string)
4+
default = ["neo", "trinity", "morpheus"]
5+
}

code/terraform/05-tips-and-tricks/loops-and-if-statements/live/global/three-iam-users-unique-names/main.tf

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,6 @@ resource "aws_iam_user" "example" {
1414
name = var.user_names[count.index]
1515
}
1616

17-
resource "aws_iam_policy" "ec2_read_only" {
18-
19-
name = "${var.policy_name_prefix}ec2-read-only"
20-
21-
policy = data.aws_iam_policy_document.ec2_read_only.json
22-
}
23-
24-
data "aws_iam_policy_document" "ec2_read_only" {
25-
statement {
26-
effect = "Allow"
27-
actions = ["ec2:Describe*"]
28-
resources = ["*"]
29-
}
30-
}
31-
32-
resource "aws_iam_user_policy_attachment" "ec2_access" {
33-
count = length(var.user_names)
34-
user = element(aws_iam_user.example[*].name, count.index)
35-
policy_arn = aws_iam_policy.ec2_read_only.arn
36-
}
37-
3817
resource "aws_iam_policy" "cloudwatch_read_only" {
3918

4019
name = "${var.policy_name_prefix}cloudwatch-read-only"

0 commit comments

Comments
 (0)