This example exposes an AWS Lambda function that sends an SMS using Twilio when it receives a POST request.
We use Serverless Framework to do production ready deployments and local development using serverless-offline.
-
Sign up for a Twilio account
-
Create a new phone number in your Twilio trial account
To install this example to bootstrap your project, run the following command:
npx serverless install -u https://github.com/nanlabs/devops-reference/tree/main/examples/serverless-twilio-aws-lambdas-typescript -n my-project
You’ll need to have Node 16.13.2 or later on your local development machine (but it’s not required on the server). You can use fnm to easily switch Node versions between different projects.
fnm use
npm install
This repo has a local development set up that uses the file .env.local
to configure the local environment.
Grab your ACCOUNT SID
and AUTH TOKEN
from the Twilio console
Run the following command to start the local development server:
npm run sls:offline
It will start the following services:
- AWS Lambda at
http://localhost:3000/send-twilio-sms
You can then send a request using curl
curl -X POST \
--url http://localhost:3000/send-twilio-sms \
--header 'Content-Type: application/json' \
--data '{"toNumber": "+541153535353", "messageBody": "This message comes from an AWS Lambda"}'
To deploy the app to AWS, you'll first need to configure your AWS credentials. There are many ways to set your credentials, for more information refer to the AWS documentation.
Once set you can deploy your app using the serverless framework with:
npm run sls:deploy
We recommend the following resources to add local development tools to your project: