【发布时间】:2020-09-30 20:40:02
【问题描述】:
我正在使用下面的 python boto3 代码来启动 Ec2
import boto3
region='us-east-1'
instance_id = 'i-06ce851edfXXXXXX'
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
resp = ec2.describe_instance_status(InstanceIds=[str(instance_id)],
IncludeAllInstances=True)
print("Response = ",resp)
instance_status = resp['InstanceStatuses'][0]['InstanceState']['Code']
print("Instance status =", instance_status)
if instance_status == 80:
ec2.start_instances(InstanceIds=[instance_id])
print("Started instance with Instance_id",instance_id)
elif instance_status == 16:
ec2.stop_instances(InstanceIds=[instance_id])
print("Stopped EC2 with Instance-ID",instance_id)
else:
print("No desired state found")
当实例处于运行状态时,我可以通过运行这个 lambda 来停止实例。
但是当实例处于停止状态并且我运行 Lambda 时,我收到以下消息并且它没有显示错误。但是当我签入控制台实例仍处于停止状态时。我无法找出实例没有进入的原因运行阶段。 实例状态 = 80 使用 Instance_id i-06ce851edfXXXXXX 启动实例
以下是使用的 IAM 角色
{
"Action": [
"ec2:StopInstances",
"ec2:StartInstances",
"ec2:RebootInstances"
],
"Resource": [
"arn:aws:ec2:us0east-1:2x83xxxxxxxxxx:instance/i-06ce851edfXXXXXX"
],
"Effect": "Allow"
【问题讨论】:
-
您是否检查过 CloudWatch 日志中的输出?
-
是的,和我上面给出的一样。
标签: python-3.x amazon-web-services amazon-ec2 aws-lambda boto3