【发布时间】:2017-01-25 19:18:56
【问题描述】:
以下是我编写的 Lambda 函数,它获取 Autoscaling 组的列表并打印出来。
import json
import boto3
import boto.ec2.autoscale
role = "arn:aws:iam::XXXXXXXXXX:role/lambda-autoshutdown-role"
regions = ["eu-central-1"]
autoscaling = boto3.client('autoscaling')
class App(object):
def __init__(self,RoleArn):
self.RoleArn = RoleArn
if self.RoleArn != "local":
sts_client = boto3.client('sts')
self.sts = sts_client.assume_role(
RoleArn=self.RoleArn,
RoleSessionName="lambda_poweroff")["Credentials"]
def get_resource(self,region="eu-central-1"):
if self.RoleArn == "local":
return boto3.resource(region_name=region)
else:
return boto.ec2.autoscale.connect_to_region(
region_name=region,
aws_access_key_id=self.sts['AccessKeyId'],
aws_secret_access_key=self.sts['SecretAccessKey'],)
def lambda_handler(event, context):
a = App(role)
for region in regions:
asgs = a.get_resource(region)
# locate all running instances
#autoscaling_groups_to_suspend = []
#for i in asgs:
# print asgs[i]
print '[%s]' % ', '.join(map(str, asgs))
此函数使用:boto.ec2.autoscale.connect_to_region 连接并返回对象。
但是当我尝试在 AWS 上部署它时,我收到以下错误:
Unable to import module 'lambda_function': No module named boto.ec2.autoscale
AWS 似乎没有加载 boto.ec2.autoscale 类。
知道这里可能出了什么问题吗?
【问题讨论】:
标签: python amazon-web-services boto aws-lambda