Skip to content
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

Support for multiple DNS zones #55

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,116 @@ module "autoscale_dns" {
}
```

## Setup for multiple DNS zones

This example (CDKTF/Typescript) demonstrates how to use the asg-dns-handler for simultaneous updating of both private and public zones.

```typescript
import { AsgDnsHandler, AsgDnsHandlerConfig } from ".gen/modules/asg-dns-handler"
import {
AutoscalingGroup,
AutoscalingGroupConfig,
AutoscalingGroupInitialLifecycleHook,
AutoscalingGroupTag
} from ".gen/providers/aws/autoscaling-group"
import { LaunchTemplate } from ".gen/providers/aws/launch-template"

interface InstanceConfigDNSHandler {
ZoneId: string
ZoneName: string
Prefix: string
}

interface InstanceConfig {
vpcId: string
vpcName: string
subnetId: string
name: string
privateDNS: InstanceConfigDNSHandler
publicDNS: InstanceConfigDNSHandler
}

let config: InstanceConfig
let launchTemplate: LaunchTemplate

const hostnamePatternPrivate = "asg:hostname_pattern:private"
const hostnamePatternPublic = "asg:hostname_pattern:public"

const asgDNSHandlerPrivate = new AsgDnsHandler(this, "asg_dns_handler_private", <AsgDnsHandlerConfig>{
autoscaleHandlerUniqueIdentifier: `asg_handler_private`,
autoscaleRoute53ZoneArn: config.privateDNS.ZoneId,
vpcName: config.vpcName,
hostnameTagName: hostnamePatternPrivate
})

const asgDNSHandlerPublic = new AsgDnsHandler(this, "asg_dns_handler_public", <AsgDnsHandlerConfig>{
autoscaleHandlerUniqueIdentifier: `asg_handler_public`,
autoscaleRoute53ZoneArn: config.publicDNS.ZoneId,
vpcName: config.vpcName,
usePublicIp: true,
hostnameTagName: hostnamePatternPublic
})

new AutoscalingGroup(this, "aws_autoscaling_group", <AutoscalingGroupConfig>{
name: "asg",
vpcZoneIdentifier: [config.subnetId],
desiredCapacity: 1,
maxSize: 1,
minSize: 0,
launchTemplate: {
id: launchTemplate.id,
version: "$Default"
},
initialLifecycleHook: [
<AutoscalingGroupInitialLifecycleHook>{
name: "lifecycle-launching-private",
defaultResult: "ABANDON",
heartbeatTimeout: 60,
lifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
notificationTargetArn: asgDNSHandlerPrivate.autoscaleHandlingSnsTopicArnOutput,
roleArn: asgDNSHandlerPrivate.agentLifecycleIamRoleArnOutput
},
<AutoscalingGroupInitialLifecycleHook>{
name: "lifecycle-terminating-private",
defaultResult: "ABANDON",
heartbeatTimeout: 60,
lifecycleTransition: "autoscaling:EC2_INSTANCE_TERMINATING",
notificationTargetArn: asgDNSHandlerPrivate.autoscaleHandlingSnsTopicArnOutput,
roleArn: asgDNSHandlerPrivate.agentLifecycleIamRoleArnOutput
},
<AutoscalingGroupInitialLifecycleHook>{
name: "lifecycle-launching-public",
defaultResult: "ABANDON",
heartbeatTimeout: 60,
lifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
notificationTargetArn: asgDNSHandlerPublic.autoscaleHandlingSnsTopicArnOutput,
roleArn: asgDNSHandlerPublic.agentLifecycleIamRoleArnOutput
},
<AutoscalingGroupInitialLifecycleHook>{
name: "lifecycle-terminating-public",
defaultResult: "ABANDON",
heartbeatTimeout: 60,
lifecycleTransition: "autoscaling:EC2_INSTANCE_TERMINATING",
notificationTargetArn: asgDNSHandlerPublic.autoscaleHandlingSnsTopicArnOutput,
roleArn: asgDNSHandlerPublic.agentLifecycleIamRoleArnOutput
}
],
tag: [
<AutoscalingGroupTag>{
key: hostnamePatternPrivate,
value: `${config.privateDNS.Prefix}.${config.privateDNS.ZoneName}@${config.privateDNS.ZoneId}`,
propagateAtLaunch: true
},
<AutoscalingGroupTag>{
key: hostnamePatternPublic,
value: `${config.publicDNS.Prefix}.${config.publicDNS.ZoneName}@${config.publicDNS.ZoneId}`,
propagateAtLaunch: true
}
]
})
```


## Developers Guide / Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) to understand how to submit pull requests to us, and also see our [Code of Conduct](CODE_OF_CONDUCT.md).
Expand Down
2 changes: 1 addition & 1 deletion lambda/autoscale/autoscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ec2 = boto3.client('ec2')
route53 = boto3.client('route53')

HOSTNAME_TAG_NAME = "asg:hostname_pattern"
HOSTNAME_TAG_NAME = os.environ['hostname_tag_name']

LIFECYCLE_KEY = "LifecycleHookName"
ASG_KEY = "AutoScalingGroupName"
Expand Down
3 changes: 2 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ resource "aws_lambda_function" "autoscale_handling" {
description = "Handles DNS for autoscaling groups by receiving autoscaling notifications and setting/deleting records from route53"
environment {
variables = {
"use_public_ip" = var.use_public_ip
"use_public_ip" = var.use_public_ip,
"hostname_tag_name" = var.hostname_tag_name
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ variable "autoscale_route53zone_arn" {
description = "The ARN of route53 zone associated with autoscaling group"
}

variable "hostname_tag_name" {
description = "Name of tag with hostname pattern"
default = "asg:hostname_pattern"
type = string
}