以下是使用AWS SDK for Python(boto3)的示例代码,演示如何在AWS EC2实例和Route 53中创建域名解析记录:
import boto3
ec2_client = boto3.client('ec2')
route53_client = boto3.client('route53')
response = ec2_client.run_instances(
ImageId='ami-xxxxxxxx', # 替换为所需的AMI ID
InstanceType='t2.micro',
MinCount=1,
MaxCount=1
)
instance_id = response['Instances'][0]['InstanceId']
print("EC2实例创建成功,实例ID为:" + instance_id)
response = ec2_client.describe_instances(
InstanceIds=[instance_id]
)
public_ip = response['Reservations'][0]['Instances'][0]['PublicIpAddress']
print("EC2实例的公有IP地址为:" + public_ip)
response = route53_client.change_resource_record_sets(
HostedZoneId='your_hosted_zone_id', # 替换为您的Hosted Zone ID
ChangeBatch={
'Changes': [
{
'Action': 'CREATE',
'ResourceRecordSet': {
'Name': 'example.com', # 替换为您的域名
'Type': 'A',
'TTL': 300,
'ResourceRecords': [
{
'Value': public_ip
},
],
}
},
]
}
)
print("Route 53域名解析记录创建成功。")
请注意,在实际使用中,您需要将示例代码中的参数替换为您自己的值,例如AMI ID、Hosted Zone ID和域名。
上一篇:AWS EC2共享租户的资源