Rajesh Chamarthi
Rajesh Chamarthi

Reputation: 18818

AWS boto function calls from Lambda code

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())))

Upvotes: 2

Views: 2517

Answers (1)

Raghav
Raghav

Reputation: 521

This is how I import SNS via python:

import boto
sns = boto.connect_sns()

OR

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

Upvotes: 4

Related Questions