Mitch Garnaat

Getting SNS sample working again

......@@ -81,7 +81,7 @@ class KinesisEventSource(EventSource):
try:
response = self._lambda.call(
'update_event_source_mapping',
FunctionName=function.name,
UUID=self._get_uuid(function),
Enabled=self.enabled
)
LOG.debug(response)
......@@ -242,6 +242,8 @@ class SNSEventSource(EventSource):
except Exception:
LOG.exception('Unable to add SNS event source')
enable = add
def update(self, function):
self.add(function)
......@@ -257,6 +259,11 @@ class SNSEventSource(EventSource):
except Exception:
LOG.exception('Unable to remove event source %s', self.arn)
disable = remove
def status(self, function):
LOG.debug('status for SNS notification for %s', function.name)
return self.exists(function)
status = self.exists(function)
if status:
status['EventSourceArn'] = status['TopicArn']
return status
......
......@@ -122,8 +122,9 @@ def status(ctx):
if status['event_sources']:
for event_source in status['event_sources']:
if event_source:
line = ' {}: {}'.format(
event_source['EventSourceArn'], event_source['State'])
arn = event_source.get('EventSourceArn')
state = event_source.get('State', 'Enabled')
line = ' {}: {}'.format(arn, state)
click.echo(click.style(line, fg='green'))
else:
click.echo(click.style(' None', fg='green'))
......
{
"Version": "2012-10-17",
"Statement":[
{
"Sid":"Stmt1428510662000",
"Effect":"Allow",
"Action":["dynamodb:*"],
"Resource":["arn:aws:dynamodb:us-east-1:084307701560:table/snslambda"]
}
]
}
import logging
LOG = logging.getLogger()
LOG.setLevel(logging.INFO)
def handler(event, context):
for record in event['Records']:
start_time = record['Sns']['Timestamp']
LOG.info('start_time: %s', start_time)
---
# change profile and region to suit your needs
profile: personal
region: us-east-1
resources: resources.json
iam:
policy:
description: A policy used with the Kappa SNS->DynamoDB example
name: LambdaSNSSamplePolicy
document: LambdaSNSSamplePolicy.json
role:
name: SNSSampleRole
policy: LambdaSNSSamplePolicy
lambda:
name: SNSSample
zipfile_name: SNSSample.zip
description: Testing SNS -> DynamoDB Lambda handler
path: messageStore.js
handler: messageStore.handler
runtime: nodejs
memory_size: 128
timeout: 3
permissions:
-
statement_id: sns_invoke
action: lambda:invokeFunction
principal: sns.amazonaws.com
# change this to refer to your own SNS topic
source_arn: arn:aws:sns:us-east-1:084307701560:lambda_topic
event_sources:
-
# change this to refer to your own SNS topic
arn: arn:aws:sns:us-east-1:084307701560:lambda_topic
test_data: input.json
\ No newline at end of file
{
"TableName": "snslambda",
"AttributeDefinitions": [
{
"AttributeName": "SnsTopicArn",
"AttributeType": "S"
},
{
"AttributeName": "SnsPublishTime",
"AttributeType": "S"
},
{
"AttributeName": "SnsMessageId",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "SnsTopicArn",
"KeyType": "HASH"
},
{
"AttributeName": "SnsPublishTime",
"KeyType": "RANGE"
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "MesssageIndex",
"KeySchema": [
{
"AttributeName": "SnsMessageId",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 1
}
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
---
name: kappa-sns-sample
environments:
dev:
# Change the profile and region to suit your application
profile: <your profile name>
region: <your region name>
policy:
resources:
- arn: <arn to your SNS topic>
actions:
- "*"
- arn: arn:aws:logs:*:*:*
actions:
- "*"
event_sources:
- arn: <arn to your SNS topic>
lambda:
description: A simple SNS example
handler: sns.handler
runtime: python2.7
memory_size: 128
timeout: 3
console.log('Loading event');
var aws = require('aws-sdk');
var ddb = new aws.DynamoDB({params: {TableName: 'snslambda'}});
exports.handler = function(event, context) {
var SnsMessageId = event.Records[0].Sns.MessageId;
var SnsPublishTime = event.Records[0].Sns.Timestamp;
var SnsTopicArn = event.Records[0].Sns.TopicArn;
var LambdaReceiveTime = new Date().toString();
var itemParams = {Item: {SnsTopicArn: {S: SnsTopicArn},
SnsPublishTime: {S: SnsPublishTime}, SnsMessageId: {S: SnsMessageId},
LambdaReceiveTime: {S: LambdaReceiveTime} }};
ddb.putItem(itemParams, function() {
context.done(null,'');
});
};
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Creates the DynamoDB Table needed for the example",
"Resources" : {
"snslambda" : {
"Type" : "AWS::DynamoDB::Table",
"Properties" : {
"AttributeDefinitions": [
{
"AttributeName" : "SnsTopicArn",
"AttributeType" : "S"
},
{
"AttributeName" : "SnsPublishTime",
"AttributeType" : "S"
}
],
"KeySchema": [
{ "AttributeName": "SnsTopicArn", "KeyType": "HASH" },
{ "AttributeName": "SnsPublishTime", "KeyType": "RANGE" }
],
"ProvisionedThroughput" : {
"ReadCapacityUnits" : 5,
"WriteCapacityUnits" : 5
}
}
}
},
"Outputs" : {
"TableName" : {
"Value" : {"Ref" : "snslambda"},
"Description" : "Table name of the newly created DynamoDB table"
}
}
}