Skip to content
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

feat: support inline policies #79

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ use_fullname = true
principals = {
"AWS" : ["*"]
}

inline_policy_enabled = true
1 change: 1 addition & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module "role" {
policy_document_count = 2
policy_description = "Test IAM policy"
role_description = "Test IAM role"
inline_policy_enabled = var.inline_policy_enabled

context = module.this.context
}
6 changes: 6 additions & 0 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ variable "principals" {
type = map(list(string))
description = "Map of service name as key and a list of ARNs to allow assuming the role as value (e.g. map(`AWS`, list(`arn:aws:iam:::role/admin`)))"
}

variable "inline_policy_enabled" {
type = bool
description = "Whether or not to enable an inline policy instead of a reusable managed policy"
default = false
}
23 changes: 16 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,43 @@ resource "aws_iam_role" "default" {
tags = var.tags_enabled ? module.this.tags : null
}

resource "aws_iam_role_policy" "default" {
count = module.this.enabled && var.policy_document_count > 0 && var.inline_policy_enabled ? 1 : 0

name = var.policy_name != "" && var.policy_name != null ? var.policy_name : module.this.id
role = join("", aws_iam_role.default[*].name)

policy = join("", data.aws_iam_policy_document.default[*].json)
}

data "aws_iam_policy_document" "default" {
count = module.this.enabled && var.policy_document_count > 0 ? 1 : 0
override_policy_documents = var.policy_documents
}

resource "aws_iam_policy" "default" {
count = module.this.enabled && var.policy_document_count > 0 ? 1 : 0
count = module.this.enabled && var.policy_document_count > 0 && !var.inline_policy_enabled ? 1 : 0
name = var.policy_name != "" && var.policy_name != null ? var.policy_name : module.this.id
description = var.policy_description
policy = join("", data.aws_iam_policy_document.default.*.json)
policy = join("", data.aws_iam_policy_document.default[*].json)
path = var.path
tags = var.tags_enabled ? module.this.tags : null
}

resource "aws_iam_role_policy_attachment" "default" {
count = module.this.enabled && var.policy_document_count > 0 ? 1 : 0
role = join("", aws_iam_role.default.*.name)
policy_arn = join("", aws_iam_policy.default.*.arn)
count = module.this.enabled && var.policy_document_count > 0 && !var.inline_policy_enabled ? 1 : 0
role = join("", aws_iam_role.default[*].name)
policy_arn = join("", aws_iam_policy.default[*].arn)
}

resource "aws_iam_role_policy_attachment" "managed" {
for_each = module.this.enabled ? var.managed_policy_arns : []
role = join("", aws_iam_role.default.*.name)
role = join("", aws_iam_role.default[*].name)
policy_arn = each.key
}

resource "aws_iam_instance_profile" "default" {
count = module.this.enabled && var.instance_profile_enabled ? 1 : 0
name = module.this.id
role = join("", aws_iam_role.default.*.name)
role = join("", aws_iam_role.default[*].name)
}
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ variable "path" {
}

variable "tags_enabled" {
type = string
type = bool
description = "Enable/disable tags on IAM roles and policies"
default = true
}

variable "inline_policy_enabled" {
type = bool
description = "Whether or not to enable an inline policy instead of a reusable managed policy"
default = false
}