diff --git a/serverless-sms-service/.chalice/config.json b/serverless-sms-service/.chalice/config.json new file mode 100644 index 0000000000..a45f7e2f96 --- /dev/null +++ b/serverless-sms-service/.chalice/config.json @@ -0,0 +1,15 @@ +{ + "version": "2.0", + "app_name": "serverless-sms-service", + "stages": { + "dev": { + "api_gateway_stage": "api", + "environment_variables": { + "ACCOUNT_SID": "", + "AUTH_TOKEN": "", + "FROM_NUMBER": "", + "TO_NUMBER": "" + } + } + } +} diff --git a/serverless-sms-service/.gitignore b/serverless-sms-service/.gitignore new file mode 100644 index 0000000000..3dd60a9721 --- /dev/null +++ b/serverless-sms-service/.gitignore @@ -0,0 +1,2 @@ +.chalice/deployments/ +.chalice/venv/ diff --git a/serverless-sms-service/README.md b/serverless-sms-service/README.md new file mode 100644 index 0000000000..f046a0844d --- /dev/null +++ b/serverless-sms-service/README.md @@ -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. diff --git a/serverless-sms-service/app.py b/serverless-sms-service/app.py new file mode 100644 index 0000000000..a9bd1cce8f --- /dev/null +++ b/serverless-sms-service/app.py @@ -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}, + ) diff --git a/serverless-sms-service/chalicelib/sms.py b/serverless-sms-service/chalicelib/sms.py new file mode 100644 index 0000000000..30afbec60c --- /dev/null +++ b/serverless-sms-service/chalicelib/sms.py @@ -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 diff --git a/serverless-sms-service/requirements-dev.txt b/serverless-sms-service/requirements-dev.txt new file mode 100644 index 0000000000..ef2826cc64 --- /dev/null +++ b/serverless-sms-service/requirements-dev.txt @@ -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 diff --git a/serverless-sms-service/requirements.txt b/serverless-sms-service/requirements.txt new file mode 100644 index 0000000000..b54fcae7cb --- /dev/null +++ b/serverless-sms-service/requirements.txt @@ -0,0 +1 @@ +twilio==6.18.1