All Projects → meltwater → terraform-aws-asg-dns-handler

meltwater / terraform-aws-asg-dns-handler

Licence: Apache-2.0 license
Terraform module for dynamically setting hostnames following a pattern on instances in AWS Auto Scaling Groups

Programming Languages

python
139335 projects - #7 most used programming language
HCL
1544 projects
go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to terraform-aws-asg-dns-handler

terraform-modules
Xenit Terraform modules
Stars: ✭ 23 (-61.67%)
Mutual labels:  terraform-module
terraform-aws-ses
Terraform module to provision Simple Email Service on AWS
Stars: ✭ 24 (-60%)
Mutual labels:  terraform-module
terraform-aws-iam-system-user
Terraform Module to Provision a Basic IAM System User Suitable for CI/CD Systems (E.g. TravisCI, CircleCI)
Stars: ✭ 71 (+18.33%)
Mutual labels:  terraform-module
terraform-aws-waf-webacl-supporting-resources
A module to create several resources needed by AWS WAF WebACL.
Stars: ✭ 25 (-58.33%)
Mutual labels:  terraform-module
terraform-aws-lb-s3-bucket
Terraform module to provision an S3 bucket with built in IAM policy to allow AWS Load Balancers to ship access logs
Stars: ✭ 29 (-51.67%)
Mutual labels:  terraform-module
terraform-azurerm-kubernetes
Terraform module to deploy a Kubernetes cluster on Azure, using AKS.
Stars: ✭ 16 (-73.33%)
Mutual labels:  terraform-module
terraform-kubernetes-alb-ingress-controller
Terraform module to ease deployment of the AWS ALB Ingress Controller
Stars: ✭ 55 (-8.33%)
Mutual labels:  terraform-module
symreader-converter
Converts between Windows PDB and Portable PDB formats.
Stars: ✭ 50 (-16.67%)
Mutual labels:  managed
terraform-aws-ecs-fargate-task-definition
Terraform module to create AWS ECS Fargate Task Definition
Stars: ✭ 20 (-66.67%)
Mutual labels:  terraform-module
terraform-aws-transit-gateway
Terraform module which creates Transit Gateway resources on AWS 🇺🇦
Stars: ✭ 98 (+63.33%)
Mutual labels:  terraform-module
terraform-aws-cognito-user-pool
Terraform module to create Amazon Cognito User Pools, configure its attributes and resources such as app clients, domain, resource servers. Amazon Cognito User Pools provide a secure user directory that scales to hundreds of millions of users.
Stars: ✭ 65 (+8.33%)
Mutual labels:  terraform-module
terraform-aws-msk-cluster
Terraform module which creates Msk Kafka Cluster on AWS
Stars: ✭ 26 (-56.67%)
Mutual labels:  terraform-module
terraform-aws-concourse
Terraform Module for a distributed concourse cluster on AWS
Stars: ✭ 12 (-80%)
Mutual labels:  terraform-module
terraform-aws-chatbot-slack-configuration
Module to enable Chatbot Slack channel configuration in Terraform
Stars: ✭ 31 (-48.33%)
Mutual labels:  terraform-module
terraform-digitalocean-kubernetes
A terraform module for managing and creating a Kubernetes cluster on digital ocean
Stars: ✭ 11 (-81.67%)
Mutual labels:  terraform-module
terraform-openstack-rke2
Deploy Kubernetes on OpenStack with RKE2
Stars: ✭ 38 (-36.67%)
Mutual labels:  terraform-module
terraform-aws-kms
This terraform module creates a KMS Customer Master Key (CMK) and its alias.
Stars: ✭ 14 (-76.67%)
Mutual labels:  terraform-module
terraform-aws-datadog-forwarders
Terraform module which creates resources on AWS to forward logs/metrics to Datadog 🇺🇦
Stars: ✭ 30 (-50%)
Mutual labels:  terraform-module
terraform-aws-sonarqube
SonarQube Terraform Module for AWS
Stars: ✭ 28 (-53.33%)
Mutual labels:  terraform-module
terraform-aws-enforce-mfa
A terraform module to enforce MFA for AWS groups and users
Stars: ✭ 24 (-60%)
Mutual labels:  terraform-module

ASG DNS handler | Build Status

This Terraform module sets up everything necessary for dynamically setting hostnames following a certain pattern on instances spawned by AWS Auto Scaling Groups (ASGs).

Learn more about our motivation to build this module in our blog post Dynamic Route53 records for AWS Auto Scaling Groups with Terraform.

Maintainers

This repository and the module it houses are maintained Foundation Missions A-Team. Should you encounter issues or require changes to code maintained in this repository, please reachout through an issue that is part of this project.

Requirements

How do I use it?

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

asg-test-#instanceid.asg-handler-vpc.testing@Z3QP9GZSRL8IVA

#instanceid is converted by a Lambda function within this module to the actual AWS instance_id that corresponds to the launched instance. The @ symbol is used to split the FQDN from the Route 53 zone_id.

This could be interpolated in Terraform like this:

tag {
  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 to it:

module "clever_name_autoscale_dns" {
  source  = meltwater/asg-dns-handler/aws"
  version = "~> 2.0"
  
  # use_public_ip = true
  autoscale_handler_unique_identifier = "clever_name"
  autoscale_route53zone_arn           = "ABCDEFGHIJ123"
  vpc_name                            = "my_vpc"
}

How does it work?

The module sets up these things:

  1. A SNS topic
  2. A Lambda function
  3. A topic subscription sending SNS events to the Lambda function

The Lambda function then does the following:

  • Fetch the asg:hostname_pattern tag value from the ASG, and parse out the hostname and Route53 zone ID from it.
  • If it's an instance being created
    • Fetch internal IP from EC2 API
    • Create a Route53 record pointing the hostname to the IP
    • Set the Name tag of the instance to the initial part of the generated hostname
  • If it's an instance being deleted
    • Fetch the internal IP from the existing record from the Route53 API
    • Delete the record

Setup

Add initial_lifecycle_hook definitions to your aws_autoscaling_group resource , like so:

resource "aws_autoscaling_group" "my_asg" {
  name = "myASG"

  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"
  health_check_grace_period = 300
  force_delete              = false

  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
  }

  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
  }

  tag {
    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  = "meltwater/asg-dns-handler/aws"
  version = "2.1.7"

  autoscale_handler_unique_identifier = "my_asg_handler"
  autoscale_route53zone_arn           = var.internal_zone_id
  vpc_name                            = var.vpc_name
}

Developers Guide / Contributing

Please read CONTRIBUTING.md to understand how to submit pull requests to us, and also see our Code of Conduct.

Difference between Lifecycle action

Lifecycle_hook can have CONTINUE or ABANDON as default_result. By setting default_result to ABANDON will terminate the instance if the lambda function fails to update the DNS record as required. Complete_lifecycle_action in lambda function returns LifecycleActionResult as CONTINUE on success to Lifecycle_hook. But if lambda function fails, Lifecycle_hook doesn't get any response from Complete_lifecycle_action which results in timeout and terminates the instance.

At the conclusion of a lifecycle hook, the result is either ABANDON or CONTINUE. If the instance is launching, CONTINUE indicates that your actions were successful, and that the instance can be put into service. Otherwise, ABANDON indicates that your custom actions were unsuccessful, and that the instance can be terminated.

If the instance is terminating, both ABANDON and CONTINUE allow the instance to terminate. However, ABANDON stops any remaining actions, such as other lifecycle hooks, while CONTINUE allows any other lifecycle hooks to complete.

License and Copyright

This project was built at Meltwater. It is licensed under the Apache License 2.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].