8000 Fixed formatting · dacdo/aws-lambda-ddns-function@c8c1095 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8c1095

Browse files
authored
Fixed formatting
1 parent c7b01f9 commit c8c1095

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff 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
22

3-
##Introduction
3+
## Introduction
44

55
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.
66

77
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.
88

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
1010

1111
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.
1212

1313
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.
1414

1515
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.
1616

17-
##Route 53 Hosted Zones
17+
## Route 53 Hosted Zones
1818

1919
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.
2020

2121
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.
2222

23-
##Choosing between VPC DNS or Route 53 Private Hosted Zones
23+
## Choosing between VPC DNS or Route 53 Private Hosted Zones
2424

2525
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.
2626

@@ -37,15 +37,15 @@ Route 53 doesn't offer support for dynamic registration of resource record sets
3737

3838
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.
3939

40-
##DDNS/Lambda example
40+
## DDNS/Lambda example
4141

4242
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).
4343

4444
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**.
4545

4646
After the VPC is created, you can proceed to the next steps.
4747

48-
#####Step 1 – Create an IAM role for the Lambda function
48+
##### Step 1 – Create an IAM role for the Lambda function
4949

5050
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.
5151

@@ -116,7 +116,7 @@ aws iam create-role --role-name ddns-lambda-role --assume-role-policy-document f
116116
```
117117
aws iam attach-role-policy --role-name ddns-lambda-role --policy-arn <enter-your-policy-arn-here>
118118
```
119-
#####Step 2 – Create the Lambda function
119+
##### Step 2 – Create the Lambda function
120120

121121
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.
122122

@@ -150,7 +150,7 @@ aws lambda create-function --function-name ddns_lambda --runtime python2.7 --rol
150150
```
151151
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.
152152

153-
#####Step 3 – Create the CloudWatch Events Rule
153+
##### Step 3 – Create the CloudWatch Events Rule
154154

155155
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:
156156
```
@@ -166,11 +166,11 @@ Next, you add the permissions required for the CloudWatch Events rule to execute
166166
```
167167
aws lambda add-permission --function-name ddns_lambda --statement-id 45 --action lambda:InvokeFunction --principal events.amazonaws.com --source-arn <enter-your-cloudwatch-events-rule-arn-here>
168168
```
169-
#####Step 4 – Create the private hosted zone in Route 53
169+
##### Step 4 – Create the private hosted zone in Route 53
170170

171171
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).
172172

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
174174

175175
In this step, you create a new DHCP options set, and set the domain to be that of your private hosted zone.
176176

@@ -182,7 +182,7 @@ In this step, you create a new DHCP options set, and set the domain to be that o
182182

183183
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.
184184

185-
#####Step 6 – Launching the EC2 instance and validating results
185+
##### Step 6 – Launching the EC2 instance and validating results
186186

187187
In this step, you launch an EC2 instance and verify that the function executed successfully.
188188

@@ -241,7 +241,7 @@ In this step, you verify that your Lambda function successfully updated the Rout
241241
10) Verify that the records have been removed from the zone file by the Lambda function.
242242

243243

244-
##Conclusion
244+
## Conclusion
245245

246246
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.  
247247

0 commit comments

Comments
 (0)
0