# AWS

Amazon Web Services is one possible way to host your Node.js application.

This tutorial shows you how to configure the Express application written with Ts.ED, to be executed as an AWS Lambda Function.

More information here: Official AWS Docs (opens new window)

# Installation

First, install the tsed/platform-aws module:

npm install --save @tsed/platform-aws
1

# Configuration

Create a new LambdaServer.ts in src directory:

import {PlatformAws} from "@tsed/platform-aws";
import "@tsed/platform-express";
import {Server} from "./Server";

// or import "@tsed/platform-koa";

PlatformAws.bootstrap(Server, {
  aws: {
    binaryMimeTypes: [ // optional
      // mime types list
    ]
  },
  // additional Ts.ED options. See https://tsed.io/tutorials/aws.html
});

// Handler used by AWS
export const handler = PlatformAws.callback();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Then create lambda.js on your root project:

module.exports = require("./dist/LambdaServer.js");
1

This file will be used by AWS to forward request to your application.

Finally, package and create your Lambda function (opens new window), then configure a simple proxy API using Amazon API Gateway and integrate it with your Lambda function.

See more details on aws-serveless-express (opens new window) project.

# Getting the API Gateway event object

This package includes decorators to easily get the event object Lambda received from API Gateway:

import {Controller, Get} from "@tsed/common"; 
import {AwsEvent, AwsContext} from "@tsed/platform-aws"; 

@Controller('/')
class MyCtrl {
 @Get('/')
 get(@AwsEvent() event: any, @AwsContext() context: any) {
   console.log("Event", apiGateway.event);
   console.log("Context", apiGateway.context);
   
   return apiGateway;
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

TIP

You can find a project example with AWS configuration here (opens new window).

Example

You can see an example provided by the AWS Team on this github repository (opens new window).

# Author

    # Maintainers Help wanted

      Last Updated: 12/4/2020, 12:31:38 PM

      Other topics