Skip to content

Commit e315175

Browse files
author
Sunil Mallya
committed
update readme
1 parent b0bd383 commit e315175

File tree

2 files changed

+77
-52
lines changed

2 files changed

+77
-52
lines changed

README.md

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
This example uses [Twilio](https://www.twilio.com/) to save an image from your mobile phone to the AWS cloud. A user sends an image using MMS to a Twilio phone number which sends a request to an Amazon API Gateway endpoint that triggers a Lambda function. The app then returns a publicly accessible link to the image in AWS S3. This app uses AWS Lambda, API Gateway, DynamoDB & S3. It is also 100% serverless!
44

5-
###AWS Lambda
5+
NOTE: The project has been updated to use AWS Serverless Application Model (AWS SAM)
66

7-
[Lambda](https://aws.amazon.com/lambda/) is a compute service that runs your code in response to events. Events are triggered or invoked by resources in your AWS environment or via API Gateway. Here our Lambda function is triggered by an API Gateway endpoint that Twilio hits after an MMS is received. The Lambda function is responsible for writing user info to DynamoDB, writing the image to S3 with meta data and returning a response to Twilio.
8-
9-
###Amazon API Gateway
10-
[API Gateway](https://aws.amazon.com/api-gateway/) is a fully managed API as a service where you can create, publish, maintain, monitor, and secure APIs at any scale. In this app, we use API Gateway to create an endpoint for Twilio to make a GET request. API Gateway transforms Twilio's URL encoded request into a JSON object, so that Lambda can process it. Lastly, API Gateway takes Lambda's response and builds an XML object for Twilio.
11-
12-
###Amazon DynamoDB & Amazon S3
13-
[DynamoDB](https://aws.amazon.com/dynamodb/) is Amazon's non-relational database service. This app leverages DynamoDB to store user data. [S3](https://aws.amazon.com/s3/) provides developers with object level storage that is endlessly scalable. We use S3 to store images received via MMS.
7+
##Architecture
148

15-
**Please Note:** Twilio is a 3rd party service that has terms of use that the user is solely responsible for complying with (https://www.twilio.com/legal/tos)
9+
![Architecture](https://s3.amazonaws.com/smallya-useast-1/twilio-apig/architecture.png)
1610

1711
# Building the App
1812

1913
Step-by-step on how to configure, develop & deploy this app on AWS.
2014

21-
###Housekeeping
15+
### Pre-Requisites
2216
1. Sign-in to AWS or [Create an Account](https://us-west-2.console.aws.amazon.com).
2317
2. Pick a region in the console and be consistent throughout this app. Use either `us-east-1`, `us-west-2` & `eu-west-1`.
2418
3. Create a table in DynamoDB with a single Hash for primary key of type String. We don't need any additional indexes and you can keep the read/write capacity at 1 for this example. [Screenshot](https://s3-us-west-2.amazonaws.com/mauerbac-hosting/dynamoDB.png)
@@ -41,6 +35,9 @@ the following AWS CLI commands in order.
4135
NOTE: Make sure you update the template.yaml and swagger.yaml (sam/ folder) with the code-uri, region and
4236
account id before running the commands. Refer to comments in the files for more info
4337

38+
You can use the basic_lambda_function.py as the reference for a simple backend to test the end to
39+
end flow
40+
4441
```
4542
aws cloudformation package \
4643
--template-file template.yaml \
@@ -53,6 +50,46 @@ aws cloudformation deploy \
5350
--capabilities CAPABILITY_IAM
5451
```
5552

53+
### Generating the Lambda code
54+
55+
Connect to a 64-bit Amazon Linux instance via SSH.
56+
57+
```
58+
ssh -i key.pem ec2-user@public-ip-address
59+
```
60+
61+
Ensure basic build requirements are installed.
62+
63+
```
64+
sudo yum install python27-devel python27-pip gcc
65+
```
66+
67+
Install native dependencies required by Pillow.
68+
69+
```
70+
sudo yum install libjpeg-devel zlib-devel
71+
```
72+
73+
Create and activate a virtual environment.
74+
75+
```
76+
virtualenv ~/lambda-apigateway-twilio-tutorial
77+
78+
source ~/lambda-apigateway-twilio-tutorial/bin/activate
79+
```
80+
81+
Install libraries in the virtual environment.
82+
83+
```
84+
pip install Pillow
85+
86+
pip install boto3
87+
88+
pip install twilio
89+
```
90+
91+
### Manually creating the API Gateway and Lambda deployment (for blog compatilibity)
92+
5693
###Lambda
5794
1. Create a new Lambda function. I've provided the function, so we can skip a blueprint.
5895
2. Give it a name and description. Use Python 2.7 for runtime.
@@ -120,45 +157,4 @@ Click Test. At the bottom of the page you view Execution result and the log outp
120157
2. All Lambda interactions are logged in Cloudwatch logs. View the logs for debugging.
121158
3. Lambda/API Gateway Forums
122159

123-
##Architecture
124-
125-
![Architecture](https://s3.amazonaws.com/smallya-useast-1/twilio-apig/architecture.png)
126-
127-
### Lambda Deployment
128-
129-
Connect to a 64-bit Amazon Linux instance via SSH.
130-
131-
```
132-
ssh -i key.pem ec2-user@public-ip-address
133-
```
134-
135-
Ensure basic build requirements are installed.
136-
137-
```
138-
sudo yum install python27-devel python27-pip gcc
139-
```
140-
141-
Install native dependencies required by Pillow.
142-
143-
```
144-
sudo yum install libjpeg-devel zlib-devel
145-
```
146-
147-
Create and activate a virtual environment.
148-
149-
```
150-
virtualenv ~/lambda-apigateway-twilio-tutorial
151-
152-
source ~/lambda-apigateway-twilio-tutorial/bin/activate
153-
```
154-
155-
Install libraries in the virtual environment.
156-
157-
```
158-
pip install Pillow
159-
160-
pip install boto3
161-
162-
pip install twilio
163-
```
164-
160+
**Please Note:** Twilio is a 3rd party service that has terms of use that the user is solely responsible for complying with (https://www.twilio.com/legal/tos)

basic_lambda_function.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Basic Twilio handler function
3+
'''
4+
5+
import boto3
6+
import random
7+
import StringIO
8+
import urllib2
9+
10+
from boto3.dynamodb.conditions import Key
11+
from boto3.session import Session
12+
13+
# create an S3 & Dynamo session
14+
s3 = boto3.resource('s3')
15+
session = Session()
16+
17+
def lambda_handler(event, context):
18+
19+
message = event['body']
20+
from_number = event['fromNumber']
21+
pic_url = event['image']
22+
num_media = event['numMedia']
23+
24+
if num_media != '0':
25+
twilio_resp = "Hi I got an image @ location %s" % pic_url
26+
else:
27+
twilio_resp = 'No image found'
28+
29+
return twilio_resp

0 commit comments

Comments
 (0)