Python 2.7 AWS Lambda函数按标记调整实例大小
我有一个AWS Lambda函数,可以调整EC2实例的大小,但实例id是硬编码的-我更喜欢使用标记,但无法让它工作 以下是有效的代码:Python 2.7 AWS Lambda函数按标记调整实例大小,python-2.7,amazon-web-services,aws-lambda,Python 2.7,Amazon Web Services,Aws Lambda,我有一个AWS Lambda函数,可以调整EC2实例的大小,但实例id是硬编码的-我更喜欢使用标记,但无法让它工作 以下是有效的代码: # lambda function import boto3 import botocore import os # define environment variables Instance_Type = os.environ['InstanceType'] #define the connections client = boto3.client('ec
# lambda function
import boto3
import botocore
import os
# define environment variables
Instance_Type = os.environ['InstanceType']
#define the connections
client = boto3.client('ec2')
RunningInstances = 'i-02a1130833c928708'
def lambda_handler(event, context):
# Stop the instance
client.stop_instances(InstanceIds=[RunningInstances])
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[RunningInstances])
print "Stopped", RunningInstances
# Change the instance type
client.modify_instance_attribute(InstanceId=RunningInstances,
Attribute='instanceType', Value=Instance_Type)
print RunningInstances, "resized down to", Instance_Type
# Start the instance
client.start_instances(InstanceIds=[RunningInstances])
print "Started", RunningInstances
以下是我想从事的工作:
import boto3
import botocore
import os
# define environment variables
Instance_Type = os.environ['InstanceType']
#define the connections
client = boto3.client('ec2')
#RunningInstances = 'i-02a1130833c928708'
def lambda_handler(event, context):
filters=[
{'Name': 'tag-key', 'Values': ['ResizeDown']},
{'Name': 'tag-value', 'Values': ['True']},
{'Name': 'instance-state-name','Values': ['running']}
]
#filter the instances
instances = client.instances.filter(Filters=filters)
#locate all running instances
RunningInstances = [instance.id for instance in instances]
# Stop the instance
client.stop_instances(InstanceIds=[RunningInstances])
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[RunningInstances])
print "Stopped", RunningInstances
# Change the instance type
client.modify_instance_attribute(InstanceId=RunningInstances,
Attribute='instanceType', Value=Instance_Type)
print RunningInstances, "resized down to", Instance_Type
# Start the instance
client.start_instances(InstanceIds=[RunningInstances])
print "Started", RunningInstances
我得到的错误是:
“EC2”对象没有属性“实例”:AttributeError
回溯(最近一次呼叫最后一次):
lambda_处理程序中的文件“/var/task/lambda_function.py”,第21行
实例=客户端.实例.过滤器(过滤器=过滤器)
文件“/var/runtime/botocore/client.py”,第555行,在getattr
自我。类名称,项目)
AttributeError:“EC2”对象没有属性“实例”
正如您的错误所说,您的
EC2
对象没有属性实例
,如果您在中查找,您将不会在那里找到它。您应该能够在您已经设置的过滤器上使用boto客户端上的函数,这将像以前一样返回相关实例,即:
instances = client.describe_instances(Filters=filters)
这一行:
instances=client.instances.filter(Filters=Filters)
正在使用EC2资源的格式,而不是EC2客户端的格式
您可以使用:
ec2_resource=boto3.resource('ec2'))
实例=ec2_资源.instances.filter(过滤器=Filters)
基本上,这是引用AWS的两种不同方式。
client
方法映射到AWS API,而resource
方法更像python。感谢您的回复,它修复了错误,但之后代码不起作用。正如John指出的,我使用两种不同的方法来引用aws。我不是python程序员,所以我会坚持使用有效的代码。我选择danimal的答案作为解决方案,因为它修复了错误消息,并且是第一个,代码的其余部分抛出了太多错误,我坚持使用有效的代码。