python - AWS Lambda : Unable to import module 'lambda_function' : No module named boto. ec2.autoscale

标签 python amazon-web-services boto aws-lambda

以下是 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

类 boto.ec2.autoscale 似乎没有被 AWS 加载。

知道这里可能出了什么问题吗?

最佳答案

对于寻找答案的人,以下 Lambda 代码片段获取所有 ASG 的列表,然后暂停它们(除了与正则表达式匹配的那些)

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)

关于python - AWS Lambda : Unable to import module 'lambda_function' : No module named boto. ec2.autoscale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41440141/

相关文章:

python - 这个flask错误是什么意思?/usr/local/bin/flask : bad interpreter:/usr/local/opt/python/bin/python2. 7: no such file or directory

python - 线程中的 pyGame

python - PyGTK 阻塞非 GUI 线程

amazon-web-services - 在 CloudFormation 中,如何链接 RDS 和 Secrets Manager,这样我就不必对密码进行硬编码?

Python 客户端支持在 Amazon EMR 上运行 Hive

python - 添加装饰器后,Flask 抛出 "Could not build url for endpoint ' 索引'"

python - 在 python 中为文件名 (amazon S3) 添加动态内容配置

amazon-web-services - 在 AWS-ECS-Task 完成其任务后,您如何停止它?

python - 如何使用 boto3 手动从 DynamoDB 有线协议(protocol)转换为 native Python 对象?

boto - 在 AWS DynamoDb 上查询 Range 键的最大值