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

Commit 40a7385

Browse files
authored
Merge pull request #80 from claranet/jnesbitt/add-enabled-option
Jnesbitt/add enabled option
2 parents c86bc20 + 50b946e commit 40a7385

File tree

8 files changed

+51
-30
lines changed

8 files changed

+51
-30
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Inputs for this module are the same as the [aws_lambda_function](https://www.ter
7979
| 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 |
8080
| policy | An additional policy to attach to the Lambda function role | `object({json=string})` | | no |
8181
| trusted\_entities | Additional trusted entities for the Lambda function. The lambda.amazonaws.com (and edgelambda.amazonaws.com if lambda\_at\_edge is true) is always set | `list(string)` | | no |
82+
| enabled | Enabling and disaling of resources | `bool` | `true` | no |
8283

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

archive.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Generates a filename for the zip archive based on the contents of the files
22
# in source_path. The filename will change when the source code changes.
33
data "external" "archive" {
4+
count = var.enabled ? 1 : 0
5+
46
program = ["python", "${path.module}/hash.py"]
57

68
query = {
@@ -14,12 +16,14 @@ data "external" "archive" {
1416

1517
# Build the zip archive whenever the filename changes.
1618
resource "null_resource" "archive" {
19+
count = var.enabled ? 1 : 0
20+
1721
triggers = {
18-
filename = lookup(data.external.archive.result, "filename")
22+
filename = lookup(data.external.archive[0].result, "filename")
1923
}
2024

2125
provisioner "local-exec" {
22-
command = lookup(data.external.archive.result, "build_command")
26+
command = lookup(data.external.archive[0].result, "build_command")
2327
working_dir = path.module
2428
}
2529
}
@@ -30,12 +34,14 @@ resource "null_resource" "archive" {
3034
# deletes the Lambda function. If the file is rebuilt here, the build
3135
# output is unfortunately invisible.
3236
data "external" "built" {
37+
count = var.enabled ? 1 : 0
38+
3339
program = ["python", "${path.module}/built.py"]
3440

3541
query = {
36-
build_command = lookup(data.external.archive.result, "build_command")
37-
filename_old = lookup(null_resource.archive.triggers, "filename")
38-
filename_new = lookup(data.external.archive.result, "filename")
42+
build_command = lookup(data.external.archive[0].result, "build_command")
43+
filename_old = lookup(null_resource.archive[0].triggers, "filename")
44+
filename_new = lookup(data.external.archive[0].result, "filename")
3945
module_relpath = path.module
4046
}
4147
}

iam.tf

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Create the role.
22

33
data "aws_iam_policy_document" "assume_role" {
4+
count = var.enabled ? 1 : 0
5+
46
statement {
57
effect = "Allow"
68
actions = ["sts:AssumeRole"]
@@ -13,8 +15,10 @@ data "aws_iam_policy_document" "assume_role" {
1315
}
1416

1517
resource "aws_iam_role" "lambda" {
18+
count = var.enabled ? 1 : 0
19+
1620
name = var.function_name
17-
assume_role_policy = data.aws_iam_policy_document.assume_role.json
21+
assume_role_policy = data.aws_iam_policy_document.assume_role[0].json
1822
tags = var.tags
1923
}
2024

@@ -27,7 +31,7 @@ locals {
2731
}
2832

2933
data "aws_iam_policy_document" "logs" {
30-
count = var.cloudwatch_logs ? 1 : 0
34+
count = var.enabled && var.cloudwatch_logs ? 1 : 0
3135

3236
statement {
3337
effect = "Allow"
@@ -54,24 +58,24 @@ data "aws_iam_policy_document" "logs" {
5458
}
5559

5660
resource "aws_iam_policy" "logs" {
57-
count = var.cloudwatch_logs ? 1 : 0
61+
count = var.enabled && var.cloudwatch_logs ? 1 : 0
5862

5963
name = "${var.function_name}-logs"
6064
policy = data.aws_iam_policy_document.logs[0].json
6165
}
6266

6367
resource "aws_iam_policy_attachment" "logs" {
64-
count = var.cloudwatch_logs ? 1 : 0
68+
count = var.enabled && var.cloudwatch_logs ? 1 : 0
6569

6670
name = "${var.function_name}-logs"
67-
roles = [aws_iam_role.lambda.name]
71+
roles = [aws_iam_role.lambda[0].name]
6872
policy_arn = aws_iam_policy.logs[0].arn
6973
}
7074

7175
# Attach an additional policy required for the dead letter config.
7276

7377
data "aws_iam_policy_document" "dead_letter" {
74-
count = var.dead_letter_config == null ? 0 : 1
78+
count = var.dead_letter_config == null ? 0 : var.enabled ? 1 : 0
7579

7680
statement {
7781
effect = "Allow"
@@ -88,24 +92,24 @@ data "aws_iam_policy_document" "dead_letter" {
8892
}
8993

9094
resource "aws_iam_policy" "dead_letter" {
91-
count = var.dead_letter_config == null ? 0 : 1
95+
count = var.dead_letter_config == null ? 0 : var.enabled ? 1 : 0
9296

9397
name = "${var.function_name}-dl"
9498
policy = data.aws_iam_policy_document.dead_letter[0].json
9599
}
96100

97101
resource "aws_iam_policy_attachment" "dead_letter" {
98-
count = var.dead_letter_config == null ? 0 : 1
102+
count = var.dead_letter_config == null ? 0 : var.enabled ? 1 : 0
99103

100104
name = "${var.function_name}-dl"
101-
roles = [aws_iam_role.lambda.name]
105+
roles = [aws_iam_role.lambda[0].name]
102106
policy_arn = aws_iam_policy.dead_letter[0].arn
103107
}
104108

105109
# Attach an additional policy required for the VPC config
106110

107111
data "aws_iam_policy_document" "network" {
108-
count = var.vpc_config == null ? 0 : 1
112+
count = var.vpc_config == null ? 0 : var.enabled ? 1 : 0
109113

110114
statement {
111115
effect = "Allow"
@@ -123,33 +127,33 @@ data "aws_iam_policy_document" "network" {
123127
}
124128

125129
resource "aws_iam_policy" "network" {
126-
count = var.vpc_config == null ? 0 : 1
130+
count = var.vpc_config == null ? 0 : var.enabled ? 1 : 0
127131

128132
name = "${var.function_name}-network"
129133
policy = data.aws_iam_policy_document.network[0].json
130134
}
131135

132136
resource "aws_iam_policy_attachment" "network" {
133-
count = var.vpc_config == null ? 0 : 1
137+
count = var.vpc_config == null ? 0 : var.enabled ? 1 : 0
134138

135139
name = "${var.function_name}-network"
136-
roles = [aws_iam_role.lambda.name]
140+
roles = [aws_iam_role.lambda[0].name]
137141
policy_arn = aws_iam_policy.network[0].arn
138142
}
139143

140144
# Attach an additional policy if provided.
141145

142146
resource "aws_iam_policy" "additional" {
143-
count = var.policy == null ? 0 : 1
147+
count = var.policy == null ? 0 : var.enabled ? 1 : 0
144148

145149
name = var.function_name
146150
policy = var.policy.json
147151
}
148152

149153
resource "aws_iam_policy_attachment" "additional" {
150-
count = var.policy == null ? 0 : 1
154+
count = var.policy == null ? 0 : var.enabled ? 1 : 0
151155

152156
name = var.function_name
153-
roles = [aws_iam_role.lambda.name]
157+
roles = [aws_iam_role.lambda[0].name]
154158
policy_arn = aws_iam_policy.additional[0].arn
155159
}

lambda.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
resource "aws_lambda_function" "lambda" {
2-
2+
count = var.enabled ? 1 : 0
33
function_name = var.function_name
44
description = var.description
5-
role = aws_iam_role.lambda.arn
5+
role = aws_iam_role.lambda[0].arn
66
handler = var.handler
77
memory_size = var.memory_size
88
reserved_concurrent_executions = var.reserved_concurrent_executions
@@ -14,7 +14,7 @@ resource "aws_lambda_function" "lambda" {
1414

1515
# Use a generated filename to determine when the source code has changed.
1616

17-
filename = data.external.built.result.filename
17+
filename = data.external.built[0].result.filename
1818
depends_on = [null_resource.archive]
1919

2020
# Add dynamic blocks based on variables.

outputs.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
output "function_arn" {
22
description = "The ARN of the Lambda function"
3-
value = aws_lambda_function.lambda.arn
3+
value = join("", aws_lambda_function.lambda.*.arn)
44
}
55

66
output "function_invoke_arn" {
77
description = "The Invoke ARN of the Lambda function"
8-
value = aws_lambda_function.lambda.invoke_arn
8+
value = join("", aws_lambda_function.lambda.*.invoke_arn)
99
}
1010

1111
output "function_name" {
1212
description = "The name of the Lambda function"
13-
value = aws_lambda_function.lambda.function_name
13+
value = join("", aws_lambda_function.lambda.*.function_name)
1414
}
1515

1616
output "function_qualified_arn" {
1717
description = "The qualified ARN of the Lambda function"
18-
value = aws_lambda_function.lambda.qualified_arn
18+
value = join("", aws_lambda_function.lambda.*.qualified_arn)
1919
}
2020

2121
output "role_arn" {
2222
description = "The ARN of the IAM role created for the Lambda function"
23-
value = aws_iam_role.lambda.arn
23+
value = join("", aws_iam_role.lambda.*.arn)
2424
}
2525

2626
output "role_name" {
2727
description = "The name of the IAM role created for the Lambda function"
28-
value = aws_iam_role.lambda.name
28+
value = join("", aws_iam_role.lambda.*.name)
2929
}

tests/dead-letter-queue/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ module "lambda" {
3131
dead_letter_config = {
3232
target_arn = aws_sqs_queue.dlq.arn
3333
}
34+
35+
enabled = true
3436
}

tests/environment-variables/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ module "lambda" {
3030
ARN = aws_iam_user.test.arn
3131
}
3232
}
33+
34+
enabled = true
3335
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ variable "vpc_config" {
134134
})
135135
default = null
136136
}
137+
138+
variable "enabled" {
139+
description = "Enable or disable the Lambda resources."
140+
type = bool
141+
default = true
142+
}

0 commit comments

Comments
 (0)