要将AWS Route 53配置为指向Elastic Beanstalk URL,可以使用AWS SDK或AWS管理控制台进行操作。以下是使用AWS SDK(基于Python)的示例代码:
import boto3
# 创建Route 53的客户端
route53 = boto3.client('route53')
# 获取现有的Hosted Zone ID
response = route53.list_hosted_zones_by_name(DNSName='example.com.')
hosted_zone_id = response['HostedZones'][0]['Id']
# 创建一条新的ALIAS记录集
response = route53.change_resource_record_sets(
HostedZoneId=hosted_zone_id,
ChangeBatch={
'Comment': 'Update Elastic Beanstalk URL',
'Changes': [{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': 'example.com.',
'Type': 'A',
'AliasTarget': {
'HostedZoneId': 'Z2XXXXXXXXXXXX', # Elastic Beanstalk的ALIAS目标Hosted Zone ID
'DNSName': 'example-env.elasticbeanstalk.com.', # Elastic Beanstalk的URL
'EvaluateTargetHealth': False
}
}
}]
}
)
print('Route 53 record set updated successfully.')
在上面的示例中,替换以下值:
'example.com.'
:您的域名'Z2XXXXXXXXXXXX'
:Elastic Beanstalk的ALIAS目标Hosted Zone ID'example-env.elasticbeanstalk.com.'
:Elastic Beanstalk的URL运行上面的代码后,它将在AWS Route 53中创建/更新名为example.com.
的ALIAS记录集,将其指向Elastic Beanstalk的URL。请确保已正确配置AWS SDK的凭证。