Skip to content

Conversation

@joshmyers
Copy link
Contributor

@joshmyers joshmyers commented Dec 14, 2018

what

This PR adds the main gubbins of the ES cleanup Lambda function and associated Terraform code

@joshmyers joshmyers force-pushed the add_es_cleanup_module branch 2 times, most recently from 3bd9ac6 to 5512c28 Compare December 14, 2018 16:59
@osterman
Copy link
Member

I think we should convey lambda somewhere in the repo name

README.yaml Outdated
# Short description of this project
description: |-
Terraform module to provision a scheduled Lambda function which will
delete old Elasticsearch indexes using SigV4Auth authentication. The
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
delete old Elasticsearch indexes using SigV4Auth authentication. The
delete old Elasticsearch indexes using [SigV4Auth](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html) authentication. The
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if that is the right link

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the overarching sigV4 (needed when talking to ES clusters) documentation link - from there you can dig more into those docs.

README.yaml Outdated

# Contributors to this project
contributors:
- name: "Erik Osterman"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove non-contributors

example/main.tf Outdated
enabled = "true"
namespace = "example"
stage = "dev"
schedule = "rate(5 minutes)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we limit this to specific indexes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add an index whitelist should we wish. Currently the function accepts a specific index, or all.

@@ -0,0 +1,210 @@
#!/usr/bin/python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add link of attribution to original lambda

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, mention copyrights and license

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshmyers joshmyers force-pushed the add_es_cleanup_module branch from 5512c28 to bfd7c0f Compare December 14, 2018 17:33
Copy link
Member

@osterman osterman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

variables.tf Outdated
}

variable "python_version" {
default = "2.7"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing description

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment addressed.

@osterman osterman requested a review from aknysh December 14, 2018 17:36
example/main.tf Outdated

module "elasticsearch" {
source = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=tags/0.1.5"
namespace = "example"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to hardcode these in examples b/c it precludes us testing the module concurrently. Additionally, sometimes our community will deploy this version without changing the values, which means we cannot reuse them.


class ES_Cleanup(object):

name = "lambda_es_cleanup"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to make sure this doesn't need to be unique to the AWS account

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this is used to prefix outputted messaged when sending to SNS

main.tf Outdated

resource "aws_security_group" "default" {
count = "${local.enabled == true ? 1 : 0}"
name = "${module.label.id}-elasticsearch-cleanup"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not consistent about this, but technically should be using a new label with attributes = ["elasticsearch", "cleanup"]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, we ignore the user's delimiter selection

main.tf Outdated

resource "aws_iam_role_policy" "default" {
count = "${local.enabled == true ? 1 : 0}"
name = "${module.label.id}-elasticsearch-cleanup"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variables.tf Outdated
}

variable "vpc_id" {
description = "VPC Id for the Lambda function - should be the same VPC as ES domain"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "VPC Id for the Lambda function - should be the same VPC as ES domain"
description = "VPC ID for the Lambda function. Should be the same VPC as the ES domain."
variables.tf Outdated
}

variable "index" {
description = "Index/indices to process comma separated, with all every index will be processed except '.kibana'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Index/indices to process comma separated, with all every index will be processed except '.kibana'"
description = "Index/indices to process. Use a comma-separated list. Specify`all` to match every index except for `.kibana`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces ok?

variables.tf Outdated
}

variable "delete_after" {
description = "Numbers of days to preserve"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Numbers of days to preserve"
description = "Number of days to preserve"
variables.tf Outdated

variable "enabled" {
type = "string"
description = "Enable or disable the ElasticSearch Cleanup function"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Enable or disable the ElasticSearch Cleanup function"
description = "Enable or disable the Elasticsearch Cleanup function"

Seems like the canonical spelling is Elasticsearch per AWS documentation

variables.tf Outdated
variable "tags" {
type = "map"
default = {}
description = "Additional tags (e.g. `map('BusinessUnit`,`XYZ`)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Additional tags (e.g. `map('BusinessUnit`,`XYZ`)"
description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, the origin of this map(..) business is from OLD terraforms which did not support { ... } notation. At some point we need to bite the bullet.

@joshmyers joshmyers force-pushed the add_es_cleanup_module branch 3 times, most recently from 4ef843b to fbdd26d Compare December 14, 2018 18:27
.gitignore Outdated
@@ -1,7 +1,6 @@
# Compiled files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use our standard .gitignore for Terraform repos:

# Local .terraform directories **/.terraform/* # .tfstate files *.tfstate *.tfstate.* # .tfvars files *.tfvars **/.idea **/*.iml **/.build-harness **/build-harness 
README.yaml Outdated
description: "Terraform module for dynamic subnets provisioning."
url: "https://github.com/cloudposse/terraform-aws-dynamic-subnets"
- name: "terraform-aws-elasticsearch"
description: "Terraform module for AWS ElasticSearch provisioning."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use canonical Elasticsearch

README.yaml Outdated
# How to use this project
usage: |-
```hcl
module "elasticsearch-cleanup" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module "elasticsearch-cleanup" {
module "elasticsearch_cleanup" {

use snake case to name resources

enabled = "true"
namespace = "example"
stage = "dev"
schedule = "rate(5 minutes)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just a free text for the schedule, not regex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example/main.tf Outdated
}

module "elasticsearch" {
source = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=tags/0.1.5"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=tags/0.1.5"
source = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=master"

use master for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I was going for a known working release

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's kind of a chicken and the egg - also easily forgotten to update. We tend to pin our README.md examples to master so users who copy and paste get something that probably works.

example/main.tf Outdated
vpc_id = "${module.vpc.vpc_id}"
subnet_ids = ["${module.subnets.public_subnet_ids}"]
zone_awareness_enabled = "true"
elasticsearch_version = "6.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elasticsearch_version = "6.2"
elasticsearch_version = "6.3"

use the latest supported version

resource "aws_cloudwatch_event_target" "default" {
count = "${local.enabled == true ? 1 : 0}"
target_id = "${local.function_name}"
rule = "${aws_cloudwatch_event_rule.default.name}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rule = "${aws_cloudwatch_event_rule.default.name}"
rule = "${join("", aws_cloudwatch_event_rule.default.*.name})"
variables.tf Outdated
description = "Subnet ids"
}

variable "sns_alert" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
variable "sns_alert" {
variable "sns_arn" {
variables.tf Outdated
variable "sns_alert" {
type = "string"
default = ""
description = "SNS ARN to pusblish any alert"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "SNS ARN to pusblish any alert"
description = "SNS ARN to publish alerts"
variables.tf Outdated
variable "index" {
type = "string"
default = "all"
description = "Index/indices to process. Use a comma-separated list. Specify`all` to match every index except for `.kibana`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Index/indices to process. Use a comma-separated list. Specify`all` to match every index except for `.kibana`"
description = "Index/indices to process. Use a comma-separated list. Specify `all` to match every index except for `.kibana`"
variables.tf Outdated
description = "Stage, e.g. 'prod', 'staging', 'dev', or 'test'"
}

variable "environment" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this var not needed

variables.tf Outdated

variable "index_format" {
default = "%Y.%m.%d"
description = "Combined with 'index' varible is used to evaluate the index age"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Combined with 'index' varible is used to evaluate the index age"
description = "Combined with 'index' variable and is used to evaluate the index age"

data "aws_iam_policy_document" "default" {
statement {
actions = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to give these permissions to the module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah these are needed to the Lambda function can a) write log files to CloudWatch logs b) post a v4 signed payload to ES

Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see inline comments

@joshmyers joshmyers force-pushed the add_es_cleanup_module branch 2 times, most recently from 134754c to 588494c Compare December 17, 2018 18:05
@joshmyers
Copy link
Contributor Author

@aknysh Ready for another review

Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

This creates all the base resources necessary to test the elasticsearch-cleanup module. Unfortunately this requires a two stage apply as the `aws_route53_zone` resource fails to compute ID when passed into other modules. See: cloudposse/terraform-aws-elasticsearch#13
Pull the deployment artifact from S3 using the new terraform-external-module-artifact module. This approach has the benefit of not checking in zip files into git, but the downside of more preamble when wanting to test this module as per the example docs. You need to know that the function should already be in S3 at a pre determined path. We need to think of proper deployment and promotion of these artifacts through different S3 buckets e.g. artifacts.testing.cloudposse.org -> artifacts.prod.cloudposse.org
@joshmyers joshmyers force-pushed the add_es_cleanup_module branch from 588494c to a4585fe Compare December 17, 2018 19:02
@aknysh aknysh requested a review from osterman December 17, 2018 19:08
@joshmyers joshmyers dismissed osterman’s stale review December 17, 2018 19:32

@osterman already commented and handed over to @aknysh

@joshmyers joshmyers merged commit 30523cd into master Dec 17, 2018
@joshmyers joshmyers deleted the add_es_cleanup_module branch December 17, 2018 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants