Skip to content

Commit 61a49f3

Browse files
committed
Initial commit
1 parent 470ccdf commit 61a49f3

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

handler.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import json
2+
import uuid
3+
from urllib2 import urlopen
4+
from urlparse import urljoin
5+
6+
import boto3
7+
8+
import logging
9+
log = logging.getLogger()
10+
11+
URL = 'https://mandrillapp.com/api/1.0/'
12+
13+
dynamodb = boto3.resource('dynamodb')
14+
15+
Config = dynamodb.Table('Config')
16+
17+
18+
def send_email(event, context):
19+
uid = event['pathParameters']['uuid']
20+
try:
21+
uid = uuid.UUID(uid)
22+
except ValueError:
23+
log.info('Invalid UUID: %r', uid)
24+
return {'statusCode': 404, 'body': ''}
25+
26+
# Try to retrieve config
27+
resp = Config.get_item(Key={'uuid': str(uid)})
28+
config = resp.get('Item')
29+
30+
if not config:
31+
return {'statusCode': 400, 'body': ''}
32+
33+
try:
34+
data = json.loads(event['body'])
35+
except json.JSONDecodeError as e:
36+
log.info('Invalid Body: %r', e)
37+
return {'statusCode': 400, 'body': 'Invalid JSON format'}
38+
39+
# XXX Generate content
40+
req = {
41+
'key': config['key'],
42+
'template_name': config['template'],
43+
'template_content': [],
44+
'message': {
45+
'to': [
46+
{'email': data['target-email'], 'type': 'to'}
47+
],
48+
'merge': True,
49+
'merge_langauge': 'handlebars',
50+
'global_merge_vars': [
51+
{'name': key, 'content': value}
52+
for key, value in data.items()
53+
],
54+
},
55+
}
56+
57+
# Post to Mandrill
58+
resp = urlopen(urljoin(URL, 'messages/send-template.json'), data=json.dumps(req))
59+
if resp.getcode() != 200:
60+
body = resp.read()
61+
log.error('Error from Mandrill: [%r] %r', resp.getcode(), body)
62+
return {'statusCode': 400, 'body': 'Service Error'}
63+
64+
content = json.loads(resp.read())
65+
if isinstance(content, dict):
66+
log.warn('Rejected by Mandrill: %r', content)
67+
return {'statusCode': 400, 'body': content}
68+
69+
return {'statusCode': 204, 'body': '', 'headers': {"Access-Control-Allow-Origin" : "*"}}

serverless.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
service: mandrillaxe
3+
4+
provider:
5+
name: aws
6+
runtime: python2.7
7+
region: ap-southeast-2
8+
memorySize: 128
9+
# cfLogs: true
10+
11+
iamRoleStatements:
12+
- Effect: Allow
13+
Action:
14+
- dynamodb:GetItem
15+
- dynamodb:PutItem
16+
Resource:
17+
- Fn::Join:
18+
- ""
19+
- - "arn:aws:dynamodb:${self:provider.region}:"
20+
- Ref: AWS::AccountId
21+
- ":table/Config"
22+
23+
package:
24+
exclude:
25+
- .npmignore
26+
- event.json
27+
28+
functions:
29+
sendEmail:
30+
handler: handler.send_email
31+
events:
32+
- http:
33+
method: post
34+
path: send/{uuid}
35+
cors: true
36+
37+
resources:
38+
Resources:
39+
ConfigTable:
40+
Type: AWS::DynamoDB::Table
41+
Properties:
42+
TableName: Config
43+
AttributeDefinitions:
44+
- AttributeName: uuid
45+
AttributeType: S
46+
KeySchema:
47+
- AttributeName: uuid
48+
KeyType: HASH
49+
ProvisionedThroughput:
50+
ReadCapacityUnits: 5
51+
WriteCapacityUnits: 5

0 commit comments

Comments
 (0)