Skip to content

Commit

Permalink
PGAP-10: Add local IAM role module (feat)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukiffer committed Dec 3, 2020
1 parent 4f14dd1 commit f7480db
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
52 changes: 52 additions & 0 deletions modules/aws-iam-role/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# DEPLOY AN IAM ROLE
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

terraform {
required_version = ">= 0.12"
}

data "aws_iam_policy_document" "assume_role_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]

dynamic "principals" {
for_each = length(var.aws_principals) > 0 ? [1] : []

content {
type = "AWS"
identifiers = var.aws_principals
}
}

dynamic "principals" {
for_each = length(var.service_principals) > 0 ? [1] : []

content {
type = "Service"
identifiers = var.service_principals
}
}

dynamic "principals" {
for_each = length(var.federated_principals) > 0 ? [1] : []

content {
type = "Federated"
identifiers = var.federated_principals
}
}
}
}

resource "aws_iam_role" "role" {
name = var.name
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}

resource "aws_iam_role_policy_attachment" "policy" {
for_each = var.iam_policy_arns
role = aws_iam_role.role.id
policy_arn = each.value
}
9 changes: 9 additions & 0 deletions modules/aws-iam-role/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "arn" {
description = "The ARN of the IAM role."
value = aws_iam_role.role.arn
}

output "id" {
description = "The ID of the IAM role."
value = aws_iam_role.role.id
}
28 changes: 28 additions & 0 deletions modules/aws-iam-role/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
variable "name" {
description = "The name of the IAM role."
type = string
}

variable "service_principals" {
description = "A list service principals permitted to assume this role."
type = set(string)
default = []
}

variable "aws_principals" {
description = "A list of AWS principals (ARNs) permitted to assume this role."
type = set(string)
default = []
}

variable "federated_principals" {
description = "A list of federated principals permitted to assume this role."
type = set(string)
default = []
}

variable "iam_policy_arns" {
description = "The ARNs of IAM policies to attach to this role."
type = set(string)
default = []
}

0 comments on commit f7480db

Please sign in to comment.