更新 route53 记录中的自动缩放组 ip [英] updating the autoscaling group ips in route53 record

查看:24
本文介绍了更新 route53 记录中的自动缩放组 ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个没有 n 的自动缩放组.ec2 实例.我们创建了一个 route53 记录集(multi wieght),它指向自动缩放组的 IP 地址.对于每个 ec2 扩展操作,我想查询自动扩展组中正在运行的实例并使用 userdata 脚本更新 dns.

We have an autoscaling group with n no. of ec2 instances. We have a route53 record set (multi wieght) created which points to the ip address of autoscaling group. For every ec2 scaling action, i wanted to query the running instances in autoscaling group and update the dns using the userdata script.

我正在使用 aws cli 查询在自动缩放组中运行的 ec2 实例:

I am using aws cli to query the ec2 instances running in the autoscaling group:

aws autoscaling describe-auto-scaling-instances --region us-east-1 --output text --query "AutoScalingInstances[?AutoScalingGroupName=='myapp-asg'].InstanceId" | xargs -n1 aws ec2 describe-instances --instance-ids $ID --region us-east-1 --query "Reservations[].Instances[].PrivateIpAddress" --output text

这会以以下格式提供自动缩放组中的所有 ipaddress:

This gives all the ipaddress in the autoscaling group in below format:

20.91.0.1
20.91.0.2
20.91.0.3
20.91.0.4

使用 aws cli 我想更新 dns.以下是我正在使用的命令:

Using aws cli i want to update the dns. Below is the command i am using:

aws --region us-east-1 route53 change-resource-record-sets --hosted-zone-id "Zone-id" --change-batch '{"Changes": [{"Action": "UPSERT","ResourceRecordSet": {"Name": "'"myrecord.mydomain.com"'","Type": "A","TTL": 60,"Weight": 200,"SetIdentifier":"myrecord","ResourceRecords": [{"Value": "20.91.0.1"},{"Value": "20.91.0.2"}]}}]}'

如何自动化从第一个命令获得的 ipadress 以更新第二个命令中记录集的值

How do I automate the ipadress which i get from the first command to update the values for record sets in the second command

推荐答案

您可以只迭代从第一个命令获得的值.大概是这样

You can just iterate over the values you get from the first command. Roughly like this



AWS_ROUTE53_ZONEID="redacted"

TTL="600"

IPS=$(aws autoscaling describe-auto-scaling-instances --region us-east-1 --output text --query "AutoScalingInstances[?AutoScalingGroupName=='myapp-asg'].InstanceId" | xargs -n1 aws ec2 describe-instances --instance-ids $ID --region us-east-1 --query "Reservations[].Instances[].PrivateIpAddress" --output text)

for IP in $IPS;
for HOSTNAME in  host1 host2 host3
do

aws route53 change-resource-record-sets --hosted-zone-id $AWS_ROUTE53_ZONEID --change-batch "{ \"Changes\": [ { \"Action\": \"UPSERT\", \"ResourceRecordSet\": { \"Name\": \"$HOSTNAME\", \"Type\": \"A\", \"TTL\": $TTL, \"ResourceRecords\": [ { \"Value\": \"$IP\" } ] } } ] }"

echo "Updated the DNS Zone to $IP"

done

我实际上会使用 boto3 库编写一些 python 代码,因为这会给我各种错误处理.

I actually will write some python code using boto3 library, because that will give me all kinds of error handling.

 asg_client = boto3.client('autoscaling')
 asg_response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=["myasg"])

 instance_ids = []

 for i in asg_response['AutoScalingGroups']:
     for k in i['Instances']:
         instance_ids.append(k['InstanceId'])

获得实例后,您可以简单地对其进行迭代以更新 dns 记录 change_resource_record_sets

Once you get the instances you can simply iterate over the same to update the dns records change_resource_record_sets

def change_record(domain, subdomain, target_ip, action, ttl=900):
    """ Change the record for subdomain """
    zone_id = get_hosted_zone_id(domain)
    name = subdomain + "." + domain
    client.change_resource_record_sets(
        HostedZoneId=zone_id,
        ChangeBatch={
            "Comment": "%s subdomain %s from zone %s" % (action, subdomain, zone_id),
            "Changes": [
                {
                    "Action": action,
                    "ResourceRecordSet": {
                        "Name": name,
                        "Type": "A",
                        "ResourceRecords": [{"Value": target_ip}],
                        "TTL": ttl,
                    },
                }
            ],
        },
    )

尽管这整个事情是静态的,因为 Autoscaling 会根据需求减少和增加实例.好主意是创建一个 CloudWatch 事件规则并调用 lambda.这甚至可以删除陈旧的记录并更新记录.

This whole thing is static though, As Autoscaling brings down and up instances depending on the requirements. Good idea would be to create a CloudWatch event rule and invoke the lambda. This would even remove stale records and updates the records.

{
{
  "source": [
    "aws.autoscaling"
  ],
  "detail-type": [
    "EC2 Instance Launch Successful",
    "EC2 Instance Terminate Successful"
  ]
}

这篇关于更新 route53 记录中的自动缩放组 ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆