2017-09-14 4 views
1

Filter CloudWatch Logs to extract Instance IDAWS Lambdaでpythonでイベントオブジェクトにアクセスするには?この質問にフォローアップする

私はそれのpythonでeventオブジェクトにアクセスする方法を言っていないので、それが不完全な疑問を残しと思います。

  • がインスタンス
  • 開始同じタグを持つ他のすべてのインスタンスに関連付けられているタグ値を取得する状態
  • を実行しているの変化によってトリガーされたインスタンスをお読みくださいに

    私の目標です

CloudWatchのトリガ・イベントは次のとおりです。

{ 
    "source": [ 
    "aws.ec2" 
    ], 
    "detail-type": [ 
    "EC2 Instance State-change Notification" 
    ], 
    "detail": { 
    "state": [ 
     "running" 
    ] 
    } 
} 

私はこのような例を見ることができます。

def lambda_handler(event, context): 

    # here I want to get the instance tag value 
    # and set the tag filter based on the instance that 
    # triggered the event 

    filters = [{ 
      'Name': 'tag:StartGroup', 
      'Values': ['startgroup1'] 
     }, 
     { 
      'Name': 'instance-state-name', 
      'Values': ['running'] 
     } 
    ] 

    instances = ec2.instances.filter(Filters=filters) 

私はイベントオブジェクトを見ることができますが、私はそれは状態が実行されているに変更します持っていたインスタンスのタグにドリルダウンする方法が表示されません。

トリガされたインスタンスからタグを取得するためのオブジェクト属性は何ですか。

私はそれはのようなものである疑いがある:

イベントの詳細セクションで
myTag = event.details.instance-id.tags["startgroup1"] 

答えて

0

、インスタンスIDを取得します。インスタンスIDとAWS SDKを使用すると、タグを照会できます。以下はサンプルイベントです

{ 
    "version": "0", 
    "id": "ee376907-2647-4179-9203-343cfb3017a4", 
    "detail-type": "EC2 Instance State-change Notification", 
    "source": "aws.ec2", 
    "account": "123456789012", 
    "time": "2015-11-11T21:30:34Z", 
    "region": "us-east-1", 
    "resources": [ 
    "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111" 
    ], 
    "detail": { 
    "instance-id": "i-abcd1111", 
    "state": "running" 
    } 
} 
+0

ありがとう、Vaisakh。 これは実際に多くの助けになります。イベントオブジェクトと詳細セクションが表示されますが、このinstance-idには、Pythonを使用して特定のタグを取得する方法を教えてください。 これはjson構文解析の練習ですか?またはこれはオブジェクトの属性で行うことができますか? インスタンスのタグ値がイベントオブジェクトに含まれていないように見えます。 ありがとう、 Chris。 – Chrisjx

0

ラムダに渡されるイベントデータにはインスタンスIDが含まれています。

タグの辞書を取得するには、describe_tags()に電話する必要があります。これは私が思い付いたものです

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

client.describe_tags(Filters=[ 
     { 
      'Name': 'resource-id', 
      'Values': [ 
       event['detail']['instance-id'] 
      ] 
     } 
    ] 
) 
+0

John、 boto3ドキュメントのクライアントオブジェクトへの参照が見えましたが、describe_tagsメソッドが見つかりませんでした。 私は働いている(おそらくベストではない)コードを投稿しました。それは、存在しないinstance-idでテストするとエラーをスローしますが、すぐに動作します。存在しないインスタンスリストからインスタンスを起動しない可能性が高いので、これは問題ないと思います。私たちは実際には、まだリストに残っていると思われ、私はそれを誤って選択します。しかたがない。 – Chrisjx

0

...

私はそれがよりよく行うことができる方法を教えてください。助けてくれてありがとう。

# StartMeUp_Instances_byOne 
# 
# This lambda script is triggered by a CloudWatch Event, startGroupByInstance. 
# Every evening a separate lambda script is launched on a schedule to stop 
# all non-essential instances. 
# 
# This script will turn on all instances with a LaunchGroup tag that matches 
# a single instance which has been changed to the running state. 
# 
# To start all instances in a LaunchGroup, 
# start one of the instances in the LaunchGroup and wait about 5 minutes. 
# 
# Costs to run: approx. $0.02/month 
# https://s3.amazonaws.com/lambda-tools/pricing-calculator.html 
# 150 executions per month * 128 MB Memory * 60000 ms Execution Time 
# 
# Problems: talk to chrisj 
# ====================================== 

# test system 
# this is what the event object looks like (see below) 
# it is configured in the test event object with a specific instance-id 
# change that to test a different instance-id with a different LaunchGroup 

# { "version": "0", 
# "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
# "detail-type": "EC2 Instance State-change Notification", 
# "source": "aws.ec2", 
# "account": "999999999999999", 
# "time": "2015-11-11T21:30:34Z", 
# "region": "us-east-1", 
# "resources": [ 
#  "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111" 
# ], 
# "detail": { 
#  "instance-id": "i-0aad9474", # <---------- chg this 
#  "state": "running" 
# } 
# } 
# ====================================== 

import boto3 
import logging 
import json 

ec2 = boto3.resource('ec2') 

def get_instance_LaunchGroup(iid): 
    # When given an instance ID as str e.g. 'i-1234567', 
    # return the instance LaunchGroup. 
    ec2 = boto3.resource('ec2') 
    ec2instance = ec2.Instance(iid) 
    thisTag = '' 
    for tags in ec2instance.tags: 
     if tags["Key"] == 'LaunchGroup': 
      thisTag = tags["Value"] 
    return thisTag 

# this is the entry point for the cloudwatch trigger 
def lambda_handler(event, context): 

    # get the instance id that triggered the event 
    thisInstanceID = event['detail']['instance-id'] 
    print("instance-id: " + thisInstanceID) 

    # get the LaunchGroup tag value of the thisInstanceID 
    thisLaunchGroup = get_instance_LaunchGroup(thisInstanceID) 
    print("LaunchGroup: " + thisLaunchGroup) 
    if thisLaunchGroup == '': 
     print("No LaunchGroup associated with this InstanceID - ending lambda function") 
     return 

    # set the filters 
    filters = [{ 
      'Name': 'tag:LaunchGroup', 
      'Values': [thisLaunchGroup] 
     }, 
     { 
      'Name': 'instance-state-name', 
      'Values': ['stopped'] 
     } 
    ] 

    # get the instances based on the filter, thisLaunchGroup and stopped 
    instances = ec2.instances.filter(Filters=filters) 

    # get the stopped instance IDs 
    stoppedInstances = [instance.id for instance in instances] 

    # make sure there are some instances not already started 
    if len(stoppedInstances) > 0: 
     startingUp = ec2.instances.filter(InstanceIds=stoppedInstances).start() 

    print ("Finished launching all instances for tag: " + thisLaunchGroup) 
関連する問題