gpt4 book ai didi

python - AWS Lambda boto3 : Instance launch from lambda boto3 python eroor

转载 作者:行者123 更新时间:2023-11-28 16:56:15 25 4
gpt4 key购买 nike

尝试从 python 函数实例启动实例时未启动但未收到 python 语法错误。

import boto3

ec2 = boto3.resource('ec2', region_name='us-east-2')

def lambda_handler(event, context):
images = ec2.images.filter(
Filters=[
{
'Name': 'description',
'Values': [
'lambdaami',
]
},
],
Owners=[
'self'
])

amis = sorted(images, key=lambda x: x['CreationDate'], reverse=True)
print amis[0]['ImageId']
INSTANCE = ec2.create_instance(ImageId='ImageId', InstanceType='t2.micro', MinCount=1, MaxCount=1)
print(INSTANCE[0].id)

请帮忙......

最佳答案

你已经定义了 ec2 两次,

ec2 = boto3.client('ec2')
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')

甚至再次为客户服务。请仅使用一个客户端资源。此外,没有 create_instance,这似乎是资源函数 create_instances 的拼写错误。


这是一个例子:

import boto3

ec2 = boto3.resource('ec2', region_name='us-east-2')

def lambda_handler(event, context):
images = ec2.images.filter(
Filters=[
{
'Name': 'description',
'Values': [
'lambdaami',
]
},
],
Owners=[
'self'
])

AMI = sorted(images, key=lambda x: x.creation_date, reverse=True)
IMAGEID = AMI[0].image_id

INSTANCE = ec2.create_instances(ImageId=IMAGEID, InstanceType='t2.micro', MinCount=1, MaxCount=1)
print(INSTANCE[0].image_id)

要从实例制作图像并等待它,

import boto3
import time

ec2 = boto3.resource('ec2', region_name='us-east-2')

def lambda_handler(event, context):
instanceId = 'What instance id you want to create an image'

response = ec2.Instance(instanceId).create_image(Name='Image Name')
imageId = response.image_id

while(ec2.Image(imageId).state != 'available'):
time.sleep(5) # Wait for 5 seconds for each try.

# Since we know the imageId, no needs for other codes

instance = ec2.create_instances(ImageId=imageId, InstanceType='t2.micro', MinCount=1, MaxCount=1)
print(instance[0].image_id)

关于python - AWS Lambda boto3 : Instance launch from lambda boto3 python eroor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com