Here's the step-by-step guide on how to build & deploy AWS Lambda functions using Serverless library.
This guide was inspired by this tutorial.
I prefer to use Node.js & ideally develop using Typescript. In addition, this has support for running locally, using environment variables through Secrets Manager, etc.
-
Install serverless globally
npm install -g serverless
-
Create a IAM User in your AWS Console.
- Create a new user w/ programattic access
- Give the user only the required permissions (Admin by default). More info here
- Save the
Access key ID
&Secret access key
for that user
-
Enter IAM keys in the Serverless configuration
serverless config credentials --provider aws --key xxxxxxxxxxxxxx --secret xxxxxxxxxxxxxx
-
Initialize Serverless project
mkdir dear-earth-serverless serverless create --template aws-nodejs
-
Check
serverless.yml
file and ensure it hasevents
forhttpApi
&schedule
.More info on Node Scheduled Cron here.
functions: hello: handler: handler.hello events: - httpApi: path: /hello method: get - schedule: rate(2 minutes) environment: variable2: value2
-
Commit your code w/
init serverless project
-
Add the Serverless Offline npm package
npm init npm install serverless-offline --save-dev
-
Add the Serverless Offline plugin to
serverless.yml
file# emulate AWS λ and API Gateway locally when developing your Serverless project plugins: - serverless-offline
-
Run the serverless functions locally
serverless offline start
-
Commit your code w/
setup serverless-offline
-
Deploy the serverless functions to AWS
export AWS_ACCESS_KEY_ID=XXXXXXXXX export AWS_SECRET_ACCESS_KEY=XXXXXXXXX # AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are now available for serverless to use serverless deploy --verbose
-
Run the deployed function on AWS with logs
serverless invoke -f hello --log
-
Add the Serverless Plugin Typescript npm package
npm install -D serverless-plugin-typescript typescript
-
Add the Serverless Typescript plugin to
serverless.yml
file and make sure to include it before the offline pluginplugins: # Add typescript support to local development & deployed files - serverless-plugin-typescript # emulate AWS λ and API Gateway locally when developing your Serverless project - serverless-offline
-
Environment variables w/ dotenv are natively supported in Serverless.
More info on Referencing Environment Variables
# Serverless will load all env vars in `.env` in 3.0.0+ # https://www.serverless.com/framework/docs/environment-variables/ useDotenv: true
-
Add a
.env
file with environment variables. Make sure to add it to.gitignore
as wellNODE_ENV=XXXXXXXX
-
Once you update the
.env
file with real values make sure to mark that file as unchanged so that it doesn't show up in diffsgit update-index --assume-unchanged .env
-
Include the environment variables in the
serverless.yml
filefunctions: hello: handler: handler.hello environment: NODE_ENV: ${env:NODE_ENV}
-
Import
dotenv
config at the root of each serverless functionimport 'dotenv/config';
-
The store sensitive secrets on AWS, you can use the Secrets Manager.
More info on Tutorial: Create and retrieve a secret
NODE_ENV=production
-
Then update the
serverless.yml
with a custom handlerNotice below, the
dear-earth-serverless
is the name of the secrets folder you create.custom: secrets: ${ssm:/aws/reference/secretsmanager/dear-earth-serverless}
-
Lastly update the
environment
section inserverless.yml
to get the value from the custom handlerfunctions: hello: handler: handler.hello environment: NODE_ENV: ${self:custom.secrets.NODE_ENV}