我有一个2个lambda函数,第一个得到一个实例ec2第二个检查ec2状态,我向第二个添加了一个计划事件,一个cron女巫每15分钟一次,所以现在我想验证我是否状态函数真的每15分钟调用一次,我已经检查了aws控制台中的触发器,但没有发生任何事情,我想知道我做错了什么?
这是我的handler.py文件
import sys
import schedule
import time
import logging
# Import local dependencies
sys.path.append('./site-packages')
import os
from pprint import pprint
import traceback
import json
import boto3
from botocore.exceptions import ClientError
def get_instance(client, event):
instance_name = None
instance_id = None
instance_state = None
error_message = None
# Get the instance name from the path
try:
instance_name = event['pathParameters']['name']
print( 'Instance Name: %s' % instance_name)
except:
instance_name = None
if instance_name:
r = client.describe_instances(
Filters=[{'Name':'tag:Name', 'Values': [instance_name]}]
)
# AWS' boto3 api involves a lot of looping
if len(r['Reservations']) > 0:
for res in r['Reservations']:
for ins in res['Instances']:
ins_id = ins['InstanceId']
ins_state = ins['State']['Name']
if ins_state in ('shutting-down', 'terminated'):
error_message = 'No action taken. Instance %s is %s' %(ins_id, ins_state)
else:
instance_id = ins_id
instance_state = ins_state
break
else:
error_message = 'Unable to find instance with tag:Name - %s' %instance_name
else:
error_message = 'No instance name specified'
print(error_message, instance_name, instance_id, instance_state)
return (error_message, instance_name, instance_id, instance_state)
def ec2_status(event, context):
"""Take an instance tag:name via an API call and return its status"""
body = {}
status_code = 200
try:
client = boto3.client('ec2')
# Find the instance
error, instance_name, instance_id, instance_state = get_instance(client, event)
if error:
body["message"] = error
else:
body["message"] = str(instance_id) + ' ' + str(instance_state)
except Exception as e:
print(traceback.format_exc())
status_code = 500
body["message"] = str(e)
response = {
"statusCode": status_code,
"body": json.dumps(body)
}
return response
这是我的.yml文件
service: ec2-remote
provider:
name: aws
runtime: python2.7
stage: dev
region: us-east-1
memorySize: 128
versionFunctions: false
cfLogs: true
iamRoleStatements:
- Effect: "Allow"
Action:
- "ec2:DescribeInstances"
Resource: "*" package:
exclude:
- .npmignore
- bin/** functions:
ec2-status:
handler: handler.ec2_status
description: Status ec2 instances
timeout: 30
events:
- http:
path: ec2/status/{name}
method: get
private: false
- schedule:
description: my scheduled rate event
rate: rate(15 minutes) resources:
Resources:
Ec2DashstatusLogGroup:
Properties:
RetentionInDays: "7"
答案 0 :(得分:1)
我认为你可以在Cloudwatch中看到执行日志 - >日志组 - > " LogGroupFunctionName"