Skip to content

Commit 7df6bbf

Browse files
feat: Add a way to define IAM policy name prefix (#354)
Co-authored-by: Anton Babenko <[email protected]>
1 parent b36268c commit 7df6bbf

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ No modules.
752752
| <a name="input_policy"></a> [policy](#input\_policy) | An additional policy document ARN to attach to the Lambda Function role | `string` | `null` | no |
753753
| <a name="input_policy_json"></a> [policy\_json](#input\_policy\_json) | An additional policy document as JSON to attach to the Lambda Function role | `string` | `null` | no |
754754
| <a name="input_policy_jsons"></a> [policy\_jsons](#input\_policy\_jsons) | List of additional policy documents as JSON to attach to Lambda Function role | `list(string)` | `[]` | no |
755+
| <a name="input_policy_name"></a> [policy\_name](#input\_policy\_name) | IAM policy name. It override the default value, which is the same as role\_name | `string` | `null` | no |
755756
| <a name="input_policy_path"></a> [policy\_path](#input\_policy\_path) | Path of policies to that should be added to IAM role for Lambda Function | `string` | `null` | no |
756757
| <a name="input_policy_statements"></a> [policy\_statements](#input\_policy\_statements) | Map of dynamic policy statements to attach to Lambda Function role | `any` | `{}` | no |
757758
| <a name="input_provisioned_concurrent_executions"></a> [provisioned\_concurrent\_executions](#input\_provisioned\_concurrent\_executions) | Amount of capacity to allocate. Set to 1 or greater to enable, or set to 0 to disable provisioned concurrency. | `number` | `-1` | no |

iam.tf

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ locals {
1111
# attempting to plan if the role_name and function_name are not set. This is a workaround
1212
# for #83 that will allow one to import resources without receiving an error from coalesce.
1313
# @see https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/83
14-
role_name = local.create_role ? coalesce(var.role_name, var.function_name, "*") : null
14+
role_name = local.create_role ? coalesce(var.role_name, var.function_name, "*") : null
15+
policy_name = coalesce(var.policy_name, local.role_name)
1516

1617
# IAM Role trusted entities is a list of any (allow strings (services) and maps (type+identifiers))
1718
trusted_entities_services = distinct(compact(concat(
@@ -132,7 +133,7 @@ data "aws_iam_policy_document" "logs" {
132133
resource "aws_iam_policy" "logs" {
133134
count = local.create_role && var.attach_cloudwatch_logs_policy ? 1 : 0
134135

135-
name = "${local.role_name}-logs"
136+
name = "${local.policy_name}-logs"
136137
path = var.policy_path
137138
policy = data.aws_iam_policy_document.logs[0].json
138139
tags = var.tags
@@ -169,7 +170,7 @@ data "aws_iam_policy_document" "dead_letter" {
169170
resource "aws_iam_policy" "dead_letter" {
170171
count = local.create_role && var.attach_dead_letter_policy ? 1 : 0
171172

172-
name = "${local.role_name}-dl"
173+
name = "${local.policy_name}-dl"
173174
path = var.policy_path
174175
policy = data.aws_iam_policy_document.dead_letter[0].json
175176
tags = var.tags
@@ -196,7 +197,7 @@ data "aws_iam_policy" "vpc" {
196197
resource "aws_iam_policy" "vpc" {
197198
count = local.create_role && var.attach_network_policy ? 1 : 0
198199

199-
name = "${local.role_name}-vpc"
200+
name = "${local.policy_name}-vpc"
200201
path = var.policy_path
201202
policy = data.aws_iam_policy.vpc[0].policy
202203
tags = var.tags
@@ -223,7 +224,7 @@ data "aws_iam_policy" "tracing" {
223224
resource "aws_iam_policy" "tracing" {
224225
count = local.create_role && var.attach_tracing_policy ? 1 : 0
225226

226-
name = "${local.role_name}-tracing"
227+
name = "${local.policy_name}-tracing"
227228
path = var.policy_path
228229
policy = data.aws_iam_policy.tracing[0].policy
229230
tags = var.tags
@@ -260,7 +261,7 @@ data "aws_iam_policy_document" "async" {
260261
resource "aws_iam_policy" "async" {
261262
count = local.create_role && var.attach_async_event_policy ? 1 : 0
262263

263-
name = "${local.role_name}-async"
264+
name = "${local.policy_name}-async"
264265
path = var.policy_path
265266
policy = data.aws_iam_policy_document.async[0].json
266267
tags = var.tags
@@ -280,7 +281,7 @@ resource "aws_iam_role_policy_attachment" "async" {
280281
resource "aws_iam_policy" "additional_json" {
281282
count = local.create_role && var.attach_policy_json ? 1 : 0
282283

283-
name = local.role_name
284+
name = local.policy_name
284285
path = var.policy_path
285286
policy = var.policy_json
286287
tags = var.tags
@@ -300,7 +301,7 @@ resource "aws_iam_role_policy_attachment" "additional_json" {
300301
resource "aws_iam_policy" "additional_jsons" {
301302
count = local.create_role && var.attach_policy_jsons ? var.number_of_policy_jsons : 0
302303

303-
name = "${local.role_name}-${count.index}"
304+
name = "${local.policy_name}-${count.index}"
304305
path = var.policy_path
305306
policy = var.policy_jsons[count.index]
306307
tags = var.tags
@@ -384,7 +385,7 @@ data "aws_iam_policy_document" "additional_inline" {
384385
resource "aws_iam_policy" "additional_inline" {
385386
count = local.create_role && var.attach_policy_statements ? 1 : 0
386387

387-
name = "${local.role_name}-inline"
388+
name = "${local.policy_name}-inline"
388389
path = var.policy_path
389390
policy = data.aws_iam_policy_document.additional_inline[0].json
390391
tags = var.tags

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ variable "role_tags" {
433433
# Policies
434434
###########
435435

436+
variable "policy_name" {
437+
description = "IAM policy name. It override the default value, which is the same as role_name"
438+
type = string
439+
default = null
440+
}
441+
436442
variable "attach_cloudwatch_logs_policy" {
437443
description = "Controls whether CloudWatch Logs policy should be added to IAM role for Lambda Function"
438444
type = bool

0 commit comments

Comments
 (0)