AWS boto函数从Lambda代码调用

时间:2015-11-26 13:38:54

标签: amazon-web-services aws-lambda boto3

我正在尝试修改默认的aws-lambda预定事件代码,以调用SNS。每当出现错误时,我想发布到SNS主题而不是引发错误,然后使用cloudwatch。

boto导入失败。任何想法为什么失败。

错误消息,当我尝试测试时:

Unable to import module 'lambda_function': cannot import name sns

Lambda代码:

from __future__ import print_function

from datetime import datetime
from urllib2 import urlopen
from boto3 import sns #I have also tried import boto3
import json

SITE = 'http://my-site.com/'  # URL of the site to check
EXPECTED = 'My Site'  # String expected to be on the page


def validate(res):
    '''Return False to trigger the canary

    Currently this simply checks whether the EXPECTED string is present.
    However, you could modify this to perform any number of arbitrary
    checks on the contents of SITE.
    '''
    return EXPECTED in res


def lambda_handler(event, context):
    print('Checking {} at {}...'.format(SITE, event['time']))
    try:
        if not validate(urlopen(SITE).read()):
            raise Exception('Validation failed')
    except:
        print('Check failed!')
        # pub = boto.sns.connect_to_region('us-east-1').publish(topic='<my topic name'',message='site down!')
        raise
    else:
        print('Check passed!')
        return event['time']
    finally:
        print('Check complete at {}'.format(str(datetime.now())))

1 个答案:

答案 0 :(得分:4)

这是我通过python导入SNS的方式:

import boto
sns = boto.connect_sns()

OR

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