Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an UI for clients with Swagger using SAM (Serverless Application Model) #10

Open
alcance opened this issue Sep 21, 2021 · 0 comments
Assignees
Labels
feature help wanted Extra attention is needed

Comments

@alcance
Copy link

alcance commented Sep 21, 2021

Overview

This is not a problem, is a requirement. There has been some issues with this on having a NestJS application running properly in SAM.

We haven't been able to successfully deploy it on AWS but more people has faced this problem and seems that found a solution.

Description

Looks like the root problem comes from src/main.ts where bootstrapping is being made.

const app = await NestFactory.create(AppModule);

I think SAM cannot trigger the lambda function using this method and have. An alternative is using createApplicationContext for Standalone applications.

In the other hand i don't see this being used in swagger-ui issue mentioned above.

I not sure about this but in our previous implementation i don't remember having a serverless.yaml nor lambda.ts files. Maybe was not committed (?).

Possible implementation

This is from previous implementation, is for illustrative reasons and is not meant to be working.

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { createServer, proxy } from 'aws-serverless-express';
import { eventContext } from 'aws-serverless-express/middleware';
import { Handler, Context } from 'aws-lambda';
import express from 'express';
import { Server } from 'http';
import { AppModule } from './app.module';

const binaryMimeTypes: string[] = [];
let cachedServer: Server;

process.on('unhandledRejection', (reason) => {
  console.log(reason);
});

process.on('uncaughtException', (reason) => {
  console.log(reason);
});

/**
 * 
 * @param {INestApplication} app
 */
const setupSwagger = (app: INestApplication) => {
  const config = new DocumentBuilder()
    .setTitle('Jet Premium Finance API')
    .setVersion('1.0')
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup(
    'api',
    app,
    document,
    {
      swaggerOptions: {
        filter: true
      }
    }
  );
}

const bootstrapServer = async (): Promise<Server> => {
  if (!cachedServer) {
    try {
      const expressApp = express();
      const nestApp = await NestFactory.create(AppModule, new ExpressAdapter(expressApp));
      nestApp.use(eventContext());

      setupSwagger(nestApp);
      cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
    } catch (error) {
      return error;
    }
  }

  return cachedServer;
}

export const handler: Handler = async(event: any, context: Context) => {
  if (event.path === '/api') {
    event.path = '/api/';
  }

  event.path = event.path.includes('swagger-ui') ? `api${event.path}` : event.path;

  cachedServer = await bootstrapServer();
  return proxy(cachedServer, event, context, 'PROMISE').promise;
}

Additional considerations

Beware of possible errors. Refer to Clubhouse ticket for more details.

This is indeed not working as expected and i shouldn't pushed it to main without testing it from it's own branch. 😿

curl https://2sjy3m1ru6.execute-api.us-east-2.amazonaws.com/Prod/

{"message": "Internal server error"}% 

I will proceed to revert main branch to a working state.

  • I am attaching logs from CloudWatch where it seems to not being able to run the application. This can be related to our prior discussion of not using createApplicationContext and work as an stand alone app:

More info here: https://docs.nestjs.com/standalone-applications

  • Is valid SAM template
021-08-23 20:13:25 Loading policies from IAM...
2021-08-23 20:13:26 Finished loading policies from IAM.

Screen Shot 2021-08-23 at 20 29 46

Logs

Download log files

[$LATEST]5d1efa5e3b8742ccabb934d356ed0aba:us-east-2

TL;DR for logs:

2021-08-23T20:02:47.460-07:00	REPORT RequestId: 8cd898b0-8854-4ac9-9abe-d7559073089f	Duration: 11694.45 ms	Billed Duration: 11695 ms	Memory Size: 128 MB	Max Memory Used: 52 MB	
2021-08-23T20:02:47.460-07:00	Unknown application error occurred
                             	Runtime.HandlerNotFound
@alcance alcance added help wanted Extra attention is needed feature labels Sep 21, 2021
@alcance alcance self-assigned this Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant