Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions serverless-sms-service/.chalice/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0",
"app_name": "serverless-sms-service",
"stages": {
"dev": {
"api_gateway_stage": "api",
"environment_variables": {
"ACCOUNT_SID": "<your-account-sid>",
"AUTH_TOKEN": "<your-auth-token>",
"FROM_NUMBER": "<from-number>",
"TO_NUMBER": "<to-number>"
}
}
}
}
2 changes: 2 additions & 0 deletions serverless-sms-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.chalice/deployments/
.chalice/venv/
20 changes: 20 additions & 0 deletions serverless-sms-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Serverless SMS Sender Service

This repository is linked to https://realpython.com/aws-lambda-serverless-python/ post.

A step by step tutorial on how to build and deploy your very first serverless app. Multiple steps are divided in
3 tags where each tag serves as an incremental feature. Let's begin by cloning the repository and follow the steps from the post.


## Phase 1 - Hello World

A simple Hello World app showcasing the simplicity of how a serverless app can be deployed in no time.


## Phase 2 - SMS Sender Service

Evolving our Hello World app from the last section, to a more robust and real word SMS sender app, using Twilio APIs underneath.

## Phase 3 - Refactoring

Refactoring the code using few best practices pertaining to large serverless projects.
46 changes: 46 additions & 0 deletions serverless-sms-service/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# core imports
from chalice import Chalice, Response
from twilio.base.exceptions import TwilioRestException

# app level imports
from chalicelib import sms

app = Chalice(app_name="sms-shooter")


@app.route("/")
def index():
return {"hello": "world"}


@app.route("/service/sms/send", methods=["POST"])
def send_sms():
request_body = app.current_request.json_body
if request_body:
try:
resp = sms.send(request_body)
if resp:
return Response(
status_code=201,
headers={"Content-Type": "application/json"},
body={
"status": "success",
"data": resp.sid,
"message": "SMS successfully sent",
},
)
else:
return Response(
status_code=200,
headers={"Content-Type": "application/json"},
body={
"status": "failure",
"message": "Please try again!!!",
},
)
except TwilioRestException as exc:
return Response(
status_code=400,
headers={"Content-Type": "application/json"},
body={"status": "failure", "message": exc.msg},
)
21 changes: 21 additions & 0 deletions serverless-sms-service/chalicelib/sms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from os import environ as env
from twilio.rest import Client

# Twilio Config
ACCOUNT_SID = env.get("ACCOUNT_SID")
AUTH_TOKEN = env.get("AUTH_TOKEN")
FROM_NUMBER = env.get("FROM_NUMBER")
TO_NUMBER = env.get("TO_NUMBER")

# create a twilio client using account_sid and auth token
tw_client = Client(ACCOUNT_SID, AUTH_TOKEN)


def send(payload_params=None):
""" send sms to the specified number """
msg = tw_client.messages.create(
from_=FROM_NUMBER, body=payload_params["msg"], to=TO_NUMBER
)

if msg.sid:
return msg
11 changes: 11 additions & 0 deletions serverless-sms-service/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
attrs==17.4.0
botocore==1.12.30
chalice==1.6.0
click==6.7
docutils==0.14
enum-compat==0.0.2
jmespath==0.9.3
python-dateutil==2.7.3
six==1.11.0
typing==3.6.4
urllib3==1.24
1 change: 1 addition & 0 deletions serverless-sms-service/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twilio==6.18.1