Skip to content

Commit d25ff6d

Browse files
authored
Merge pull request #201 from NetApp/replace_inline_policies
Migrated away from the creation of the policy within a role since that method was deprecated.
2 parents 2fab416 + e2294f3 commit d25ff6d

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

Management-Utilities/fsxn-rotate-secret/terraform/main.tf

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ resource "random_id" "id" {
1111
byte_length = 4
1212
}
1313
#
14-
# Create the assume role policy document for the Lambda function.
14+
# Create a local variable for the Lambda function name, so it can be used in two places without causing a cycle.
15+
locals {
16+
lambdaName = "fsxn_rotate_secret-${random_id.id.hex}"
17+
}
18+
#
19+
# Create the policy document for the assume role policy for the Lambda function role.
1520
data "aws_iam_policy_document" "assume_role" {
1621
statement {
1722
effect = "Allow"
@@ -25,8 +30,8 @@ data "aws_iam_policy_document" "assume_role" {
2530
}
2631
}
2732
#
28-
# Create the inline policy document for the Lambda function role.
29-
data "aws_iam_policy_document" "inline_permissions" {
33+
# Create a policy document for the policy for the Lambda function role.
34+
data "aws_iam_policy_document" "lambda_permissions" {
3035
#
3136
# The frist two statements are required for the lambda function to write logs to CloudWatch.
3237
# While not required, are useful for debugging.
@@ -75,20 +80,18 @@ data "aws_iam_policy_document" "inline_permissions" {
7580
}
7681
}
7782
#
78-
# Create a local variable for the Lambda function name, so it can be used in two places without causing a cycle.
79-
locals {
80-
lambdaName = "fsxn_rotate_secret-${random_id.id.hex}"
81-
}
82-
#
8383
# Create the IAM role for the Lambda function.
84-
resource "aws_iam_role" "iam_for_lambda" {
85-
name = "iam_for_lambda-${random_id.id.hex}"
84+
resource "aws_iam_role" "role_for_lambda" {
85+
name = "rotate_fsxn_secret_role_${random_id.id.hex}"
8686
description = "IAM role for the Rotate FSxN Secret Lambda function."
8787
assume_role_policy = data.aws_iam_policy_document.assume_role.json
88-
inline_policy {
89-
name = "required_policy"
90-
policy = data.aws_iam_policy_document.inline_permissions.json
91-
}
88+
}
89+
#
90+
# Create the policy based on the policy document.
91+
resource "aws_iam_role_policy" "lambda_permissions" {
92+
name = "rotate_fsxn_secret_policy_${random_id.id.hex}"
93+
role = aws_iam_role.role_for_lambda.name
94+
policy = data.aws_iam_policy_document.lambda_permissions.json
9295
}
9396
#
9497
# Create the archive file for the Lambda function.
@@ -103,7 +106,7 @@ resource "aws_lambda_function" "rotateLambdaFunction" {
103106
provider = aws.secrets_provider
104107
function_name = local.lambdaName
105108
description = var.svm_id != "" ? "Lambda function to rotate the secret for SVM (${var.svm_id})." : "Lambda function to rotate the secret for FSxN File System (${var.fsx_id})."
106-
role = aws_iam_role.iam_for_lambda.arn
109+
role = aws_iam_role.role_for_lambda.arn
107110
runtime = "python3.12"
108111
handler = "fsxn_rotate_secret.lambda_handler"
109112
filename = "fsxn_rotate_secret.zip"

Management-Utilities/fsxn-rotate-secret/terraform/output.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ output "lambda_name" {
2020

2121
output "role_arn" {
2222
description = "The ARN of the role that was created that allows the Lambda function to rotate the secret."
23-
value = aws_iam_role.iam_for_lambda.arn
23+
value = aws_iam_role.role_for_lambda.arn
2424
}
2525

2626
output "role_name" {
2727
description = "The name of the role that was created that allows the Lambda function to rotate the secret."
28-
value = aws_iam_role.iam_for_lambda.name
28+
value = aws_iam_role.role_for_lambda.name
2929
}

0 commit comments

Comments
 (0)