如何根据 EC2 实例标签名称自动执行 AWS Route53 DNS 更新



我想实现一种基于实例的 EC2 标签自动更新 Route53 DNS 的方法。

想象一下,我可以有一个名为 dns_name 的标签名称,它应该记录机器的预期 dns 名称。

如果我关闭计算机,升级它并重新启动它,我确实想在不更改任何内容的情况下更新DNS。

我不想在计算机上安装 DNS 更新服务,因为这需要我将 AWS 凭证放在所有虚拟机上,并将其部署到这些虚拟机。

使用 AWS EC2 标签声明预期名称似乎要容易得多。cron 作业可以轻松检查更改并更新 DNS。

有人实施过吗?

计算机上的 DNS 更新服务不需要 AWS 凭证。您最好改用 AWS IAM 实例配置文件。我已使用此方法在服务器启动时运行脚本,以使用最新的 IP 地址更新 Route53 记录。

如果您想在单个服务器上调度一个 cron,该服务器根据实例标签处理更新所有服务器的 Route53 记录,那么这当然是可能的。它只有几行代码。

我强烈建议使用弹性IP。这就是他们的确切目的。对于此实现,请转到EC2 => Network & Security => Elastic IP => Allocate New Address => Actions => Associate Address =>(选择您要查找的实例标签名称)

EIP复制到剪贴板,然后转到Route53 => Create Hosted Zone =>去想那些步骤 => Go to Record Sets => Create Record Set => 对于Name:,您可以有或没有www.=> Type: A - IPv4 => Alias: NO => TTL: 无论您想要什么=> Value:您的EIP地址=> Save Record Set

如果您有与实例关联的弹性 IP,则可以根据需要重启、交换、停止、更新、更改、调整形状!EIP 不会去任何地方。

更新:您可以通过 Route53 API 实现所有这些自动化。这些是执行此操作的步骤,因为您必须知道该过程才能使用 API 调用执行此操作。

我试图做我认为你试图做的事情。

我从这个链接中找到了这个脚本,这是一个很好的起点。

https://medium.com/@dbclin/a-bash-script-to-update-an-existing-aws-security-group-with-my-new-public-ip-address-d0c965d67f28

#!/bin/bash
# Extract information about the Instance
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id/)
AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/)
MY_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4/)
# Extract tags associated with instance
ZONE_TAG=$(aws ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`AUTO_DNS_ZONE`].Value' --output text)
NAME_TAG=$(aws ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`AUTO_DNS_NAME`].Value' --output text)
# Update Route 53 Record Set based on the Name tag to the current Public IP address of the Instance
aws route53 change-resource-record-sets --hosted-zone-id $ZONE_TAG --change-batch '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"'$NAME_TAG'","Type":"A","TTL":300,"ResourceRecords":[{"Value":"'$MY_IP'"}]}}]}'

这包含 Mark B 建议的解决方案的详细信息。 如果您没有任何可用的 IP,弹性 IP 就不起作用,而亚马逊只允许您使用 5 个。

我使用 user14441273 粘贴的脚本作为起点。 提供的链接已失效(将您重定向到与 Route53 无关的文章),并且该帖子没有提供有关如何ZONE_TAG或NAME_TAG设置所需标签的线索。

无论如何执行此操作,您都需要允许对实例配置文件角色执行操作。

这是产品化的工作脚本。 我从本地文件系统获取实例名称,这比使用网络服务更快、更可靠。 将域更改为要使用的域:

#!/usr/bin/env bash
set -e
shopt -s xpg_echo
set +u
DNS_DOMAIN=yourdomain.com
TTL_S=30  # You want this low or you will have to wait up to this long for changes to propagate
PATH="$PATH:/usr/local/bin"
# Extract information about the Instance
INSTNAME=$(ls -ld /var/lib/cloud/instance | sed 's@.*/@@;')
[ -n "$INSTNAME" ] || {
    echo 'Failed to determine Instance name' 1>&2
    false
}
[ -f /tmp/date.txt ] && {
      echo "date present" 1>&2
    false
}
AWS_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone  | sed 's/.$//')
[ -n "$AWS_REGION" ] || {
    echo 'Failed to determine AWS Region' 1>&2
    false
}
HOSTNAME=$(aws ec2 describe-tags --region $AWS_REGION --filters Name=resource-type,Values=instance Name=resource-id,Values=$INSTNAME Name=key,Values=Name --query 'Tags[0].Value' --output text) 
[ -n "$HOSTNAME" ] || {
    echo 'Failed to determine DNS Hostname to update' 1>&2
    false
}
MY_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4/)
[ -n "$MY_IP" ] || {
    echo 'Failed to determine my public IP address' 1>&2
    false
}
ZONE_ID=$(aws route53 --region $AWS_REGION list-hosted-zones-by-name --region $AWS_REGION --dns-name $DNS_DOMAIN --query 'HostedZones[0].Id' --output text)
[ -n "$ZONE_ID" ] || {
    echo 'Failed to determine my Route53 Zone ID' 1>&2
    false
}
aws route53 change-resource-record-sets --hosted-zone-id $ZONE_ID --region $AWS_REGION --change-batch '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"'${HOSTNAME}.$DNS_DOMAIN'","Type":"A","TTL":'$TTL_S',"ResourceRecords":[{"Value":"'$MY_IP'"}]}}]}'
enter code here

假设您运行的是最新型号的 Linux 变体,下面是一个有效的服务脚本。 文学硕士我已将其配置为从挂载了 NFS 的/usr/local/sbin 运行 shell 脚本:

[Unit]
Description=Update Route53 DNS Service with instance's current public IP addr
RequiresMountsFor=/usr/local
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/updateRoute53.bash
RemainAfterExit=no
StandardOutput=truncate:/var/log/updateRoute53.log
[Install]
WantedBy=multi-user.target

最新更新