2

I am trying to modify the default aws-lambda scheduled events code, to make a call to SNS. Whenever there is an error, I want to publish to a SNS topic instead of raising the error, and then using cloudwatch.

The boto import is failing. Any ideas why this is failing.

Error message, when I try to test:

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

Lambda code:

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 Answer 1

4

This is how I import SNS via python:

import boto
sns = boto.connect_sns()

OR

import boto3
client = boto3.client('sns')
Sign up to request clarification or add additional context in comments.

1 Comment

The first one is from a create_lambda function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.