Skip to content

Commit 5a060fd

Browse files
authored
Add enabled flag, fix IAM permissions (#6)
1 parent 7167461 commit 5a060fd

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ We literally have [*hundreds of terraform modules*][terraform_modules] that are
4848

4949
## Usage
5050

51+
52+
**IMPORTANT:** The `master` branch is used in `source` just as an example. In your code, do not pin to `master` because there may be breaking changes between releases.
53+
Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases](https://github.com/cloudposse/terraform-aws-lambda-elasticsearch-cleanup/releases).
54+
55+
5156
```hcl
5257
module "elasticsearch_cleanup" {
5358
source = "../"
@@ -92,6 +97,7 @@ is given
9297
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
9398
| delete_after | Number of days to preserve | string | `15` | no |
9499
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
100+
| enabled | This module will not create any resources unless enabled is set to "true" | string | `true` | no |
95101
| es_domain_arn | The Elasticsearch domain ARN | string | - | yes |
96102
| es_endpoint | The Elasticsearch endpoint for the Lambda function to connect to | string | - | yes |
97103
| es_security_group_id | The Elasticsearch cluster security group ID | string | - | yes |
@@ -196,7 +202,7 @@ In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
196202

197203
## Copyright
198204

199-
Copyright © 2017-2018 [Cloud Posse, LLC](https://cpco.io/copyright)
205+
Copyright © 2017-2019 [Cloud Posse, LLC](https://cpco.io/copyright)
200206

201207

202208

docs/terraform.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ is given
1212
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
1313
| delete_after | Number of days to preserve | string | `15` | no |
1414
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
15+
| enabled | This module will not create any resources unless enabled is set to "true" | string | `true` | no |
1516
| es_domain_arn | The Elasticsearch domain ARN | string | - | yes |
1617
| es_endpoint | The Elasticsearch endpoint for the Lambda function to connect to | string | - | yes |
1718
| es_security_group_id | The Elasticsearch cluster security group ID | string | - | yes |

main.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ data "aws_iam_policy_document" "default" {
5454
effect = "Allow"
5555

5656
resources = [
57-
"${var.es_domain_arn}",
57+
"${var.es_domain_arn}/*",
5858
]
5959
}
6060
}
@@ -88,6 +88,7 @@ locals {
8888
# Resources
8989
#--------------------------------------------------------------
9090
resource "aws_lambda_function" "default" {
91+
count = "${var.enabled == "true" ? 1 : 0}"
9192
filename = "${module.artifact.file}"
9293
function_name = "${local.function_name}"
9394
description = "${local.function_name}"
@@ -115,13 +116,15 @@ resource "aws_lambda_function" "default" {
115116
}
116117

117118
resource "aws_security_group" "default" {
119+
count = "${var.enabled == "true" ? 1 : 0}"
118120
name = "${local.function_name}"
119121
description = "${local.function_name}"
120122
vpc_id = "${var.vpc_id}"
121123
tags = "${module.label.tags}"
122124
}
123125

124126
resource "aws_security_group_rule" "udp_dns_egress_from_lambda" {
127+
count = "${var.enabled == "true" ? 1 : 0}"
125128
description = "Allow outbound UDP traffic from Lambda Elasticsearch cleanup to DNS"
126129
type = "egress"
127130
from_port = 53
@@ -132,6 +135,7 @@ resource "aws_security_group_rule" "udp_dns_egress_from_lambda" {
132135
}
133136

134137
resource "aws_security_group_rule" "tcp_dns_egress_from_lambda" {
138+
count = "${var.enabled == "true" ? 1 : 0}"
135139
description = "Allow outbound TCP traffic from Lambda Elasticsearch cleanup to DNS"
136140
type = "egress"
137141
from_port = 53
@@ -142,6 +146,7 @@ resource "aws_security_group_rule" "tcp_dns_egress_from_lambda" {
142146
}
143147

144148
resource "aws_security_group_rule" "egress_from_lambda_to_es_cluster" {
149+
count = "${var.enabled == "true" ? 1 : 0}"
145150
description = "Allow outbound traffic from Lambda Elasticsearch cleanup SG to Elasticsearch SG"
146151
type = "egress"
147152
from_port = 443
@@ -152,6 +157,7 @@ resource "aws_security_group_rule" "egress_from_lambda_to_es_cluster" {
152157
}
153158

154159
resource "aws_security_group_rule" "ingress_to_es_cluster_from_lambda" {
160+
count = "${var.enabled == "true" ? 1 : 0}"
155161
description = "Allow inbound traffic to Elasticsearch domain from Lambda Elasticsearch cleanup SG"
156162
type = "ingress"
157163
from_port = 443
@@ -162,29 +168,34 @@ resource "aws_security_group_rule" "ingress_to_es_cluster_from_lambda" {
162168
}
163169

164170
resource "aws_iam_role" "default" {
171+
count = "${var.enabled == "true" ? 1 : 0}"
165172
name = "${local.function_name}"
166173
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
167174
tags = "${module.label.tags}"
168175
}
169176

170177
resource "aws_iam_role_policy" "default" {
178+
count = "${var.enabled == "true" ? 1 : 0}"
171179
name = "${local.function_name}"
172180
role = "${aws_iam_role.default.name}"
173181
policy = "${data.aws_iam_policy_document.default.json}"
174182
}
175183

176184
resource "aws_iam_role_policy_attachment" "default" {
185+
count = "${var.enabled == "true" ? 1 : 0}"
177186
role = "${aws_iam_role.default.name}"
178187
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
179188
}
180189

181190
resource "aws_cloudwatch_event_rule" "default" {
191+
count = "${var.enabled == "true" ? 1 : 0}"
182192
name = "${local.function_name}"
183193
description = "${local.function_name}"
184194
schedule_expression = "${var.schedule}"
185195
}
186196

187197
resource "aws_lambda_permission" "default" {
198+
count = "${var.enabled == "true" ? 1 : 0}"
188199
statement_id = "AllowExecutionFromCloudWatch"
189200
action = "lambda:InvokeFunction"
190201
function_name = "${aws_lambda_function.default.arn}"
@@ -193,6 +204,7 @@ resource "aws_lambda_permission" "default" {
193204
}
194205

195206
resource "aws_cloudwatch_event_target" "default" {
207+
count = "${var.enabled == "true" ? 1 : 0}"
196208
target_id = "${local.function_name}"
197209
rule = "${aws_cloudwatch_event_rule.default.name}"
198210
arn = "${aws_lambda_function.default.arn}"

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
output "security_group_id" {
2-
value = "${aws_security_group.default.id}"
2+
value = "${join(",",aws_security_group.default.*.id)}"
33
description = "Security Group ID of the Lambda "
44
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "enabled" {
2+
type = "string"
3+
default = "true"
4+
description = "This module will not create any resources unless enabled is set to \"true\""
5+
}
6+
17
variable "es_endpoint" {
28
type = "string"
39
description = "The Elasticsearch endpoint for the Lambda function to connect to"

0 commit comments

Comments
 (0)