Skip to content

Commit 7f03af2

Browse files
authored
Merge pull request #16 from d3prof3t/aws-chalice
Serverless SMS Service
2 parents 2d13c5e + feb188e commit 7f03af2

File tree

7 files changed

+116
-0
lines changed

7 files changed

+116
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0",
3+
"app_name": "serverless-sms-service",
4+
"stages": {
5+
"dev": {
6+
"api_gateway_stage": "api",
7+
"environment_variables": {
8+
"ACCOUNT_SID": "<your-account-sid>",
9+
"AUTH_TOKEN": "<your-auth-token>",
10+
"FROM_NUMBER": "<from-number>",
11+
"TO_NUMBER": "<to-number>"
12+
}
13+
}
14+
}
15+
}

serverless-sms-service/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.chalice/deployments/
2+
.chalice/venv/

serverless-sms-service/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Serverless SMS Sender Service
2+
3+
This repository is linked to https://realpython.com/aws-lambda-serverless-python/ post.
4+
5+
A step by step tutorial on how to build and deploy your very first serverless app. Multiple steps are divided in
6+
3 tags where each tag serves as an incremental feature. Let's begin by cloning the repository and follow the steps from the post.
7+
8+
9+
## Phase 1 - Hello World
10+
11+
A simple Hello World app showcasing the simplicity of how a serverless app can be deployed in no time.
12+
13+
14+
## Phase 2 - SMS Sender Service
15+
16+
Evolving our Hello World app from the last section, to a more robust and real word SMS sender app, using Twilio APIs underneath.
17+
18+
## Phase 3 - Refactoring
19+
20+
Refactoring the code using few best practices pertaining to large serverless projects.

serverless-sms-service/app.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# core imports
2+
from chalice import Chalice, Response
3+
from twilio.base.exceptions import TwilioRestException
4+
5+
# app level imports
6+
from chalicelib import sms
7+
8+
app = Chalice(app_name="sms-shooter")
9+
10+
11+
@app.route("/")
12+
def index():
13+
return {"hello": "world"}
14+
15+
16+
@app.route("/service/sms/send", methods=["POST"])
17+
def send_sms():
18+
request_body = app.current_request.json_body
19+
if request_body:
20+
try:
21+
resp = sms.send(request_body)
22+
if resp:
23+
return Response(
24+
status_code=201,
25+
headers={"Content-Type": "application/json"},
26+
body={
27+
"status": "success",
28+
"data": resp.sid,
29+
"message": "SMS successfully sent",
30+
},
31+
)
32+
else:
33+
return Response(
34+
status_code=200,
35+
headers={"Content-Type": "application/json"},
36+
body={
37+
"status": "failure",
38+
"message": "Please try again!!!",
39+
},
40+
)
41+
except TwilioRestException as exc:
42+
return Response(
43+
status_code=400,
44+
headers={"Content-Type": "application/json"},
45+
body={"status": "failure", "message": exc.msg},
46+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from os import environ as env
2+
from twilio.rest import Client
3+
4+
# Twilio Config
5+
ACCOUNT_SID = env.get("ACCOUNT_SID")
6+
AUTH_TOKEN = env.get("AUTH_TOKEN")
7+
FROM_NUMBER = env.get("FROM_NUMBER")
8+
TO_NUMBER = env.get("TO_NUMBER")
9+
10+
# create a twilio client using account_sid and auth token
11+
tw_client = Client(ACCOUNT_SID, AUTH_TOKEN)
12+
13+
14+
def send(payload_params=None):
15+
""" send sms to the specified number """
16+
msg = tw_client.messages.create(
17+
from_=FROM_NUMBER, body=payload_params["msg"], to=TO_NUMBER
18+
)
19+
20+
if msg.sid:
21+
return msg
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
attrs==17.4.0
2+
botocore==1.12.30
3+
chalice==1.6.0
4+
click==6.7
5+
docutils==0.14
6+
enum-compat==0.0.2
7+
jmespath==0.9.3
8+
python-dateutil==2.7.3
9+
six==1.11.0
10+
typing==3.6.4
11+
urllib3==1.24
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
twilio==6.18.1

0 commit comments

Comments
 (0)