【问题标题】:AWS boto function calls from Lambda code来自 Lambda 代码的 AWS boto 函数调用
【发布时间】:2016-02-29 15:19:48
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    import boto
    sns = boto.connect_sns()
    

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

    【讨论】:

    • 第一个来自 create_lambda 函数
    猜你喜欢
    • 2019-01-21
    • 2019-05-12
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多