This project has been generated using the aws-nodejs-typescript
template from the Serverless framework.
For detailed instructions, please refer to the documentation.
- Install Serverless using the command:
npm install serverless -g
- Configure the AWS credentials in case of AWS provider with the command bellow:
serverless config credentials -o --provider aws --key <KEYHERE> --secret <SECRETHERE>
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 two lambda functions triggered by an HTTP request made on the provisioned API Gateway REST API.
- sending a
POST
request to/trace
with a payload containing some string properties ofsrc/functions/trace/schema.ts
will result in API Gateway returning a200
HTTP status code.
- sending a
GET
request to/tracker
with the params ofsrc/functions/tracker/schema.ts
will result in API Gateway returning a301
HTTP status code in order to redirect to the url from the param.
requesting any other path than both above will result in API Gateway returning a 403
HTTP error code
In order to test the hello function locally, run the following command:
npx sls invoke local -f trace --path src/functions/trace/mock.json
if you're testing the trace function ornpm run tracetest
npx sls invoke local -f tracker --path src/functions/tracker/mock.json
if you're testing the tracker function ornpm run trackertest
Check the sls invoke local command documentation for more information.
Copy and replace your url
- found in Serverless deploy
command output - and all parameters in the following curl
command in your terminal or in Postman to test your newly deployed application.
curl --location --request POST 'https://myApiEndpoint/dev/trace' \
--header 'Content-Type: application/json' \
--data-raw '{
"customer_id": "11111",
"destination": "LP",
"origin_type": "phone",
"origin_value": "5511998989898",
"action_type": "CLICK",
}'
Copy and past the url below in your browser:
https://grt08tfpmb.execute-api.us-east-2.amazonaws.com/dev/tracker?url=http://www.google.com.br&origin_type=LP&origin_value=novalue&id=1111111
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
│ │ ├── trace
│ │ │ ├── handler.ts # `trace` lambda source code
│ │ │ ├── index.ts # `trace` lambda Serverless configuration
│ │ │ ├── mock.json # `trace` lambda input parameter, if any, for local invocation
│ │ │ └── schema.ts # `trace` lambda input event JSON-Schema
│ │ │
│ │ ├── tracker
│ │ │ ├── handler.ts # `tracker` lambda source code
│ │ │ ├── index.ts # `tracker` lambda Serverless configuration
│ │ │ ├── mock.json # `tracker` lambda input parameter, if any, for local invocation
│ │ │ └── schema.ts # `tracker` 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 - serverless-mysql - mysql management
- @types/mysql - This package contains the mysql driver for typescript