使用Boto3调用CreateSnapshots时ExcludeDataVolumeIds参数验证错误
使用Boto3创建快照时的参数验证错误解决
问题场景
尝试用Boto3创建实例快照,代码如下:
snapshot_response = client.create_snapshots( Description='super important backup', InstanceSpecification={ 'InstanceId': 'i-something', 'ExcludeBootVolume': True, 'ExcludeDataVolumeIds': ['vol-something1','vol-something2'], }, CopyTagsFromSource='volume' )
运行后触发参数验证错误:
[ERROR] ParamValidationError: Parameter validation failed: Unknown parameter in InstanceSpecification: "ExcludeDataVolumeIds", must be one of: InstanceId, ExcludeBootVolume Traceback (most recent call last): File "/something/some_dir/some_script.py", line 50, in some_function snapshot_response = client.create_snapshots( File "/var/runtime/botocore/client.py", line 391, in _api_call return self._make_api_call(operation_name, kwargs) File "/var/runtime/botocore/client.py", line 691, in _make_api_call request_dict = self._convert_to_request_dict( File "/var/runtime/botocore/client.py", line 739, in _convert_to_request_dict request_dict = self._serializer.serialize_to_request( File "/var/runtime/botocore/validate.py", line 360, in serialize_to_request raise ParamValidationError(report=report.generate_report())
原因分析
ExcludeDataVolumeIds是AWS EC2 API后续新增的参数,当前使用的Boto3/botocore版本过低,不支持该参数,因此触发参数校验失败。
解决办法
升级依赖版本
直接升级Boto3和botocore到最新稳定版,即可支持该参数:pip install --upgrade boto3 botocore兼容旧版本的替代方案
如果无法升级依赖,可以手动筛选需要备份的卷,逐个创建快照:# 获取实例关联的所有卷 volumes = client.describe_volumes( Filters=[{'Name': 'attachment.instance-id', 'Values': ['i-something']}] )['Volumes'] # 排除不需要备份的卷(指定的卷+启动盘) excluded_volume_ids = {'vol-something1', 'vol-something2'} target_volumes = [ vol for vol in volumes if vol['VolumeId'] not in excluded_volume_ids and not vol['Attachments'][0]['BootVolume'] ] # 批量创建快照 snapshots = [] for vol in target_volumes: snap = client.create_snapshot( VolumeId=vol['VolumeId'], Description='super important backup', CopyTagsFromSource='volume' ) snapshots.append(snap)
内容的提问来源于stack exchange,提问作者dedunu
相关产品推荐
相关产品推荐

