You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用boto3的change_resource_record_sets为LB添加含多IP的Route53 A记录

问题根因

Python字典结构中,同一个键名重复声明时,后声明的值会直接覆盖前面的值。你将两个IP的Value字段写在了同一个ResourceRecords数组的元素字典里,最终只有最后一个IP10.201.10.12被保留提交给Route53,因此仅单个IP生效。

修正后代码

#!/usr/bin/env python3
import boto3

# TODO use env variables for names, Values and zonename

def lambda_handler(event, context):
    client = boto3.client('route53')
    response = client.change_resource_record_sets(
        HostedZoneId='Z03115902SB93XHRQS9LT',
        ChangeBatch={
            'Changes': [
                {
                    'Action': 'UPSERT',
                    'ResourceRecordSet': {
                        'Name': 'web-staging-lb.ggnp3ggdjcvwpfpqhsuwda.soemdomain.com',
                        'Type': 'A',
                        'TTL': 60,
                        # 每个IP单独作为一个字典元素放在ResourceRecords数组中
                        'ResourceRecords': [
                            {
                                'Value': '10.201.11.246'
                            },
                            {
                                'Value': '10.201.10.12'
                            }
                        ],
                    },
                },
            ],
            'Comment': 'Record to access private ips of alb',
        },
    )

优化建议

如果需要关联的IP数量较多,可以提前把IP存入列表,动态生成ResourceRecords结构,避免重复手写代码,示例逻辑如下:

# 提前定义IP列表
ip_list = ['10.201.11.246', '10.201.10.12', '10.201.10.13']
# 动态生成ResourceRecords结构
resource_records = [{'Value': ip} for ip in ip_list]

内容的提问来源于stack exchange,提问作者awiechert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 19:48:02