-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathusers-loops.tf
49 lines (38 loc) · 1019 Bytes
/
users-loops.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
provider "aws" {
region = "ap-south-1"
}
resource "aws_iam_user" "example" {
name = "deng.${count.index}"
count = 3
}
# now multiple user using list
variable "user_names" {
description = "create iam users"
type = "list"
default = ["kapil", "deng", "yuva"]
}
resource "aws_iam_user" "testing" {
count = "${length(var.user_names)}"
name = "${element(var.user_names, count.index)}"
}
output "all_arn" {
value = ["${aws_iam_user.testing.*.arn}"]
}
data "aws_iam_policy_document" "ec2_read_only" {
statement {
effect = "Allow"
actions = ["ec2:Describe*"]
resources = ["*"]
}
}
#now create the policy from that document
resource "aws_iam_policy" "ec2_read_only" {
name = "ec2-read-only"
policy = "${data.aws_iam_policy_document.ec2_read_only.json}"
}
#now attach iam policy to users
resource "aws_iam_user_policy_attachment" "ec2_access" {
count = "${length(var.user_names)}"
user = "${element(aws_iam_user.testing.*.name, count.index)}"
policy_arn = "${aws_iam_policy.ec2_read_only.arn}"
}