2017-01-03 19 views
2

次はラムダ関数です。オートスケーリンググループのリストを取得して出力します。AWS Lambda: 'lambda_function'モジュールをインポートできません:boto.ec2.autoscaleという名前のモジュールがありません

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によってロードされていないように思え。

何が間違っているのでしょうか?

答えて

0

答えを探して誰かのために、ラムダ次のコードは、(regrexに一致するものを除く)全ての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) 
0

あなただけ使用する必要があります。

import boto3 
client = boto3.client('autoscaling') 

参考:私はS3と同じことをやろうとしていますhttp://boto3.readthedocs.io/en/latest/reference/services/autoscaling.html

+0

私はboto3使用したくありません。 describe_auto_scaling_groups()メソッドがJSON dictを返すメソッドとしてクライアント( 'autoscaling')を呼び出すと、配列またはリストのいずれかの戻り値型に興味があり、 'get_resource'がリストを返します。 – Spaniard89

0

。私はboto.s3.connect_to_region()が必要ですが、私は同じエラーが発生します。おそらく、lambda依存関係でbotoモジュールをインポートすると、問題が解決する可能性があります。それ以外の場合は、boto3.clientを使用してjsonレスポンスを解析して適切な値を取得する必要があります。

関連する問題