Upload
julien-lecadoumsc
View
20
Download
1
Embed Size (px)
Citation preview
Auto Update AWS Route53 Record Set Using bash script and auto starting your Docker instances
1. The background It often happens to run a single AWS EC2 instance and attach it an AWS Route53 Hosted Zone. This can be a lab instance or something that doesn’t need to be run on multiple instances.
In my case, I have an EC2 instance that hosts multiple Docker instances. To save cost, I don’t need to use an EIP 1(Elastic IP) address for this instance. And because it is a lab instance, I shut it down every time I am done with my trainings or testing sessions. My EC2 instance uses multiple services and features but here are the ones concerned for the scope of this article:
- Route53 - Public IP address enabled - Docker Image
2. The problemEvery time, I start my EC2 instance for a new lab session, AWS EC2 service will provide (lease) a new public address to my EC2 instance. Thus, to access my application through that web, I must update the Route53 “Record Set” that allows me to access my application through the web. This process can take several minutes because I have to do the following tasks:
- Start my instance- SSH the instance - Start my Docker instances- Update the Route53 Record Set
3. SolutionAutomate all this using bash scripts as shown below:
1 An EIP address is not free when the EC2 Instance they are attached to isn’t running. Ref: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html#eip-basics
4. The implementation This article will assume that the user has fairly knowledge about AWS EC2. I will also assume that the user knows how to SSH an EC2 Instance. You should have administrative right the EC2 instance as well.
1. Create change Record Set bash script
a. Retrieve the Hosted Zones Id and the Authoritative record setWe need the hosted Zones Id of the Record set that we want to automatically update.
- Go to https://console.aws.amazon.com/route53/ or from the console click on the Service button (on the top left) and select the Route53 Service
- Click the Hosted Zones link on the left as shown below
- Select the target hosted Zones and copy the Hosted Zones Id
the only manual step that triggers the automation
Start EC2
Using the rc.local file
Start the Docker Instances Using Seperate
bash script
Update Route53 RecordSet
Retrieve the Hosted Zone Id
Retrieve the Authoritative record
set name
Create Change-RecordSet document
template
- Let’s us assume that the hosted zone Id is Z1R8UBAEXAMPLE- Click on the target hosted zone to view the hosted zone’s record sets- Select the target Record Set and note the record set name
For this demo, we will use the following values
HOSTED ZONE ID Z1R8UBAEXAMPLE
RECORD SET EXAMPLE.COMTYPE ATTL 300
b. Create Change-Record Set document templateThe syntax of the change record set document template is base of the one made available by AWS2.
We will modify it to accommodate our need. We will create a json document. Here is the syntax
1. { 2. "Comment": "Change record set", 3. "Changes": [{ 4. "Action": "UPSERT", 5. "ResourceRecordSet": { 6. "Name": "example.com.", 7. "Type": "A", 8. "TTL": 300, 9. "ResourceRecords": [{ 10. "Value": "public_ipv4_here" 11. }] 12. } 13. }] 14. }
Using your preferred text editor copy and paste the script above. Replace the “example.com” with your target record set name.
Note: Please do not remove the “.” at the end of the record set name.
Save the file as a json file, here will save it as change.json.temp
2. Create the charge record set bash script
2 change-resource-record-sets syntax : http://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html#change-resource-record-sets
The change record set bash script will execute following tasks:
#!/bin/bash
# Replace current Templaterm -f /home/ec2-user/change.jsoncp /home/ec2-user/change.json.temp /home/ec2-user/change.json
# Getting the new IP Addressip=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
# Update the public-ipv4sed -i "s/public_ipv4_here/"$ip"/g" /home/ec2-user/change.json
# Update Route53 Record Setaws route53 change-resource-record-sets --hosted-zone-id Z1R8UBAEXAMPLE --change-batch file:///home/ec2-user/change.json
- Copy and paste the bash script above. - Replace the Z1R8UBAEXAMPLE with your target record set name- And save the file as change.sh
3. Applying the changes to the EC2 InstanceNow it is time to apply the changes to our EC2 instance. Please note that these commands are valid for Amazon Linux
- SSH to your EC2 instance using your favorite application - Switch to the root user using the command (sudo su)
a. Updating the « rc.local» fileType the command below to open the rc.local file
nano /etc/rc.local - Copy and paste the script below
docker start docker-id-1
docker start docker-id-2
docker start docker-id-3
Replace current json file from template
Getting the new IP Address
Update the public-ipv4 on the new json file
Update the Route53 Record Set
sh /home/ec2-user/change.ch
- Clr + o and Clr +x to save and close the file
b. Create change.json.tempGo to the EC2-user home, if not yet there
Type: cd /home/ec2-user/
Type: nano change.json.temp
Copy and paste the change.json.temp that you have written earlier
- Clr + o and Clr +x to save and close the file
c. Create the change.sh file While you are still in the EC2-user home location
Type: nano change.sh
Copy and paste the change.ch that you have written earlier
- Clr + o and Clr +x to save and close the file
With this final step, you’re done automating the update of your EC2 Route53 record set name.
We took the opportunity to show how you can automate the start of your docker instances as well
Thank You
Julien LECADOU
AWS Solution Architect - Associate