Skip to content

Commit 5f3b8be

Browse files
Use object for var.policy to avoid computed count errors (claranet#53)
1 parent 2397f29 commit 5f3b8be

File tree

5 files changed

+107
-9
lines changed

5 files changed

+107
-9
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ module "lambda" {
3939
source_path = "${path.module}/lambda.py"
4040

4141
// Attach a policy.
42-
policy = data.aws_iam_policy_document.lambda.json
42+
policy = {
43+
json = data.aws_iam_policy_document.lambda.json
44+
}
4345

4446
// Add a dead letter queue.
4547
dead_letter_config = {
@@ -72,7 +74,7 @@ Inputs for this module are the same as the [aws_lambda_function](https://www.ter
7274
| build\_paths | The files or directories used by the build command, to trigger new Lambda package builds whenever build scripts change | list(string) | `["build.py"]` | no |
7375
| cloudwatch\_logs | Set this to false to disable logging your Lambda output to CloudWatch Logs | bool | true | no |
7476
| lambda\_at\_edge | Set this to true if using Lambda@Edge, to enable publishing, limit the timeout, and allow edgelambda.amazonaws.com to invoke the function | bool | false | no |
75-
| policy | An addional policy to attach to the Lambda function | string | | no |
77+
| policy | An additional policy to attach to the Lambda function role | object({json=string}) | | no |
7678

7779
The following arguments from the [aws_lambda_function](https://www.terraform.io/docs/providers/aws/r/lambda_function.html) resource are not supported:
7880

iam.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ resource "aws_iam_policy" "additional" {
143143
count = var.policy == null ? 0 : 1
144144

145145
name = var.function_name
146-
policy = var.policy
146+
policy = var.policy.json
147147
}
148148

149149
resource "aws_iam_policy_attachment" "additional" {

tests/policy/lambda.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def lambda_handler(event, context):
2+
if event['pass']:
3+
return True
4+
else:
5+
raise Exception('oh no')

tests/policy/main.tf

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
terraform {
2+
backend "local" {
3+
path = "terraform.tfstate"
4+
}
5+
}
6+
7+
provider "aws" {
8+
region = "eu-west-1"
9+
}
10+
11+
resource "random_id" "name" {
12+
byte_length = 6
13+
prefix = "terraform-aws-lambda-policy-"
14+
}
15+
16+
resource "aws_sqs_queue" "test" {
17+
name = random_id.name.hex
18+
}
19+
20+
data "aws_iam_policy_document" "computed" {
21+
statement {
22+
effect = "Allow"
23+
24+
actions = [
25+
"sqs:SendMessage",
26+
]
27+
28+
resources = [
29+
aws_sqs_queue.test.arn,
30+
]
31+
}
32+
}
33+
34+
data "aws_iam_policy_document" "known" {
35+
statement {
36+
effect = "Deny"
37+
38+
actions = [
39+
"sqs:SendMessage",
40+
]
41+
42+
resources = [
43+
"*",
44+
]
45+
}
46+
}
47+
48+
module "lambda_with_computed_policy" {
49+
source = "../../"
50+
51+
function_name = "${random_id.name.hex}-computed"
52+
description = "Test attaching policy in terraform-aws-lambda"
53+
handler = "lambda.lambda_handler"
54+
runtime = "python3.6"
55+
56+
source_path = "${path.module}/lambda.py"
57+
58+
policy = {
59+
json = data.aws_iam_policy_document.computed.json
60+
}
61+
}
62+
63+
64+
module "lambda_with_known_policy" {
65+
source = "../../"
66+
67+
function_name = "${random_id.name.hex}-known"
68+
description = "Test attaching policy in terraform-aws-lambda"
69+
handler = "lambda.lambda_handler"
70+
runtime = "python3.6"
71+
72+
source_path = "${path.module}/lambda.py"
73+
74+
policy = {
75+
json = data.aws_iam_policy_document.known.json
76+
}
77+
}
78+
79+
80+
module "lambda_without_policy" {
81+
source = "../../"
82+
83+
function_name = "${random_id.name.hex}-without"
84+
description = "Test attaching policy in terraform-aws-lambda"
85+
handler = "lambda.lambda_handler"
86+
runtime = "python3.6"
87+
88+
source_path = "${path.module}/lambda.py"
89+
}

variables.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ variable "cloudwatch_logs" {
3737
default = true
3838
}
3939

40-
variable "policy" {
41-
description = "An addional policy to attach to the Lambda function"
42-
type = string
43-
default = null
44-
}
45-
4640
variable "lambda_at_edge" {
4741
description = "Set this to true if using Lambda@Edge, to enable publishing, limit the timeout, and allow edgelambda.amazonaws.com to invoke the function"
4842
type = bool
4943
default = false
5044
}
5145

46+
variable "policy" {
47+
description = "An additional policy to attach to the Lambda function role"
48+
type = object({
49+
json = string
50+
})
51+
default = null
52+
}
53+
5254
locals {
5355
publish = var.lambda_at_edge ? true : var.publish
5456
timeout = var.lambda_at_edge ? min(var.timeout, 5) : var.timeout

0 commit comments

Comments
 (0)