要跟進這個問題: Filter CloudWatch Logs to extract Instance ID如何使用AWS Lambda中的python訪問事件對象?
我認爲它留下的問題不完整的,因爲它並沒有說如何使用Python訪問事件對象。
我的目標是:
- 讀取被改變運行狀態
- 獲得與實例
- 開始具有相同標記的所有其他實例相關聯的標籤值觸發實例
的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"]
謝謝,Vaisakh。 這實際上有很大幫助。我看到事件對象和詳細信息部分,但是,請問,如何使用python爲此實例標識獲取特定標記。 這是一個json解析練習嗎?或者這可以通過對象屬性來完成嗎? 實例的標記值看起來不在事件對象中。 謝謝, 克里斯。 – Chrisjx