From bb3406362e0cc10b90b3fc55b7ba61a1c7bf9d81 Mon Sep 17 00:00:00 2001 From: Ana K M Vilhena Date: Thu, 19 Nov 2026 12:14:39 +0100 Subject: [PATCH 1/2] Set an optional permissions boundary on the IAM role `iam_role_permissions_boundary` sets the boundary when the role is created. `ignore_changes` keeps existing roles as they are when the variable is set later, so adopting it never changes what a running function may do. Co-Authored-By: Claude Opus 5.5 --- _test/main.tf | 2 ++ main.tf | 9 +++++++++ variables.tf | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/_test/main.tf b/_test/main.tf index 73f592f..be0d7d7 100644 --- a/_test/main.tf +++ b/_test/main.tf @@ -20,6 +20,8 @@ module "lambda" { source_dir = "lambda/src" + iam_role_permissions_boundary = "arn:aws:iam::123456789012:policy/example-boundary" + default_tags = { app = "example" env = "production" diff --git a/main.tf b/main.tf index 78c2a83..4ce547c 100644 --- a/main.tf +++ b/main.tf @@ -59,7 +59,16 @@ resource "aws_iam_role" "this" { assume_role_policy = data.aws_iam_policy_document.lambda-assume-role.json + permissions_boundary = var.iam_role_permissions_boundary + tags = merge(var.default_tags, var.iam_role_tags) + + # The boundary is set when the role is created. A role created before + # `var.iam_role_permissions_boundary` was set keeps its boundary, or none, until it is + # replaced, so setting the variable never changes what a running function may do. + lifecycle { + ignore_changes = [permissions_boundary] + } } data "aws_iam_policy_document" "lambda-assume-role" { diff --git a/variables.tf b/variables.tf index c5a5c52..6a34521 100644 --- a/variables.tf +++ b/variables.tf @@ -70,6 +70,15 @@ The name of the method within your code that Lambda calls to execute your functi EOS } +variable "iam_role_permissions_boundary" { + type = string + default = null + + description = < Date: Thu, 19 Nov 2026 12:29:14 +0100 Subject: [PATCH 2/2] Take the permissions boundary by policy name A policy's ARN includes its account. A name that exists in every account lets callers pass one value whatever account the role is in; the module builds the ARN from the role's account. Co-Authored-By: Claude Opus 5.5 --- _test/main.tf | 2 +- main.tf | 20 ++++++++++++++++++-- variables.tf | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/_test/main.tf b/_test/main.tf index be0d7d7..d6871f1 100644 --- a/_test/main.tf +++ b/_test/main.tf @@ -20,7 +20,7 @@ module "lambda" { source_dir = "lambda/src" - iam_role_permissions_boundary = "arn:aws:iam::123456789012:policy/example-boundary" + iam_role_permissions_boundary_name = "example-boundary" default_tags = { app = "example" diff --git a/main.tf b/main.tf index 4ce547c..a0369c9 100644 --- a/main.tf +++ b/main.tf @@ -59,18 +59,34 @@ resource "aws_iam_role" "this" { assume_role_policy = data.aws_iam_policy_document.lambda-assume-role.json - permissions_boundary = var.iam_role_permissions_boundary + permissions_boundary = local.iam_role_permissions_boundary tags = merge(var.default_tags, var.iam_role_tags) # The boundary is set when the role is created. A role created before - # `var.iam_role_permissions_boundary` was set keeps its boundary, or none, until it is + # `var.iam_role_permissions_boundary_name` was set keeps its boundary, or none, until it is # replaced, so setting the variable never changes what a running function may do. lifecycle { ignore_changes = [permissions_boundary] } } +data "aws_caller_identity" "boundary" { + count = var.iam_role_permissions_boundary_name != null ? 1 : 0 +} + +data "aws_partition" "boundary" { + count = var.iam_role_permissions_boundary_name != null ? 1 : 0 +} + +locals { + iam_role_permissions_boundary = ( + var.iam_role_permissions_boundary_name != null + ? "arn:${data.aws_partition.boundary[0].partition}:iam::${data.aws_caller_identity.boundary[0].account_id}:policy/${var.iam_role_permissions_boundary_name}" + : null + ) +} + data "aws_iam_policy_document" "lambda-assume-role" { statement { actions = ["sts:AssumeRole"] diff --git a/variables.tf b/variables.tf index 6a34521..9852ce4 100644 --- a/variables.tf +++ b/variables.tf @@ -70,12 +70,12 @@ The name of the method within your code that Lambda calls to execute your functi EOS } -variable "iam_role_permissions_boundary" { +variable "iam_role_permissions_boundary_name" { type = string default = null description = <