0
所以我有這個啓動ec2實例的boto3腳本。但是當我運行這個lambda函數時,函數describe_instance_status返回空白的InstanceStatus數組。所以程序結束後,說索引我們的範圍。有什麼建議麼?boto3問題檢查ec2實例狀態
import boto3
from time import sleep
region = 'your region name'
def lambda_handler(event, context):
cye_production_web_server_2 = 'abcdefgh'
ec2 = boto3.client('ec2',region)
start_response = ec2.start_instances(
InstanceIds=[cye_production_web_server_2, ],
DryRun=False
)
print(
'instance id:',
start_response['StartingInstances'][0]['InstanceId'],
'is',
start_response['StartingInstances'][0]['CurrentState']['Name']
)
status = None
counter = 5
while (status != 'ok' and counter > 0):
status_response = ec2.describe_instance_status(
DryRun=False,
InstanceIds=[cye_production_web_server_2, ],
)
status = status_response['InstanceStatuses'][0]['SystemStatus'] ['Status']
sleep(5) # 5 second throttle
counter=counter-1
print(status_response)
print('status is', status.capitalize())
您是否檢查過lambda(cloudwatch)日誌以獲取任何錯誤消息? – helloV
列表索引超出範圍:IndexError Traceback(最近調用最後一個): lambda_handler中的第31行文件「/var/task/lambda_function.py」 status = status_response ['InstanceStatuses'] ['SystemStatus '] ['狀態'] IndexError:列表索引超出範圍 –
您確定這可以用作獨立腳本嗎? – helloV