Skip to content

Commit b0bd383

Browse files
author
Sunil Mallya
committed
Add SAM
1 parent feb2248 commit b0bd383

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ This example uses [Twilio](https://www.twilio.com/) to save an image from your m
1414

1515
**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)
1616

17-
#Usage
18-
19-
Try it by sending an MMS to (650) 200-1944.
20-
21-
![Example](https://s3-us-west-2.amazonaws.com/mauerbac-hosting/screenshot2.png)
22-
23-
S3 Link: https://s3-us-west-2.amazonaws.com/mauerbac-selfie/ingest-images/19145824224/795221908928951.png
24-
2517
# Building the App
2618

2719
Step-by-step on how to configure, develop & deploy this app on AWS.
@@ -34,6 +26,33 @@ Step-by-step on how to configure, develop & deploy this app on AWS.
3426
5. Create an IAM role with access to the S3 bucket & the DynamoDB table.
3527
6. Create/login to a Twilio account & create a phone number with MMS capability.
3628

29+
### *NEW* Serverless Application Model (SAM) Deployment
30+
31+
In the AWS Region you plan to deploy, make sure you have an existing Amazon S3 bucket in which SAM can create the deployment artifacts.
32+
33+
Else create a new bucket using the following AWS CLI command:
34+
35+
```
36+
aws s3 mb s3://<your-bucket-name>
37+
```
38+
To deploy the project for the first time with SAM, and for each subsequent code update, run both of
39+
the following AWS CLI commands in order.
40+
41+
NOTE: Make sure you update the template.yaml and swagger.yaml (sam/ folder) with the code-uri, region and
42+
account id before running the commands. Refer to comments in the files for more info
43+
44+
```
45+
aws cloudformation package \
46+
--template-file template.yaml \
47+
--output-template-file template-out.yaml \
48+
--s3-bucket <your-s3-bucket-name>
49+
50+
aws cloudformation deploy \
51+
--template-file <path-to-file/template-out.yaml \
52+
--stack-name <STACK_NAME> \
53+
--capabilities CAPABILITY_IAM
54+
```
55+
3756
###Lambda
3857
1. Create a new Lambda function. I've provided the function, so we can skip a blueprint.
3958
2. Give it a name and description. Use Python 2.7 for runtime.
@@ -103,7 +122,7 @@ Click Test. At the bottom of the page you view Execution result and the log outp
103122

104123
##Architecture
105124

106-
![Architecture](https://s3-us-west-2.amazonaws.com/mauerbac-hosting/Screen+Shot+2015-09-30+at+4.00.47+PM.png)
125+
![Architecture](https://s3.amazonaws.com/smallya-useast-1/twilio-apig/architecture.png)
107126

108127
### Lambda Deployment
109128

sam/swagger.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
swagger: "2.0"
3+
info:
4+
version: "2016-12-20T18:27:47Z"
5+
title: "twilio-apigateway"
6+
basePath: "/Prod"
7+
schemes:
8+
- "https"
9+
paths:
10+
/addphoto:
11+
get:
12+
consumes:
13+
- "application/json"
14+
produces:
15+
- "application/xml"
16+
responses:
17+
200:
18+
description: "200 response"
19+
x-amazon-apigateway-integration:
20+
responses:
21+
default:
22+
statusCode: "200"
23+
responseTemplates:
24+
application/xml: "#set($inputRoot = $input.path('$'))\n<?xml version=\"\
25+
1.0\" encoding=\"UTF-8\"?>\n<Response>\n <Message>\n <Body>\n\
26+
\ $inputRoot\n </Body>\n </Message>\n</Response> "
27+
requestTemplates:
28+
application/json: "{\n \"body\" : \"$input.params('Body')\",\n \"\
29+
fromNumber\" : \"$input.params('From')\",\n \"image\" : \"$input.params('MediaUrl0')\"\
30+
,\n \"numMedia\" : \"$input.params('NumMedia')\"\n}"
31+
# NOTE: Replace <<region>> and <<account>> fields
32+
uri: arn:aws:apigateway:<<region>>:lambda:path/2015-03-31/functions/arn:aws:lambda:<<region>>:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations
33+
passthroughBehavior: "when_no_templates"
34+
httpMethod: "POST"
35+
contentHandling: "CONVERT_TO_TEXT"
36+
type: "aws"

sam/template.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# To deploy for the first time, and for each update,
2+
# run both of the following commands in order:
3+
#
4+
# aws cloudformation package \
5+
# --template-file template.yaml \
6+
# --output-template-file template-out.yaml \
7+
# --s3-bucket <your-s3-bucket-name>
8+
#
9+
# aws cloudformation deploy \
10+
# --template-file <path-to-file/template-out.yaml \
11+
# --stack-name <STACK_NAME> \
12+
# --capabilities CAPABILITY_IAM
13+
14+
AWSTemplateFormatVersion: '2010-09-09'
15+
Transform: 'AWS::Serverless-2016-10-31'
16+
Description: Lambda handler for API Gateway - Twilio integration
17+
Resources:
18+
LambdaFunction:
19+
Type: 'AWS::Serverless::Function'
20+
Properties:
21+
Handler: lambda_function.lambda_handler
22+
Runtime: python2.7
23+
CodeUri: s3://<bucket-name>/<path> #UPDATE
24+
Description: Lambda handler for API Gateway - Twilio integration
25+
MemorySize: 256
26+
Timeout: 60
27+
Events:
28+
AddPhotoApi:
29+
Type: Api
30+
Properties:
31+
RestApiId: !Ref ApiGatewayApi
32+
Path: /addphoto
33+
Method: GET
34+
35+
ApiGatewayApi:
36+
Type: AWS::Serverless::Api
37+
Properties:
38+
DefinitionUri: s3://<bucket>/swagger.yaml #UPDATE
39+
StageName: Prod
40+
Variables:
41+
# NOTE: Before using this template, replace the <<region>> and <<account>> fields
42+
# in Lambda integration URI in the swagger file to region and accountId
43+
# you are deploying to
44+
LambdaFunctionName: !Ref LambdaFunction
45+
46+
Outputs:
47+
ApiUrl:
48+
Description: URL of your API endpoint
49+
Value: !Join
50+
- ''
51+
- - https://
52+
- !Ref ApiGatewayApi
53+
- '.execute-api.'
54+
- !Ref 'AWS::Region'
55+
- '.amazonaws.com/Prod'

0 commit comments

Comments
 (0)