Python 3.x AWS-如何获取卷状态
我有一个脚本正在加密当前未加密的实例卷。最后两个步骤是分离未加密卷并附加新的加密卷。如果我使用Python 3.x AWS-如何获取卷状态,python-3.x,amazon-web-services,amazon-ec2,boto3,Python 3.x,Amazon Web Services,Amazon Ec2,Boto3,我有一个脚本正在加密当前未加密的实例卷。最后两个步骤是分离未加密卷并附加新的加密卷。如果我使用time.sleep(180)在附加卷进程开始之前创建一个等待期,它就会起作用。如果它能确保前一步成功完成,这将更有效率。这就是我使用while语句的原因 import boto3, json, sys, time from botocore.exceptions import ClientError if len(sys.argv) < 2: print("A profile
time.sleep(180)
在附加卷进程开始之前创建一个等待期,它就会起作用。如果它能确保前一步成功完成,这将更有效率。这就是我使用while语句的原因
import boto3, json, sys, time
from botocore.exceptions import ClientError
if len(sys.argv) < 2:
print("A profile parameter is required.")
profile = input("Enter a profile name: ")
boto3.setup_default_session(profile_name=profile)
else:
profile = (sys.argv[1])
boto3.setup_default_session(profile_name=profile)
ec2 = boto3.resource('ec2')
instances = ec2.instances.filter(InstanceIds=[sys.argv[2]])
for i in instances:
i_name = "unnamed"
r = i.root_device_name
i_az = i.placement['AvailabilityZone']
if i.tags is None:
continue
for tag in i.tags:
if tag['Key'] == "Name":
i_name = tag['Value']
if tag["Key"] == 'Costcenter':
i_div = tag['Value']
print('\n')
print(' {}'.format(i.id))
for x in i.block_device_mappings:
volumes = i.volumes.all()
d = x.get('DeviceName')
e = x.get('Ebs')
g = e.get('VolumeId')
for v in volumes:
if v.id == g:
f = v.size
h = v.encrypted
if h == False:
snap_name = i_name + "_" + d
ec2.instances.filter(InstanceIds=[i.id]).stop()
while i.state['Name'] != 'stopped':
time.sleep(10)
i.load()
snap = ec2.create_snapshot(
VolumeId = g,
TagSpecifications=[
{
'ResourceType': 'snapshot',
'Tags' : [
{
'Key': 'Name',
'Value': snap_name
},
{
'Key': 'Backup',
'Value': 'Daily'
},
{
'Key': 'Costcenter',
'Value': i_div
},
],
},
],
Description = 'Snapshot of volume ({})'.format(v.id)
)
snap.load()
while snap.state != 'completed':
time.sleep(10)
snap.load()
snap_id = snap.id
vol = ec2.create_volume(
AvailabilityZone = i_az,
Encrypted = True,
SnapshotId = snap_id,
VolumeType = 'gp3',
TagSpecifications=[
{
'ResourceType': 'volume',
'Tags': [
{
'Key': 'Name',
'Value': i_name
},
{
'Key': 'Backup',
'Value': 'Daily'
},
{
'Key': 'Costcenter',
'Value': i_div
},
],
},
]
)
while vol.state != 'available':
time.sleep(10)
vol.load()
vol_id = vol.id
volume = ec2.Volume(v.id)
detach_vol = volume.detach_from_instance(
Device = d,
Force = False,
InstanceId = i.id,
VolumeId = v.id,
DryRun = False
)
while detach_vol != 'detached':
time.sleep(10)
detach_vol.load()
vol_id = vol.id
attach_vol = volume.attach_to_instance(
VolumeId = vol_id,
Device = d,
InstanceId = i.id,
DryRun = False
)
while attach_vol.state != 'attached':
time.sleep(10)
attach_vol.load()
这就是我在分离部分遇到的错误。如果我注释掉while语句并使用time.sleep(180)
它将到达attach部分,并且基本上得到与detach块相同的错误
Traceback (most recent call last):
File "C:\Users\jmoorhead\bin\EC2-Vol-Encryption.py", line 131, in <module>
while detach_vol.state != 'detached':
AttributeError: 'dict' object has no attribute 'state'
回溯(最近一次呼叫最后一次):
文件“C:\Users\jmoorhead\bin\EC2 Vol Encryption.py”,第131行,在
分离卷状态时!='分离':
AttributeError:“dict”对象没有属性“state”
我曾尝试使用response['responsemetadata']['httpstatuscode']
而不是while语句,但也无法使其正常工作。任何能给予的帮助都将不胜感激
Volume.detach_from_instance()
返回dict
,而不是Volume
对象。您应该使用detach\u vol[“state”]
而不是detach\u vol.state
,这就是错误指示的内容
EC2.water.VolumeAvailable
或EC2.water.VolumeInUse
,具体取决于您的逻辑(没有专用的“卷已分离”服务生):
我能够为分离和附加代码块创建一个功能性的
while
语句
volume = ec2.Volume(v.id)
detach_vol = volume.detach_from_instance(
Device = d,
Force = False,
InstanceId = i.id,
VolumeId = v.id,
DryRun = False
),
while volume.state != "available":
time.sleep(10)
volume.load()
volume = ec2.Volume(vol_id)
attach_vol = volume.attach_to_instance(
VolumeId = vol_id,
Device = d,
InstanceId = i.id,
DryRun = False
)
while volume.state != "in-use":
time.sleep(10)
volume.load()
@OleksiiDonoha
detach_vol[“state”]
也产生了一个错误<代码>回溯(最后一次调用):文件“C:\Users\jmoorhead\bin\EC2 Vol Encryption.py”,第132行,在while detach_Vol[“state”]!=“detached”:KeyError:“state”则响应不包含state。请参考我的答案,并尝试使用服务员,这可能有助于感谢您的回复和boto3文档的链接。我提出了一个与其他while语句一致的解决方案。
volume = ec2.Volume(v.id)
detach_vol = volume.detach_from_instance(
Device = d,
Force = False,
InstanceId = i.id,
VolumeId = v.id,
DryRun = False
),
while volume.state != "available":
time.sleep(10)
volume.load()
volume = ec2.Volume(vol_id)
attach_vol = volume.attach_to_instance(
VolumeId = vol_id,
Device = d,
InstanceId = i.id,
DryRun = False
)
while volume.state != "in-use":
time.sleep(10)
volume.load()