AWS Lambda: 无法导入模块'lambda_function': 没有名为boto.ec2.autoscale的模块。

3
以下是我编写的Lambda函数,它获取自动扩展组列表并将其打印出来。
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类。

这里可能出了什么问题?

2个回答

2

如果有人正在寻找答案,以下这段Lambda代码可以获取所有自动伸缩组的列表,然后暂停它们(除了与正则表达式匹配的那些)

import json
import boto3


regions = ["eu-central-1"]
autoscaling = boto3.client('autoscaling')

def lambda_handler(event, context):

  response = autoscaling.describe_auto_scaling_groups(MaxRecords=100)
  #print response
  #print(response['AutoScalingGroups'][0]['AutoScalingGroupName'])
  autoscaling_group_to_suspend = []
  for doc in response['AutoScalingGroups']:
    response_parsed = doc['AutoScalingGroupName']
    autoscaling_group_to_suspend.append(response_parsed)

  #print autoscaling_group_to_suspend
  import re
  regex = re.compile(r'es-data-asg|consul|influxdb|vault|es-master')
  filtered = filter(lambda i: not regex.search(i), autoscaling_group_to_suspend)
  filtered = [i for i in autoscaling_group_to_suspend if not regex.search(i)]
  print filtered

  if len(filtered) > 0:
    for x in filtered:
        autoscaling.suspend_processes(AutoScalingGroupName=x)

0

我正在尝试使用s3做同样的事情。我需要boto.s3.connect_to_region(),但是我得到了相同的错误。可能在lambda依赖中导入boto模块可以解决这个问题。否则,我们可能需要使用boto3.client并解析json响应以获取适当的值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接