Skip to content

Commit ab9fa5a

Browse files
Upgrade module to Terraform 0.12.x (claranet#49)
* Upgrade module to Terraform 0.12.x I could not find a way to support 0.11.x and 0.12.x at the same time, so this goes all-in on 0.12.x features and cleans up a lot of code in the process. terraform-docs does not support 0.12.x yet so I've removed the Makefile and manually wrote the README. I have tried all tests in the test directory and they all worked. * Clean up remaining 0.11.x code * Add Terraform version compatibility table * Remove Terraform from requirements as it's obvious * Use dynamic blocks properly
1 parent d034421 commit ab9fa5a

File tree

16 files changed

+211
-363
lines changed

16 files changed

+211
-363
lines changed

Diff for: Makefile

-23
This file was deleted.

Diff for: README.md

+30-38
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ This Terraform module creates and uploads an AWS Lambda function and hides the u
1616
* Python 2.7 or higher
1717
* Linux/Unix/Windows
1818

19+
## Terraform version compatibility
20+
21+
| Module version | Terraform version |
22+
|----------------|-------------------|
23+
| 1.x.x | 0.12.x |
24+
| 0.x.x | 0.11.x |
25+
1926
## Usage
2027

2128
```js
@@ -32,64 +39,49 @@ module "lambda" {
3239
source_path = "${path.module}/lambda.py"
3340

3441
// Attach a policy.
35-
attach_policy = true
36-
policy = "${data.aws_iam_policy_document.lambda.json}"
42+
policy = data.aws_iam_policy_document.lambda.json
3743

3844
// Add a dead letter queue.
39-
attach_dead_letter_config = true
4045
dead_letter_config {
41-
target_arn = "${var.dead_letter_queue_arn}"
46+
target_arn = aws_sqs_queue.dlq.arn
4247
}
4348

4449
// Add environment variables.
4550
environment {
4651
variables {
47-
SLACK_URL = "${var.slack_url}"
52+
SLACK_URL = var.slack_url
4853
}
4954
}
5055

5156
// Deploy into a VPC.
52-
attach_vpc_config = true
5357
vpc_config {
54-
subnet_ids = ["${aws_subnet.test.id}"]
55-
security_group_ids = ["${aws_security_group.test.id}"]
58+
subnet_ids = [aws_subnet.test.id]
59+
security_group_ids = [aws_security_group.test.id]
5660
}
5761
}
5862
```
5963

60-
### NB - Multi-region usage
61-
62-
IAM and Lambda function names need to be globally unique within your account.
63-
If you will be deploying this template to multiple regions, you must make the
64-
function name unique per region, for example by setting
65-
`function_name = "deployment-deploy-status-${data.aws_region.current.name}"`
66-
6764
## Inputs
6865

66+
Inputs for this module are the same as the [aws_lambda_function](https://www.terraform.io/docs/providers/aws/r/lambda_function.html) resource with the following additional arguments:
67+
6968
| Name | Description | Type | Default | Required |
70-
|------|-------------|:----:|:-----:|:-----:|
71-
| attach\_dead\_letter\_config | Set this to true if using the dead_letter_config variable | string | `"false"` | no |
72-
| attach\_policy | Set this to true if using the policy variable | string | `"false"` | no |
73-
| attach\_vpc\_config | Set this to true if using the vpc_config variable | string | `"false"` | no |
74-
| build\_command | The command that creates the Lambda package zip file | string | `"python build.py '$filename' '$runtime' '$source'"` | no |
75-
| build\_paths | The files or directories used by the build command, to trigger new Lambda package builds whenever build scripts change | list | `<list>` | no |
76-
| dead\_letter\_config | Dead letter configuration for the Lambda function | map | `<map>` | no |
77-
| description | Description of what your Lambda function does | string | `"Managed by Terraform"` | no |
78-
| enable\_cloudwatch\_logs | Set this to false to disable logging your Lambda output to CloudWatch Logs | string | `"true"` | no |
79-
| environment | Environment configuration for the Lambda function | map | `<map>` | no |
80-
| function\_name | A unique name for your Lambda function (and related IAM resources) | string | n/a | yes |
81-
| handler | The function entrypoint in your code | string | n/a | yes |
82-
| 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 | string | `"false"` | no |
83-
| layers | List of Lambda Layer Version ARNs to attach to your Lambda Function. | list | `<list>` | no |
84-
| memory\_size | Amount of memory in MB your Lambda function can use at runtime | string | `"128"` | no |
85-
| policy | An addional policy to attach to the Lambda function | string | `""` | no |
86-
| publish | Whether to publish creation/change as new Lambda Function Version | string | `"false"` | no |
87-
| reserved\_concurrent\_executions | The amount of reserved concurrent executions for this Lambda function | string | `"-1"` | no |
88-
| runtime | The runtime environment for the Lambda function | string | n/a | yes |
89-
| source\_path | The source file or directory containing your Lambda source code | string | n/a | yes |
90-
| tags | A mapping of tags | map | `<map>` | no |
91-
| timeout | The amount of time your Lambda function had to run in seconds | string | `"10"` | no |
92-
| vpc\_config | VPC configuration for the Lambda function | map | `<map>` | no |
69+
|------|-------------|------|---------|----------|
70+
| **source\_path** | The absolute path to a local file or directory containing your Lambda source code | string | | yes |
71+
| build\_command | The command to run to create the Lambda package zip file | string | `"python build.py '$filename' '$runtime' '$source'"` | no |
72+
| 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 |
73+
| cloudwatch\_logs | Set this to false to disable logging your Lambda output to CloudWatch Logs | bool | true | no |
74+
| 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 |
76+
77+
The following arguments from the [aws_lambda_function](https://www.terraform.io/docs/providers/aws/r/lambda_function.html) resource are not supported:
78+
79+
* filename (use source\_path instead)
80+
* role (one is automatically created)
81+
* s3_bucket
82+
* s3_key
83+
* s3_object_version
84+
* source_code_hash (changes are handled automatically)
9385

9486
## Outputs
9587

Diff for: archive.tf

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1-
locals {
2-
module_relpath_11 = "${substr(path.module, length(path.cwd) + 1, -1)}"
3-
module_relpath_12 = "${path.module}"
4-
module_relpath = "${path.cwd == substr(path.module, 0, length(path.cwd)) ? local.module_relpath_11 : local.module_relpath_12}"
5-
}
6-
71
# Generates a filename for the zip archive based on the contents of the files
82
# in source_path. The filename will change when the source code changes.
93
data "external" "archive" {
104
program = ["python", "${path.module}/hash.py"]
115

126
query = {
13-
build_command = "${var.build_command}"
14-
build_paths = "${jsonencode(var.build_paths)}"
15-
module_relpath = "${local.module_relpath}"
16-
runtime = "${var.runtime}"
17-
source_path = "${var.source_path}"
7+
build_command = var.build_command
8+
build_paths = jsonencode(var.build_paths)
9+
module_relpath = path.module
10+
runtime = var.runtime
11+
source_path = var.source_path
1812
}
1913
}
2014

2115
# Build the zip archive whenever the filename changes.
2216
resource "null_resource" "archive" {
2317
triggers = {
24-
filename = "${lookup(data.external.archive.result, "filename")}"
18+
filename = lookup(data.external.archive.result, "filename")
2519
}
2620

2721
provisioner "local-exec" {
28-
command = "${lookup(data.external.archive.result, "build_command")}"
29-
working_dir = "${path.module}"
22+
command = lookup(data.external.archive.result, "build_command")
23+
working_dir = path.module
3024
}
3125
}
3226

@@ -39,9 +33,9 @@ data "external" "built" {
3933
program = ["python", "${path.module}/built.py"]
4034

4135
query = {
42-
build_command = "${lookup(data.external.archive.result, "build_command")}"
43-
filename_old = "${lookup(null_resource.archive.triggers, "filename")}"
44-
filename_new = "${lookup(data.external.archive.result, "filename")}"
45-
module_relpath = "${local.module_relpath}"
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")
39+
module_relpath = path.module
4640
}
4741
}

Diff for: iam.tf

+33-31
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ data "aws_iam_policy_document" "assume_role" {
77

88
principals {
99
type = "Service"
10-
identifiers = ["${slice(list("lambda.amazonaws.com", "edgelambda.amazonaws.com"), 0, var.lambda_at_edge ? 2 : 1)}"]
10+
identifiers = slice(list("lambda.amazonaws.com", "edgelambda.amazonaws.com"), 0, var.lambda_at_edge ? 2 : 1)
1111
}
1212
}
1313
}
1414

1515
resource "aws_iam_role" "lambda" {
16-
name = "${var.function_name}"
17-
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
18-
tags = "${var.tags}"
16+
name = var.function_name
17+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
18+
tags = var.tags
1919
}
2020

2121
# Attach a policy for logs.
2222

2323
locals {
2424
lambda_log_group_arn = "arn:${data.aws_partition.current.partition}:logs:*:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/${var.function_name}"
2525
lambda_edge_log_group_arn = "arn:${data.aws_partition.current.partition}:logs:*:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/us-east-1.${var.function_name}"
26-
log_group_arns = ["${slice(list(local.lambda_log_group_arn, local.lambda_edge_log_group_arn), 0, var.lambda_at_edge ? 2 : 1)}"]
26+
log_group_arns = slice(list(local.lambda_log_group_arn, local.lambda_edge_log_group_arn), 0, var.lambda_at_edge ? 2 : 1)
2727
}
2828

2929
data "aws_iam_policy_document" "logs" {
30-
count = "${var.enable_cloudwatch_logs ? 1 : 0}"
30+
count = var.cloudwatch_logs ? 1 : 0
3131

3232
statement {
3333
effect = "Allow"
@@ -49,29 +49,29 @@ data "aws_iam_policy_document" "logs" {
4949
"logs:PutLogEvents",
5050
]
5151

52-
resources = ["${concat(formatlist("%v:*", local.log_group_arns), formatlist("%v:*:*", local.log_group_arns))}"]
52+
resources = concat(formatlist("%v:*", local.log_group_arns), formatlist("%v:*:*", local.log_group_arns))
5353
}
5454
}
5555

5656
resource "aws_iam_policy" "logs" {
57-
count = "${var.enable_cloudwatch_logs ? 1 : 0}"
57+
count = var.cloudwatch_logs ? 1 : 0
5858

5959
name = "${var.function_name}-logs"
60-
policy = "${join("", data.aws_iam_policy_document.logs.*.json)}"
60+
policy = data.aws_iam_policy_document.logs[0].json
6161
}
6262

6363
resource "aws_iam_policy_attachment" "logs" {
64-
count = "${var.enable_cloudwatch_logs ? 1 : 0}"
64+
count = var.cloudwatch_logs ? 1 : 0
6565

6666
name = "${var.function_name}-logs"
67-
roles = ["${aws_iam_role.lambda.name}"]
68-
policy_arn = "${join("", aws_iam_policy.logs.*.arn)}"
67+
roles = [aws_iam_role.lambda.name]
68+
policy_arn = aws_iam_policy.logs[0].arn
6969
}
7070

7171
# Attach an additional policy required for the dead letter config.
7272

7373
data "aws_iam_policy_document" "dead_letter" {
74-
count = "${var.attach_dead_letter_config ? 1 : 0}"
74+
count = var.dead_letter_config == null ? 0 : 1
7575

7676
statement {
7777
effect = "Allow"
@@ -82,29 +82,31 @@ data "aws_iam_policy_document" "dead_letter" {
8282
]
8383

8484
resources = [
85-
"${lookup(var.dead_letter_config, "target_arn", "")}",
85+
var.dead_letter_config.target_arn,
8686
]
8787
}
8888
}
8989

9090
resource "aws_iam_policy" "dead_letter" {
91-
count = "${var.attach_dead_letter_config ? 1 : 0}"
91+
count = var.dead_letter_config == null ? 0 : 1
9292

9393
name = "${var.function_name}-dl"
94-
policy = "${join("", data.aws_iam_policy_document.dead_letter.*.json)}"
94+
policy = data.aws_iam_policy_document.dead_letter[0].json
9595
}
9696

9797
resource "aws_iam_policy_attachment" "dead_letter" {
98-
count = "${var.attach_dead_letter_config ? 1 : 0}"
98+
count = var.dead_letter_config == null ? 0 : 1
9999

100100
name = "${var.function_name}-dl"
101-
roles = ["${aws_iam_role.lambda.name}"]
102-
policy_arn = "${join("", aws_iam_policy.dead_letter.*.arn)}"
101+
roles = [aws_iam_role.lambda.name]
102+
policy_arn = aws_iam_policy.dead_letter[0].arn
103103
}
104104

105105
# Attach an additional policy required for the VPC config
106106

107107
data "aws_iam_policy_document" "network" {
108+
count = var.vpc_config == null ? 0 : 1
109+
108110
statement {
109111
effect = "Allow"
110112

@@ -121,33 +123,33 @@ data "aws_iam_policy_document" "network" {
121123
}
122124

123125
resource "aws_iam_policy" "network" {
124-
count = "${var.attach_vpc_config ? 1 : 0}"
126+
count = var.vpc_config == null ? 0 : 1
125127

126128
name = "${var.function_name}-network"
127-
policy = "${data.aws_iam_policy_document.network.json}"
129+
policy = data.aws_iam_policy_document.network[0].json
128130
}
129131

130132
resource "aws_iam_policy_attachment" "network" {
131-
count = "${var.attach_vpc_config ? 1 : 0}"
133+
count = var.vpc_config == null ? 0 : 1
132134

133135
name = "${var.function_name}-network"
134-
roles = ["${aws_iam_role.lambda.name}"]
135-
policy_arn = "${join("", aws_iam_policy.network.*.arn)}"
136+
roles = [aws_iam_role.lambda.name]
137+
policy_arn = aws_iam_policy.network[0].arn
136138
}
137139

138140
# Attach an additional policy if provided.
139141

140142
resource "aws_iam_policy" "additional" {
141-
count = "${var.attach_policy ? 1 : 0}"
143+
count = var.policy == null ? 0 : 1
142144

143-
name = "${var.function_name}"
144-
policy = "${var.policy}"
145+
name = var.function_name
146+
policy = var.policy
145147
}
146148

147149
resource "aws_iam_policy_attachment" "additional" {
148-
count = "${var.attach_policy ? 1 : 0}"
150+
count = var.policy == null ? 0 : 1
149151

150-
name = "${var.function_name}"
151-
roles = ["${aws_iam_role.lambda.name}"]
152-
policy_arn = "${join("", aws_iam_policy.additional.*.arn)}"
152+
name = var.function_name
153+
roles = [aws_iam_role.lambda.name]
154+
policy_arn = aws_iam_policy.additional[0].arn
153155
}

0 commit comments

Comments
 (0)