Skip to content

terraform 0.12 syntax, submodules modules version upgrade and count fixes #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
127 changes: 64 additions & 63 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,157 +74,158 @@ data "aws_iam_policy_document" "sns" {
}

data "aws_iam_policy_document" "default" {
source_json = "${data.aws_iam_policy_document.es_logs.json}"
override_json = "${length(var.sns_arn) > 0 ? data.aws_iam_policy_document.sns.json : "{}"}"
source_json = data.aws_iam_policy_document.es_logs.json
override_json = length(var.sns_arn) > 0 ? data.aws_iam_policy_document.sns.json : "{}"
}

# Modules
#--------------------------------------------------------------
module "label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.2.1"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
delimiter = "${var.delimiter}"
attributes = "${compact(concat(var.attributes, list("elasticsearch", "cleanup")))}"
tags = "${var.tags}"
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.4.0"
namespace = var.namespace
name = var.name
stage = var.stage
delimiter = var.delimiter
attributes = compact(concat(var.attributes, list("elasticsearch", "cleanup")))
tags = var.tags
enabled = "true"
}

module "artifact" {
source = "git::https://github.com/cloudposse/terraform-external-module-artifact.git?ref=tags/0.1.1"
source = "git::https://github.com/cloudposse/terraform-external-module-artifact.git?ref=tags/0.2.0"
filename = "lambda.zip"
module_name = "terraform-aws-lambda-elasticsearch-cleanup"
module_path = "${substr(path.module, length(path.cwd) + 1, -1)}"
module_path = path.module
# module_path = substr(path.module, length(path.cwd) + 1, -1)
}

# Locals
#--------------------------------------------------------------
locals {
function_name = "${module.label.id}"
function_name = module.label.id
}

# Resources
#--------------------------------------------------------------
resource "aws_lambda_function" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
filename = "${module.artifact.file}"
function_name = "${local.function_name}"
description = "${local.function_name}"
timeout = "${var.timeout}"
count = var.enabled == "true" ? 1 : 0
filename = module.artifact.file
function_name = local.function_name
description = local.function_name
timeout = var.timeout
runtime = "python${var.python_version}"
role = "${aws_iam_role.default.arn}"
role = join("", aws_iam_role.default.*.arn)
handler = "es-cleanup.lambda_handler"
source_code_hash = "${module.artifact.base64sha256}"
tags = "${module.label.tags}"
source_code_hash = module.artifact.base64sha256
tags = module.label.tags

environment {
variables = {
es_endpoint = "${var.es_endpoint}"
index = "${var.index}"
delete_after = "${var.delete_after}"
index_format = "${var.index_format}"
sns_arn = "${var.sns_arn}"
es_endpoint = var.es_endpoint
index = var.index
delete_after = var.delete_after
index_format = var.index_format
sns_arn = var.sns_arn
}
}

vpc_config {
subnet_ids = ["${var.subnet_ids}"]
security_group_ids = ["${aws_security_group.default.id}"]
subnet_ids = var.subnet_ids
security_group_ids = [join("", aws_security_group.default.*.id)]
}
}

resource "aws_security_group" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${local.function_name}"
description = "${local.function_name}"
vpc_id = "${var.vpc_id}"
tags = "${module.label.tags}"
count = var.enabled == "true" ? 1 : 0
name = local.function_name
description = local.function_name
vpc_id = var.vpc_id
tags = module.label.tags
}

resource "aws_security_group_rule" "udp_dns_egress_from_lambda" {
count = "${var.enabled == "true" ? 1 : 0}"
count = var.enabled == "true" ? 1 : 0
description = "Allow outbound UDP traffic from Lambda Elasticsearch cleanup to DNS"
type = "egress"
from_port = 53
to_port = 53
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.default.id}"
security_group_id = join("", aws_security_group.default.*.id)
}

resource "aws_security_group_rule" "tcp_dns_egress_from_lambda" {
count = "${var.enabled == "true" ? 1 : 0}"
count = var.enabled == "true" ? 1 : 0
description = "Allow outbound TCP traffic from Lambda Elasticsearch cleanup to DNS"
type = "egress"
from_port = 53
to_port = 53
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.default.id}"
security_group_id = join("", aws_security_group.default.*.id)
}

resource "aws_security_group_rule" "egress_from_lambda_to_es_cluster" {
count = "${var.enabled == "true" ? 1 : 0}"
count = var.enabled == "true" ? 1 : 0
description = "Allow outbound traffic from Lambda Elasticsearch cleanup SG to Elasticsearch SG"
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
source_security_group_id = "${var.es_security_group_id}"
security_group_id = "${aws_security_group.default.id}"
source_security_group_id = var.es_security_group_id
security_group_id = join("", aws_security_group.default.*.id)
}

resource "aws_security_group_rule" "ingress_to_es_cluster_from_lambda" {
count = "${var.enabled == "true" ? 1 : 0}"
count = var.enabled == "true" ? 1 : 0
description = "Allow inbound traffic to Elasticsearch domain from Lambda Elasticsearch cleanup SG"
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
source_security_group_id = "${aws_security_group.default.id}"
security_group_id = "${var.es_security_group_id}"
source_security_group_id = join("", aws_security_group.default.*.id)
security_group_id = var.es_security_group_id
}

resource "aws_iam_role" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${local.function_name}"
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
tags = "${module.label.tags}"
count = var.enabled == "true" ? 1 : 0
name = local.function_name
assume_role_policy = data.aws_iam_policy_document.assume_role.json
tags = module.label.tags
}

resource "aws_iam_role_policy" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${local.function_name}"
role = "${aws_iam_role.default.name}"
policy = "${data.aws_iam_policy_document.default.json}"
count = var.enabled == "true" ? 1 : 0
name = local.function_name
role = join("", aws_iam_role.default.*.id)
policy = data.aws_iam_policy_document.default.json
}

resource "aws_iam_role_policy_attachment" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
role = "${aws_iam_role.default.name}"
count = var.enabled == "true" ? 1 : 0
role = join("", aws_iam_role.default.*.name)
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}

resource "aws_cloudwatch_event_rule" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${local.function_name}"
description = "${local.function_name}"
schedule_expression = "${var.schedule}"
count = var.enabled == "true" ? 1 : 0
name = local.function_name
description = local.function_name
schedule_expression = var.schedule
}

resource "aws_lambda_permission" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
count = var.enabled == "true" ? 1 : 0
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.default.arn}"
function_name = join("", aws_lambda_function.default.*.arn)
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.default.arn}"
source_arn = join("", aws_cloudwatch_event_rule.default.*.arn)
}

resource "aws_cloudwatch_event_target" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
target_id = "${local.function_name}"
rule = "${aws_cloudwatch_event_rule.default.name}"
arn = "${aws_lambda_function.default.arn}"
count = var.enabled == "true" ? 1 : 0
target_id = local.function_name
rule = join("", aws_cloudwatch_event_rule.default.*.name)
arn = join("", aws_lambda_function.default.*.arn)
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "security_group_id" {
value = "${join(",", aws_security_group.default.*.id)}"
value = join(",", aws_security_group.default.*.id)
description = "Security Group ID of the Lambda "
}
32 changes: 16 additions & 16 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
variable "enabled" {
type = "string"
type = string
default = "true"
description = "This module will not create any resources unless enabled is set to \"true\""
}

variable "es_endpoint" {
type = "string"
type = string
description = "The Elasticsearch endpoint for the Lambda function to connect to"
}

variable "es_domain_arn" {
type = "string"
type = string
description = "The Elasticsearch domain ARN"
}

variable "es_security_group_id" {
type = "string"
type = string
description = "The Elasticsearch cluster security group ID"
}

variable "schedule" {
type = "string"
type = string
default = "cron(0 3 * * ? *)"
description = "CloudWatch Events rule schedule using cron or rate expression"
}

variable "subnet_ids" {
type = "list"
type = list
description = "Subnet ids"
}

variable "sns_arn" {
type = "string"
type = string
default = ""
description = "SNS ARN to publish alerts"
}

variable "index" {
type = "string"
type = string
default = "all"
description = "Index/indices to process. Use a comma-separated list. Specify `all` to match every index except for `.kibana` or `.kibana_1`"
}
Expand All @@ -48,35 +48,35 @@ variable "delete_after" {
}

variable "namespace" {
type = "string"
type = string
description = "Namespace, which could be your organization name, e.g. 'eg' or 'cp'"
}

variable "stage" {
type = "string"
type = string
description = "Stage, e.g. 'prod', 'staging', 'dev', or 'test'"
}

variable "name" {
type = "string"
type = string
default = "app"
description = "Solution name, e.g. 'app' or 'cluster'"
}

variable "delimiter" {
type = "string"
type = string
default = "-"
description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`"
}

variable "attributes" {
type = "list"
type = list
default = []
description = "Additional attributes (e.g. `1`)"
}

variable "tags" {
type = "map"
type = map
default = {}
description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`"
}
Expand All @@ -87,7 +87,7 @@ variable "index_format" {
}

variable "python_version" {
type = "string"
type = string
default = "2.7"
description = "The Python version to use"
}
Expand All @@ -98,6 +98,6 @@ variable "timeout" {
}

variable "vpc_id" {
type = "string"
type = string
description = "The VPC ID for the Lambda function"
}