amazon-web-services - 更新 route53 记录中的自动缩放组 ip
问题描述
我们有一个 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 以更新第二个命令中记录集的值
解决方案
您可以迭代从第一个命令获得的值。大致是这样的
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
我实际上会python
使用库编写一些代码boto3
,因为这会给我各种错误处理。
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,
},
}
],
},
)
不过,这整件事是静态的,AsAutoscaling
会根据要求降低和提高实例。好主意是创建一个CloudWatch
事件规则并调用 lambda。这甚至会删除过时的记录并更新记录。
{
{
"source": [
"aws.autoscaling"
],
"detail-type": [
"EC2 Instance Launch Successful",
"EC2 Instance Terminate Successful"
]
}
推荐阅读
- python - 如何将 cURL 请求转换为 Python 请求
- python - Pandas 系列 - 计算列值之间的行数
- php - 在 MySql 列中插入多个值
- spring-boot - Spring Boot Jenkins CD/CI 部署前更改配置文件
- excel - 如何根据跨多个工作表的列返回列值?
- python - 如何使用 Python for osx 或 macos 获取用户登录/注销时间
- c# - 该函数不响应while循环中的计数器
- cors - 阿里巴巴云上缓存资源的 CORS 问题
- javascript - 如何通过点击div点击radio?
- java - 如何忽略依赖项中的@ExceptionHandler?