atlas-cli / nestjs-boilerplate

NestJs Modular Monolith with AWS Lambda and CDK
15 stars 5 forks source link

What are the basic steps to run the application on AWS? #24

Open diegosoaresub opened 2 months ago

diegosoaresub commented 2 months ago

Hello,

Thank you for providing such a useful and powerful boilerplate code to the community.

As a newcomer to NodeJS development, I’ve encountered some challenges while setting up the application on AWS. I successfully deployed the application and accessed the APIs via Swagger. However, I encountered an error: EntityMetadataNotFoundError: No metadata for "User" was found.

I believe this issue stems from not running the migration lambda successfully. When I trigger the lambda via the AWS Console, I receive the following exception:

{
    "errorType": "TypeError",
    "errorMessage": "e is not a constructor",
    "stack": [
        "TypeError: e is not a constructor",
        "    at Runtime.handler (/var/task/index.js:1613:15)",
        "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
    ]
}

Could you please provide the basic steps to ensure the application is fully functional once deployed on AWS?

Thanks

douglasgc commented 1 month ago

This seems like a problem when loading typeorm entities, you could do a test updating the typeorm configuration and see if this solves the migrations or EntityMetadataNotFoundError problem.

entities: [__dirname + '/../*/.entity.{js,ts}']

in the archives

src/common/database/lambda-cli.data-source.ts src/common/database/local-cli.data-source.ts src/common/database/typeorm-config.service.ts

I will try to reproduce it here as well

douglasgc commented 1 month ago

I submitted corrections to master, you can pull and try

diegosoaresub commented 1 month ago

Thanks for the quick help! I'll try it later today. How are we supposed to run the migration lambda?

diegosoaresub commented 1 month ago

Unfortunately, I was still not able to run the migration due to multiple errors:

$LAMBDA_NAME is not being retrieved from env, so I had to run the replace it with the actual lambda name:

npx typeorm-lambda-proxy atlas-development-migration  node ./node_modules/typeorm/cli.js --dataSource=dist/app/common/database/lambda-cli.data-source.js

The above command threw AWS credentials error. I had to add the parameters AWS_REGION and AWS_PROFILE to .env. After that, I got the following error:

Not enough non-option arguments: got 0, need at least 1

    at genericNodeError (node:internal/errors:984:15)
    at wrappedFn (node:internal/errors:538:14)
    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:519:28)
    at maybeClose (node:internal/child_process:1105:16)
    at ChildProcess._handle.onexit (node:internal/child_process:305:5) {

In face of this error, I added the option migration:run to the end of the command:

 npx typeorm-lambda-proxy atlas-development-migration  node ./node_modules/typeorm/cli.js --dataSource=dist/app/common/database/lambda-cli.data-source.js migration:run

The command resulted in IAM authentication failure:

 START RequestId: 7bfa202f-1817-4609-bac9-49428a6a659e Version: $LATEST
2024-07-16T00:19:51.081Z        7bfa202f-1817-4609-bac9-49428a6a659e    ERROR   Error: Command failed: node ./node_modules/typeorm/cli.js --dataSource=dist/app/common/database/lambda-cli.data-source.js migration:run
error: The IAM authentication failed for the role postgres. Check the IAM token for this role and try again.
    at Parser.parseErrorMessage (/var/task/node_modules/pg-protocol/dist/parser.js:283:98)
    at Parser.handlePacket (/var/task/node_modules/pg-protocol/dist/parser.js:122:29)
    at Parser.parse (/var/task/node_modules/pg-protocol/dist/parser.js:35:38)
    at TLSSocket.<anonymous> (/var/task/node_modules/pg-protocol/dist/index.js:11:42)
    at TLSSocket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:191:23) {
  length: 129,
  severity: 'FATAL',
  code: '28P01',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: undefined,
  line: undefined,
  routine: undefined
}

    at genericNodeError (node:internal/errors:984:15)
    at wrappedFn (node:internal/errors:538:14)
    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:519:28)
    at maybeClose (node:internal/child_process:1105:16)
    at ChildProcess._handle.onexit (node:internal/child_process:305:5) {

Were you able to run the migration command in your AWS account? Did you destroy the stack and recreate it via CDK? I wonder if some permission is missing. I'll keep investigating it.

diegosoaresub commented 1 month ago

Looks like we are trying to use the static password available in .env file (see code) instead of pulling the token from AWS Signer to run the migrations. Can you please confirm if you were able to run the migration in your testing environment.

Unfortunately, I didn't find any good examples on how to enable AWS Signer for TypeORM + PostgreSQL migrations.