This code can be used to manage Route53 records of type CNAME or A for an Autoscaling Group with Lambda via a tag on the ASG. The main function listens for TERMINATE and LAUNCH events, and DELETEs and UPSERTs records to Route53.
Create an Autoscaling Group Tag called Route53
with values in any of the formats below.
Format | Example | Notes |
---|---|---|
HostedZoneId:record-name | Z0987654321123:www.example.com |
Assumes CNAME type and TTL of 1 |
HostedZoneId:type:record-name | Z0987654321123:CNAME:www.example.com |
Assumes TTL of 1 |
HostedZoneId:type:record-name:ttl | Z0987654321123:CNAME:www.example.com:30 |
Format | Example | Notes |
---|---|---|
ZoneId:type:record-name:ttl | Z0987654321123:CNAME:www.#:30 |
# will be replaced by the zone name, ex: www.# -- will be replaced with --> www.example.com |
Format | Example | Notes |
---|---|---|
ZoneId1,ZoneId2,...:prefix-name | Z0987654321123,Z1234567890123:www.:30 |
All zones use the same prefix-name and prefix-name is added to zone name |
Format | Example | Notes |
---|---|---|
[<valid string 1>, <valid string 2>, ...] | ["Z0987654321123:CNAME:www.example.com:30","Z1234567890123:A:www.#"] |
JSON array of strings which are in a valid format above |
NOTE: A tag value of <empty string>
or none
is ignored.
Create ZIP file with zip ../autoscaling-route53.js index.js async
AWS makes this relatively easy for you, but you still have to jump some hoops. Let's use the Console for this, as it is easiest.
For a given Autoscaling Group you want to 'sync' with Route53, do the following:
- create an IAM role with policies AmazonEC2ReadOnlyAccess, CloudWatchLogsFullAccess and AmazonRoute53FullAccess (our name is AutoscalingRoute53Role)
- create a Lambda function with ZIP and IAM Role from before (our name is autoscalingRoute53)
- create an SNS topic (our topic name asg-notifications-staging-elasticsearch-30mhz-com)
- add a subscription to SNS topic, pointing to the Lambda function
- on the notifications tab in the details pane of the Autoscaling Group, you click 'Create notification'
- set 'send notification to' to the SNS topic
- and only 'launch' and 'terminate' are used by this lambda function. Other messages are ignored.
- last, but not least, add a tag to the Autoscaling Group with key 'Route53' and value 'HostedZoneId:record-name'
And now, scale :)
If you want to script this setup with Cloudformation, here is a snippet that covers steps 3 - 7 above. This snippet would be put into the 'Resources' of a Cloudformation script.
"Resources": {
....
"LambdaDNS": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName" : "asg-to-dns-v1",
"Code": {
"S3Bucket": "bionano-devops-build-artifacts",
"S3Key": "lambdas/lambdaDNS/autoscaling-route53.js.zip"
},
"Description" : "Lambda to update dns based on asg events",
"Role": { "Fn::Join" : [ "/", [ "arn:aws:iam::123456789012:role", "lambda-asg-sns-dns"] ] },
"Timeout": 60,
"Handler": "index.handler",
"Runtime": "nodejs4.3",
"MemorySize": 128
}
},
"autoscalingNotificationTopic" : {
"Type" : "AWS::SNS::Topic",
"DependsOn" : [ "LambdaDNS" ],
"Condition": "IsUSWest2",
"Properties" : {
"DisplayName" : "wD Scaling",
"Subscription" : [
{ "Endpoint" : {"Fn::GetAtt" : [ "LambdaDNS", "Arn" ]}, "Protocol" : "lambda" }
],
"TopicName" : "dev-autoscaling-NotificationTopic"
}
},
"LambdaInvokePermission": {
"Type": "AWS::Lambda::Permission",
"Condition": "IsUSWest2",
"DependsOn" : [ "LambdaDNS", "autoscalingNotificationTopic" ],
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "sns.amazonaws.com",
"SourceArn": { "Ref": "autoscalingNotificationTopic" },
"FunctionName": { "Fn::GetAtt": [ "LambdaDNS", "Arn" ] }
}
}
.....