Created
February 2, 2023 15:24
-
-
Save kenichi-shibata/e7d1ec7d45d57af6f7339f3572649fe8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_iam_role" "this" { | |
count = local.enabled ? 1 : 0 | |
name = "${var.function_name}-${local.region_name}" | |
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy.*.json) | |
permissions_boundary = var.permissions_boundary | |
} | |
data "aws_iam_policy_document" "assume_role_policy" { | |
count = local.enabled ? 1 : 0 | |
statement { | |
actions = ["sts:AssumeRole"] | |
principals { | |
type = "Service" | |
identifiers = ["lambda.amazonaws.com"] | |
} | |
} | |
} | |
# Allow Lambda to access specific SSM parameters | |
data "aws_iam_policy_document" "ssm" { | |
count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0 | |
statement { | |
actions = [ | |
"ssm:GetParameter", | |
"ssm:GetParameters", | |
"ssm:GetParametersByPath", | |
] | |
resources = formatlist("arn:${local.partition}:ssm:${local.region_name}:${local.account_id}:parameter%s", var.ssm_parameter_names) | |
} | |
} | |
resource "aws_iam_policy" "ssm" { | |
count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0 | |
name = "${var.function_name}-ssm-policy-${local.region_name}" | |
description = var.iam_policy_description | |
policy = data.aws_iam_policy_document.ssm[count.index].json | |
} | |
resource "aws_iam_role_policy_attachment" "ssm" { | |
count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0 | |
policy_arn = aws_iam_policy.ssm[count.index].arn | |
role = aws_iam_role.this[0].name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment