Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Allow use of existing policy #25

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module "lambda" {
// Attach a policy.
attach_policy = true
policy = "${data.aws_iam_policy_document.lambda.json}"
attach_policy_arn = true
policy_arn = "${data.aws_iam_policy.lambda.arn}"

// Add a dead letter queue.
attach_dead_letter_config = true
Expand Down
19 changes: 5 additions & 14 deletions iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,10 @@ resource "aws_iam_policy_attachment" "network" {
policy_arn = "${aws_iam_policy.network.arn}"
}

# Attach an additional policy if provided.
# Attach an additional policies if provided.

resource "aws_iam_policy" "additional" {
count = "${var.attach_policy ? 1 : 0}"

name = "${var.function_name}"
policy = "${var.policy}"
}

resource "aws_iam_policy_attachment" "additional" {
count = "${var.attach_policy ? 1 : 0}"

name = "${var.function_name}"
roles = ["${aws_iam_role.lambda.name}"]
policy_arn = "${aws_iam_policy.additional.arn}"
resource "aws_iam_role_policy_attachment" "additional" {
reisingerf marked this conversation as resolved.
Show resolved Hide resolved
count = "${local.policy_arn_count}"
role = "${aws_iam_role.lambda.name}"
policy_arn = "${var.policy_arns[count.index]}"
}
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ output "function_arn" {
value = "${element(concat(aws_lambda_function.lambda.*.arn, aws_lambda_function.lambda_with_dl.*.arn, aws_lambda_function.lambda_with_vpc.*.arn, aws_lambda_function.lambda_with_dl_and_vpc.*.arn), 0)}"
}

output "function_invoke_arn" {
description = "The ARN of the Lambda function"
value = "${element(concat(aws_lambda_function.lambda.*.invoke_arn, aws_lambda_function.lambda_with_dl.*.invoke_arn, aws_lambda_function.lambda_with_vpc.*.invoke_arn, aws_lambda_function.lambda_with_dl_and_vpc.*.invoke_arn), 0)}"
}

output "function_name" {
description = "The name of the Lambda function"
value = "${element(concat(aws_lambda_function.lambda.*.function_name, aws_lambda_function.lambda_with_dl.*.function_name, aws_lambda_function.lambda_with_vpc.*.function_name, aws_lambda_function.lambda_with_dl_and_vpc.*.function_name), 0)}"
Expand Down
15 changes: 5 additions & 10 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ variable "tags" {
default = {}
}

variable "policy" {
description = "An addional policy to attach to the Lambda function"
type = "string"
default = ""
}

variable "attach_policy" {
description = "Set this to true if using the policy variable"
type = "string"
default = false
variable "policy_arns" {
description = "Addional policies (ARNs) to attach to the Lambda function."
type = "list"
default = []
}

variable "enable_cloudwatch_logs" {
Expand All @@ -123,4 +117,5 @@ variable "lambda_at_edge" {
locals {
publish = "${var.lambda_at_edge ? true : var.publish}"
timeout = "${var.lambda_at_edge ? min(var.timeout, 5) : var.timeout}"
policy_arn_count = "${length(var.policy_arns)}"
}