【问题标题】:updating the autoscaling group ips in route53 record更新 route53 记录中的自动缩放组 ip
【发布时间】:2021-05-13 07:05:35
【问题描述】:

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

我正在使用 aws cli 来查询在 autoscaling 组中运行的 ec2 实例:

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

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

20.91.0.1
20.91.0.2
20.91.0.3
20.91.0.4

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

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 以更新第二个命令中记录集的值

【问题讨论】:

    标签: amazon-web-services shell aws-cli amazon-route53 aws-auto-scaling


    【解决方案1】:

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

    
    
    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 代码,因为这会给我各种错误处理。

     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

    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。这甚至会删除过时的记录并更新记录。

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

    【讨论】:

    • 感谢您的回答。在我的情况下,我想更新 route53 dns 记录以获取包含所有自动缩放组 IP 地址的 IP 地址的加权值。下面是一个例子: {"Name": "'"myrecord.mydomain.com"'","Type": "A","TTL": 60,"Weight": 200,"SetIdentifier":"myrecord", "ResourceRecords": [{"Value": "20.91.0.1"},{"Value": "20.91.0.2"}]}}]}' 基本上我希望一条记录在 dns 中有多个 ipaddress。在这里记录“myrecord.mydomain.com”,我想拥有所有带有 wieghted dns 的 asg ip 地址
    • @user14907472 你仍然可以用同样的方式 change_record 功能你只需更新那里的 IP 地址。如果您想通过 shell 脚本执行此操作,请考虑通过构建模板然后在命令行中使用相同的模板,如How do I create a simple resource record set in Amazon Route 53 using the AWS CLI? 所述。因为在 shell 中构建会很棘手。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2019-01-05
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多