Skip to content

Commit 50a8d1e

Browse files
committed
feat(modules): iam-role sub-module
1 parent 56b8908 commit 50a8d1e

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

modules/iam-role/.header.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# iam-role
2+
3+
This sub-module creates IAM role and attaches appropriate IAM policies to the IAM role.

modules/iam-role/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
resource "aws_iam_role" "this" {
2+
name = var.name
3+
description = var.description
4+
assume_role_policy = var.assume_role_policy
5+
6+
tags = var.tags
7+
}
8+
9+
################################################################################
10+
# IAM Policy
11+
################################################################################
12+
13+
resource "aws_iam_policy" "this" {
14+
for_each = var.iam_policies
15+
16+
name = each.value.name
17+
description = each.value.description
18+
policy = jsonencode(each.value.policy)
19+
20+
tags = each.value.tags
21+
}
22+
23+
resource "aws_iam_role_policy_attachment" "iam_policies" {
24+
for_each = aws_iam_policy.this
25+
26+
role = aws_iam_role.this.name
27+
policy_arn = each.value.arn
28+
}
29+
30+
resource "aws_iam_role_policy_attachment" "this" {
31+
for_each = var.iam_policy_attachments
32+
33+
role = aws_iam_role.this.name
34+
policy_arn = each.value
35+
}

modules/iam-role/outputs.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
output "id" {
2+
description = "Name of the role."
3+
value = aws_iam_role.this.id
4+
}
5+
6+
output "arn" {
7+
description = "Amazon Resource Name (ARN) specifying the role."
8+
value = aws_iam_role.this.arn
9+
}
10+
11+
################################################################################
12+
# IAM Policy
13+
################################################################################
14+
15+
output "iam_policies_ids" {
16+
description = "Map of IAM Policies Identifiers."
17+
value = { for name, iam_policy in aws_iam_policy.this : name => iam_policy.id }
18+
}
19+
20+
output "iam_policies_arns" {
21+
description = "Map of IAM Policies ARNs."
22+
value = { for name, iam_policy in aws_iam_policy.this : name => iam_policy.arn }
23+
}

modules/iam-role/variables.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
################################################################################
2+
# IAM Role
3+
################################################################################
4+
5+
variable "name" {
6+
description = "(Required, Forces new resource) Friendly name of the role."
7+
type = string
8+
nullable = false
9+
}
10+
11+
variable "description" {
12+
description = "(Optional, Default:null) Description of the role."
13+
type = string
14+
default = null
15+
}
16+
17+
variable "assume_role_policy" {
18+
description = "(Required) Policy that grants an entity permission to assume the role."
19+
type = any
20+
nullable = false
21+
}
22+
23+
variable "tags" {
24+
description = "(Optional, Default:{}) Key-value mapping of tags for the IAM role."
25+
type = map(string)
26+
nullable = false
27+
default = {}
28+
}
29+
30+
################################################################################
31+
# IAM Policy
32+
################################################################################
33+
34+
variable "iam_policies" {
35+
description = "(Optional, Default:{}) Map of IAM policies to create and attach to the IAM Role."
36+
type = map(
37+
object({
38+
name = string
39+
description = optional(string, null)
40+
policy = object({
41+
Version = optional(string, "2012-10-17")
42+
Statement = list(
43+
object({
44+
Sid = optional(string)
45+
Effect = string
46+
Resource = string
47+
Action = optional(list(string), [])
48+
})
49+
)
50+
})
51+
tags = optional(map(string), {})
52+
})
53+
)
54+
nullable = false
55+
default = {}
56+
}
57+
58+
variable "iam_policy_attachments" {
59+
description = "(Optional, Default:{}) Map of IAM Policy ARNs to attach to the IAM Role."
60+
type = map(string)
61+
nullable = false
62+
default = {}
63+
}

modules/iam-role/version.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.8.4"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)