来自 Lambda 代码的 AWS boto 函数调用

AWS boto function calls from Lambda code

我正在尝试修改默认的 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())))

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

import boto
sns = boto.connect_sns()

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