You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
-
#Building a Dynamic DNS for Route 53 using CloudWatch Events and Lambda
1
+
#Building a Dynamic DNS for Route 53 using CloudWatch Events and Lambda
2
2
3
-
##Introduction
3
+
##Introduction
4
4
5
5
Dynamic registration of resource records is useful when you have instances that are not behind a load balancer and that you would like to address by a host name and domain suffix of your choosing rather than the default \<region\>.compute.internal or ec2.internal assigned by VPC DNS.
6
6
7
7
In this project we explore how you can use [CloudWatch Events](https://aws.amazon.com/cloudwatch) and Lambda to create a Dynamic DNS for Route 53. Besides creating A records, this project allows you to create alias, i.e. CNAME records, for when you want to address a server by a "friendly" or alternate name. Although this is antithetical to treating instances as disposable resources, there are still a lot of shops that find this useful.
8
8
9
-
##Using CloudWatch and Lambda to respond to infrastructure changes in real-time
9
+
##Using CloudWatch and Lambda to respond to infrastructure changes in real-time
10
10
11
11
With the advent of CloudWatch Events in January 2016, you can now get near real-time information when an AWS resource changes its state, including when instances are launched or terminated. When you combine this with the power of [Amazon Route 53](https://aws.amazon.com/route53) and [AWS Lambda](https://aws.amazon.com/lambda), you can create a system that closely mimics the behavior of Dynamic DNS.
12
12
13
13
For example, when a newly-launched instance changes its state from pending to running, an event can be sent to a Lambda function that creates a resource record in the appropriate Route 53 hosted zone. Similarly, when instances are stopped or terminated, Lambda can automatically remove resource records from Route 53.
14
14
15
15
The example provided in this project works precisely this way. It uses information from a CloudWatch event to gather information about the instance, such as its public and private DNS name, its public and private IP address, the VPC ID of the VPC that the instance was launch in, its tags, and so on. It then uses this information to create A, PTR, and CNAME records in the appropriate Route 53 public or private hosted zone. The solution persists data about the instances in an [Amazon DynamoDB](https://aws.amazon.com/dynamodb) table so it can remove resource records when instances are stopped or terminated.
16
16
17
-
##Route 53 Hosted Zones
17
+
##Route 53 Hosted Zones
18
18
19
19
Route 53 offers the convenience of domain name services without having to build a globally distributed highly reliable DNS infrastructure. It allows instances within your VPC to resolve the names of resources that run within your AWS environment. It also lets clients on the Internet resolve names of your public-facing resources. This is accomplished by querying resource record sets that reside within a Route 53 public or private hosted zone.
20
20
21
21
A private hosted zone is basically a container that holds information about how you want to route traffic for a domain and its subdomains within one or more VPCs whereas a public hosted zone is a container that holds information about how you want to route traffic from the Internet.
22
22
23
-
##Choosing between VPC DNS or Route 53 Private Hosted Zones
23
+
##Choosing between VPC DNS or Route 53 Private Hosted Zones
24
24
25
25
Admittedly, you can use VPC DNS for internal name resolution instead of Route 53 private hosted zones. Although it doesn’t dynamically create resource records, VPC DNS will provide name resolution for all the hosts within a VPC’s CIDR range.
26
26
@@ -37,15 +37,15 @@ Route 53 doesn't offer support for dynamic registration of resource record sets
37
37
38
38
This was the motivation for creating a serverless architecture that dynamically creates and removes resource records from Route 53 as EC2 instances are created and destroyed.
39
39
40
-
##DDNS/Lambda example
40
+
##DDNS/Lambda example
41
41
42
42
Make sure that you have the latest version of the AWS CLI installed locally. For more information, see [Getting Set Up with the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html).
43
43
44
44
For this example, create a new VPC configured with a private and public subnet, using [Scenario 2: VPC with Public and Private Subnets (NAT)](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Scenario2.html) from the Amazon VPC User Guide. Ensure that the VPC has the **DNS resolution** and **DNS hostnames** options set to **yes**.
45
45
46
46
After the VPC is created, you can proceed to the next steps.
47
47
48
-
#####Step 1 – Create an IAM role for the Lambda function
48
+
#####Step 1 – Create an IAM role for the Lambda function
49
49
50
50
In this step, you will use the AWS Command Line Interface (AWS CLI) to create the Identity and Access Management (IAM) role that the Lambda function assumes when the function is invoked. You also need to create an IAM policy with the required permissions and then attach this policy to the role.
51
51
@@ -116,7 +116,7 @@ aws iam create-role --role-name ddns-lambda-role --assume-role-policy-document f
116
116
```
117
117
aws iam attach-role-policy --role-name ddns-lambda-role --policy-arn <enter-your-policy-arn-here>
118
118
```
119
-
#####Step 2 – Create the Lambda function
119
+
#####Step 2 – Create the Lambda function
120
120
121
121
The Lambda function uses modules included in the Python 2.7 Standard Library and the AWS SDK for Python module (boto3), which is preinstalled as part of the Lambda service. As such, you do not need to create a deployment package for this function.
4) The output of the command returns the ARN of the newly-created function. Save this ARN, since you will need it in the next section.
152
152
153
-
#####Step 3 – Create the CloudWatch Events Rule
153
+
#####Step 3 – Create the CloudWatch Events Rule
154
154
155
155
In this step, you create the CloudWatch Events rule that triggers the Lambda function whenever CloudWatch detects a change to the state of an EC2 instance. You configure the rule to fire when any EC2 instance state changes to “running”, “shutting down”, or “stopped”. Use the **aws events put-rule** command to create the rule and set the Lambda function as the execution target:
156
156
```
@@ -166,11 +166,11 @@ Next, you add the permissions required for the CloudWatch Events rule to execute
#####Step 4 – Create the private hosted zone in Route 53
169
+
#####Step 4 – Create the private hosted zone in Route 53
170
170
171
171
To create the private hosted zone in Route 53, follow the steps outlined in [Creating a Private Hosted Zone](http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zone-private-creating.html).
172
172
173
-
#####Step 5 – Create a DHCP options set and associate it with the VPC
173
+
#####Step 5 – Create a DHCP options set and associate it with the VPC
174
174
175
175
In this step, you create a new DHCP options set, and set the domain to be that of your private hosted zone.
176
176
@@ -182,7 +182,7 @@ In this step, you create a new DHCP options set, and set the domain to be that o
182
182
183
183
3) Next, follow the steps outlined in Changing the Set of DHCP Options a VPC Uses to update the VPC to use the newly-created DHCP options set.
184
184
185
-
#####Step 6 – Launching the EC2 instance and validating results
185
+
#####Step 6 – Launching the EC2 instance and validating results
186
186
187
187
In this step, you launch an EC2 instance and verify that the function executed successfully.
188
188
@@ -241,7 +241,7 @@ In this step, you verify that your Lambda function successfully updated the Rout
241
241
10) Verify that the records have been removed from the zone file by the Lambda function.
242
242
243
243
244
-
##Conclusion
244
+
##Conclusion
245
245
246
246
Now that you’ve seen how you can combine various AWS services to automate the creation and removal of Route 53 resource records, we hope it inspires you to create your own solutions. CloudWatch Events is a powerful tool because it allows you to respond to events in real-time, such as when an instance changes state. When used with Lambda, you can create highly scalable serverless infrastructures that react instantly to infrastructure changes.
0 commit comments