Skip to content

updating module to Terraform 0.12 #21

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

Merged
merged 5 commits into from
Nov 14, 2019
Merged
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
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ steps:
commands:
- apk add go build-base
- cd terratest
- terrapin -force -tf-version 0.11.8 -directory /usr/local/bin
- terrapin -force -tf-version 0.12.14 -directory /usr/local/bin
- export GOPATH=$(pwd)
- export PATH=$PATH:$GOPATH/bin
- go get -u github.com/golang/dep/cmd/dep
Expand All @@ -32,6 +32,6 @@ trigger:
- pull_request
---
kind: signature
hmac: 5cf0d57469a588f541d45af514e1ee57ef396fa268530c0fc538655a896677fc
hmac: 06fe1ecacae32cf2da4b91d1f7d57e1dcb051257a4b45137f63732c70dc00f46

...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pkg
terraform.tfstate
terraform.tfstate.backup
.terraform.tfstate.lock.info
lambda/dist/
68 changes: 35 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
## Purpose
This module sets up everything necessary for dynamically setting hostnames following a certain pattern on instances spawned by Auto Scaling Groups

# Requirements
- [Terraform](https://www.terraform.io/downloads.html) 0.12+
- [Terraform AWS provider](https://github.com/terraform-providers/terraform-provider-aws) 2.0+

## Usage
Create an ASG and set the `asg:hostname_pattern` tag for example like this:

Expand All @@ -16,19 +20,20 @@ Could be interpolated in Terraform like this:

```hcl
tag {
key = "asg:hostname_pattern"
value = "${var.hostname_prefix}-#instanceid.${var.vpc_name}.testing@${var.internal_zone_id}"
key = "asg:hostname_pattern"
value = "${var.hostname_prefix}-#instanceid.${var.vpc_name}.testing@${var.internal_zone_id}"
propagate_at_launch = true
}
```

Once you have your ASG set up, you can just invoke this module and point it to it:
```hcl
module "clever_name_autoscale_dns" {
source = "../../"
source = "meltwater/asg-dns-handler/aws"
version = "x.y.z"

autoscale_update_name = "clever_name"
autoscale_group_names = "${aws_autoscaling_group.my_asg.name}"
autoscale_update_name = "clever_name"
autoscale_group_names = "${aws_autoscaling_group.my_asg.name}"
autoscale_route53zone_arn = "${var.zone_to_manage_records_in}"
}
```
Expand Down Expand Up @@ -59,58 +64,55 @@ Add `initial_lifecycle_hook` definitions to your `aws_autoscaling_group resource
resource "aws_autoscaling_group" "my_asg" {
name = "myASG"

vpc_zone_identifier = [
"${var.aws_subnets}"
]
vpc_zone_identifier = var.aws_subnets

min_size = "${var.asg_min_count}"
max_size = "${var.asg_max_count}"
desired_capacity = "${var.asg_desired_count}"
health_check_type = "EC2"
min_size = var.asg_min_count
max_size = var.asg_max_count
desired_capacity = var.asg_desired_count
health_check_type = "EC2"
health_check_grace_period = 300
force_delete = false
force_delete = false

launch_configuration = "${aws_launch_configuration.my_launch_config.name}"
launch_configuration = aws_launch_configuration.my_launch_config.name

lifecycle {
create_before_destroy = true
}

initial_lifecycle_hook {
name = "lifecycle-launching"
default_result = "ABANDON"
heartbeat_timeout = 60
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
notification_target_arn = "${module.autoscale_dns.autoscale_handling_sns_topic_arn}"
role_arn = "${module.autoscale_dns.agent_lifecycle_iam_role_arn}"
name = "lifecycle-launching"
default_result = "ABANDON"
heartbeat_timeout = 60
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
notification_target_arn = module.autoscale_dns.autoscale_handling_sns_topic_arn
role_arn = module.autoscale_dns.agent_lifecycle_iam_role_arn
}

initial_lifecycle_hook {
name = "lifecycle-terminating"
default_result = "ABANDON"
heartbeat_timeout = 60
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
notification_target_arn = "${module.autoscale_dns.autoscale_handling_sns_topic_arn}"
role_arn = "${module.autoscale_dns.agent_lifecycle_iam_role_arn}"
name = "lifecycle-terminating"
default_result = "ABANDON"
heartbeat_timeout = 60
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
notification_target_arn = module.autoscale_dns.autoscale_handling_sns_topic_arn
role_arn = module.autoscale_dns.agent_lifecycle_iam_role_arn
}

tag {
key = "asg:hostname_pattern"
value = "${var.hostname_prefix}-#instanceid.${var.vpc_name}.testing@${var.internal_zone_id}"
key = "asg:hostname_pattern"
value = "${var.hostname_prefix}-#instanceid.${var.vpc_name}.testing@${var.internal_zone_id}"
propagate_at_launch = true
}
}

module "autoscale_dns" {
source = "github.com/meltwater/terraform-aws-asg-dns-handler"
source = "/Users/jimsheldon/git/meltwater/terraform-aws-asg-dns-handler"

autoscale_update_name = "my_asg_handler"

autoscale_route53zone_arn = "${var.internal_zone_id}"
autoscale_route53zone_arn = var.internal_zone_id

vpc_name = "${var.vpc_name}"
vpc_name = var.vpc_name
}

```

## Difference between Lifecycle action
Expand Down
36 changes: 18 additions & 18 deletions example/asg-dns-agent/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module "autoscale_dns" {
source = "../../"
autoscale_handler_unique_identifier = "asg-handler"
autoscale_route53zone_arn = "${aws_route53_zone.test.id}"
autoscale_route53zone_arn = aws_route53_zone.test.id
vpc_name = "asg-handler-vpc"
}

Expand All @@ -12,9 +12,9 @@ resource "aws_launch_configuration" "test" {
create_before_destroy = true
}

image_id = "${var.ami_id}"
instance_type = "${var.instance_type}"
security_groups = ["${aws_security_group.test.id}"]
image_id = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.test.id]
associate_public_ip_address = false
}

Expand All @@ -28,28 +28,29 @@ resource "aws_autoscaling_group" "test" {
default_result = "ABANDON"
heartbeat_timeout = 60
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
notification_target_arn = "${module.autoscale_dns.autoscale_handling_sns_topic_arn}"
role_arn = "${module.autoscale_dns.agent_lifecycle_iam_role_arn}"
notification_target_arn = module.autoscale_dns.autoscale_handling_sns_topic_arn
role_arn = module.autoscale_dns.agent_lifecycle_iam_role_arn
}

initial_lifecycle_hook {
name = "${aws_launch_configuration.test.id}-lifecycle-terminating"
default_result = "ABANDON"
heartbeat_timeout = 60
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
notification_target_arn = "${module.autoscale_dns.autoscale_handling_sns_topic_arn}"
role_arn = "${module.autoscale_dns.agent_lifecycle_iam_role_arn}"
notification_target_arn = module.autoscale_dns.autoscale_handling_sns_topic_arn
role_arn = module.autoscale_dns.agent_lifecycle_iam_role_arn
}

name = "${aws_launch_configuration.test.id}"
vpc_zone_identifier = ["${module.vpc.private_subnets}"]
name = aws_launch_configuration.test.id

min_size = "${var.min_size}"
max_size = "${var.max_size}"
vpc_zone_identifier = module.vpc.private_subnets

min_size = var.min_size
max_size = var.max_size
health_check_type = "EC2"
health_check_grace_period = 300
force_delete = false
launch_configuration = "${aws_launch_configuration.test.name}"
launch_configuration = aws_launch_configuration.test.name
termination_policies = ["OldestInstance"]

tag {
Expand All @@ -66,10 +67,10 @@ resource "aws_autoscaling_group" "test" {
}

resource "aws_security_group" "test" {
vpc_id = "${module.vpc.vpc_id}"
vpc_id = module.vpc.vpc_id
name = "asg-handler-vpc-test-agent"

tags {
tags = {
Name = "asg-handler"
}

Expand All @@ -79,9 +80,7 @@ resource "aws_security_group" "test" {
to_port = 0
protocol = "-1"

cidr_blocks = [
"${module.vpc.private_subnets_cidr_blocks}",
]
cidr_blocks = module.vpc.private_subnets_cidr_blocks
}

egress {
Expand All @@ -94,3 +93,4 @@ resource "aws_security_group" "test" {
]
}
}

7 changes: 4 additions & 3 deletions example/asg-dns-agent/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
output "asg_name" {
description = "The name of the autoscaling group"
value = "${aws_launch_configuration.test.id}"
value = aws_launch_configuration.test.id
}

output "vpc_internal_dns_id" {
description = "ID of the internal dns hosted zone"
value = "${aws_route53_zone.test.id}"
value = aws_route53_zone.test.id
}

output "vpc_internal_dns_name" {
description = "The name of the dns hosted zone"
value = "${aws_route53_zone.test.name}"
value = aws_route53_zone.test.name
}

1 change: 1 addition & 0 deletions example/asg-dns-agent/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ variable "ami_id" {
description = "AMIs by region"
default = "ami-f96c5280"
}

4 changes: 4 additions & 0 deletions example/asg-dns-agent/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
12 changes: 7 additions & 5 deletions example/asg-dns-agent/vpc.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
provider "aws" {
region = "${var.aws_region}"
version = "~> 2.0"
region = var.aws_region
}

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 1.0"
source = "terraform-aws-modules/vpc/aws"
version = "~> 2.0"

name = "asg-handler-vpc"
cidr = "10.0.0.0/16"
Expand All @@ -21,7 +22,8 @@ resource "aws_route53_zone" "test" {
name = "asg-handler-vpc.testing"
force_destroy = true

vpc {
vpc_id = "${module.vpc.vpc_id}"
vpc {
vpc_id = module.vpc.vpc_id
}
}

49 changes: 23 additions & 26 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resource "aws_sns_topic" "autoscale_handling" {

resource "aws_iam_role_policy" "autoscale_handling" {
name = "${var.vpc_name}-${var.autoscale_handler_unique_identifier}"
role = "${aws_iam_role.autoscale_handling.name}"
role = aws_iam_role.autoscale_handling.name

policy = <<EOF
{
Expand Down Expand Up @@ -42,6 +42,7 @@ resource "aws_iam_role_policy" "autoscale_handling" {
]
}
EOF

}

resource "aws_iam_role" "autoscale_handling" {
Expand All @@ -62,15 +63,16 @@ resource "aws_iam_role" "autoscale_handling" {
]
}
EOF

}

resource "aws_iam_role" "lifecycle" {
name = "${var.vpc_name}-${var.autoscale_handler_unique_identifier}-lifecycle"
assume_role_policy = "${data.aws_iam_policy_document.lifecycle.json}"
assume_role_policy = data.aws_iam_policy_document.lifecycle.json
}

data "aws_iam_policy_document" "lifecycle" {
"statement" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]

Expand All @@ -83,56 +85,51 @@ data "aws_iam_policy_document" "lifecycle" {

resource "aws_iam_role_policy" "lifecycle_policy" {
name = "${var.vpc_name}-${var.autoscale_handler_unique_identifier}-lifecycle"
role = "${aws_iam_role.lifecycle.id}"
policy = "${data.aws_iam_policy_document.lifecycle_policy.json}"
role = aws_iam_role.lifecycle.id
policy = data.aws_iam_policy_document.lifecycle_policy.json
}

data "aws_iam_policy_document" "lifecycle_policy" {
"statement" {
statement {
effect = "Allow"
actions = ["sns:Publish", "autoscaling:CompleteLifecycleAction",]
resources = ["${aws_sns_topic.autoscale_handling.arn}"]
actions = ["sns:Publish", "autoscaling:CompleteLifecycleAction"]
resources = [aws_sns_topic.autoscale_handling.arn]
}
}

data "archive_file" "autoscale" {
type = "zip"
source_file = ".${replace(path.module, path.root, "")}/lambda/autoscale/autoscale.py"
output_path = ".${replace(path.module, path.root, "")}/lambda/dist/autoscale.zip"
source_file = "${path.module}/lambda/autoscale/autoscale.py"
output_path = "${path.module}/lambda/dist/autoscale.zip"
}

resource "aws_lambda_function" "autoscale_handling" {
depends_on = [
"aws_sns_topic.autoscale_handling",
]
depends_on = [aws_sns_topic.autoscale_handling]

filename = "${data.archive_file.autoscale.output_path}"
filename = data.archive_file.autoscale.output_path
function_name = "${var.vpc_name}-${var.autoscale_handler_unique_identifier}"
role = "${aws_iam_role.autoscale_handling.arn}"
role = aws_iam_role.autoscale_handling.arn
handler = "autoscale.lambda_handler"
runtime = "python2.7"
source_code_hash = "${base64sha256(file("${data.archive_file.autoscale.output_path}"))}"
source_code_hash = filebase64sha256(data.archive_file.autoscale.output_path)
description = "Handles DNS for autoscaling groups by receiving autoscaling notifications and setting/deleting records from route53"
}

resource "aws_lambda_permission" "autoscale_handling" {
depends_on = [
"aws_lambda_function.autoscale_handling",
]
depends_on = [aws_lambda_function.autoscale_handling]

statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.autoscale_handling.arn}"
function_name = aws_lambda_function.autoscale_handling.arn
principal = "sns.amazonaws.com"
source_arn = "${aws_sns_topic.autoscale_handling.arn}"
source_arn = aws_sns_topic.autoscale_handling.arn
}

resource "aws_sns_topic_subscription" "autoscale_handling" {
depends_on = [
"aws_lambda_permission.autoscale_handling",
]
depends_on = [aws_lambda_permission.autoscale_handling]

topic_arn = "${aws_sns_topic.autoscale_handling.arn}"
topic_arn = aws_sns_topic.autoscale_handling.arn
protocol = "lambda"
endpoint = "${aws_lambda_function.autoscale_handling.arn}"
endpoint = aws_lambda_function.autoscale_handling.arn
}

Loading