This project has been generated using the aws-nodejs-typescript
template from the Serverless framework.
For detailed instructions, please refer to the documentation.
Depending on your preferred package manager, follow the instructions below to deploy your project.
Requirements: NodeJS
lts/fermium (v.14.15.0)
. If you're using nvm, runnvm use
to ensure you're using the same Node version in local and in your lambda's runtime.
- Run
npm i
to install the project dependencies - Run
npx sls deploy
to deploy this stack to AWS
- Run
yarn
to install the project dependencies - Run
yarn sls deploy
to deploy this stack to AWS
This template contains a single lambda function triggered by an HTTP request made on the provisioned API Gateway REST API /hello
route with POST
method. The request body must be provided as application/json
. The body structure is tested by API Gateway against src/functions/hello/schema.ts
JSON-Schema definition: it must contain the name
property.
- requesting any other path than
/hello
with any other method thanPOST
will result in API Gateway returning a403
HTTP error code - sending a
POST
request to/hello
with a payload not containing a string property namedname
will result in API Gateway returning a400
HTTP error code - sending a
POST
request to/hello
with a payload containing a string property namedname
will result in API Gateway returning a200
HTTP status code with a message saluting the provided name and the detailed event processed by the lambda
⚠️ As is, this template, once deployed, opens a public endpoint within your AWS account resources. Anybody with the URL can actively execute the API Gateway endpoint and the corresponding lambda. You should protect this endpoint with the authentication method of your choice.
In order to test the hello function locally, run the following command:
npx sls invoke local -f hello --stage dev --path src/functions/hello/mock.json
if you're using NPMyarn sls invoke local -f hello --path src/functions/hello/mock.json
if you're using Yarn
Check the sls invoke local command documentation for more information.
Copy and replace your url
- found in Serverless deploy
command output - and name
parameter in the following curl
command in your terminal or in Postman to test your newly deployed application.
curl --location --request POST 'https://myApiEndpoint/dev/hello' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Frederic"
}'
To run serverless-offline plugin run the following command:
npx serverless offline --noAuth --stage dev
Ensure that you have the correct Lambda functions defined and added in your serverless.ts file for them to show up. You can test the endpoints using Postman.
AWS CLI is required to run this.
Linux:
- To install the AWS CLI, run the following commands:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Mac:
-
In your browser, download the macOS pkg file: Here
-
Run your downloaded file and follow the on-screen instructions.
Windows:
- Download and run the AWS CLI MSI installer for Windows (64-bit): Here
After install is completed you need to configure it as follows: run command:
aws configure
- AWS Access Key ID: example (it can be whatever, you are running this locally)
- AWS Secret Access Key: example (same as above)
- Default region name: localhost
- Default output format: (this can be empty)
DynamoDB Local:
-
To set up DynamoDB on your computer Download DynamoDB local Here
-
After you download the archive, extract the contents and copy the extracted directory to a location of your choice.
-
There are other options for storing the table Here
-
You can also use DynamoDB local as Docker Image.
To start DynamoDB on your computer, open a command prompt window, navigate to the directory where you extracted DynamoDBLocal.jar, and enter the following command:
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
Set AttributeType accordingly based on the type of the id.
- "AttributeName=id,AttributeType=S "
- AND IN serverless.yml "AttributeType: S"
Create a new table:
aws dynamodb create-table \
--table-name MyDynamoDbTable \
--attribute-definitions \
AttributeName=id,AttributeType=S \
--key-schema \
AttributeName=id,KeyType=HASH \
--provisioned-throughput \
ReadCapacityUnits=1,WriteCapacityUnits=1 \
--endpoint-url http://localhost:8000
To check current tables:
aws dynamodb list-tables --endpoint-url http://localhost:8000
Start serverless offline:
npx sls offline --stage local --noAuth start
The project code base is mainly located within the src
folder. This folder is divided in:
functions
- containing code base and configuration for your lambda functionslibs
- containing shared code base between your lambdas
.
├── src
│ ├── functions # Lambda configuration and source code folder
│ │ ├── hello
│ │ │ ├── handler.ts # `Hello` lambda source code
│ │ │ ├── index.ts # `Hello` lambda Serverless configuration
│ │ │ ├── mock.json # `Hello` lambda input parameter, if any, for local invocation
│ │ │ └── schema.ts # `Hello` lambda input event JSON-Schema
│ │ │
│ │ └── index.ts # Import/export of all lambda configurations
│ │
│ └── libs # Lambda shared code
│ └── apiGateway.ts # API Gateway specific helpers
│ └── handlerResolver.ts # Sharable library for resolving lambda handlers
│ └── lambda.ts # Lambda middleware
│
├── package.json
├── serverless.ts # Serverless service file
├── tsconfig.json # Typescript compiler configuration
├── tsconfig.paths.json # Typescript paths
└── webpack.config.js # Webpack configuration
- json-schema-to-ts - uses JSON-Schema definitions used by API Gateway for HTTP request validation to statically generate TypeScript types in your lambda's handler code base
- middy - middleware engine for Node.Js lambda. This template uses http-json-body-parser to convert API Gateway
event.body
property, originally passed as a stringified JSON, to its corresponding parsed object - @serverless/typescript - provides up-to-date TypeScript definitions for your
serverless.ts
service file
Any tsconfig.json can be used, but if you do, set the environment variable TS_NODE_CONFIG
for building the application, eg TS_NODE_CONFIG=./tsconfig.app.json npx serverless webpack
Deployment bucket name opt.stage should be included with the stage flag when using sls commands e.g. --stage dev.
Staging implementation for selecting slack user ids (through an environment variable) who will receive all the messages sent by daily or weekly messages.
For use in staging environment/ develop branch use the SLACK_USER_OVERRIDE environment variable. If this variable does not exist or is empty the program runs normally. Copy the variable and add it (SLACK_USER_OVERRIDE=<slackid>
) to the .env
file.
Multiple ids must be divided by ,
for example SLACK_USER_OVERRIDE=xxxxxxx,xxxxxxx,xxxxxxx
- Click on a user profile within Slack.
- Click on "View full profile" in the menu that appears.
- Click the ellipses (three dots).
- Click on
Copy Member ID
.