如何在Localstack中部署搭载GraphQL的NestJS Serverless Lambda服务
解决方案
我已经找到了解决方案,感谢大家的时间。StackOverflow不允许我自行回答问题 >:(
问题描述
我想了解如何配置才能将搭载GraphQL而非RestApi的NestJS应用部署为Localstack的Lambda服务,以下是我目前的配置:
- 当前配置是否正确?
- 如何获取模拟访问链接,通过Localstack Lambda访问GraphQL Playground?
实现方案
NestJS Lambda函数项目配置
import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { ExpressAdapter } from '@nestjs/platform-express'; import serverlessExpress from '@vendia/serverless-express'; import { APIGatewayProxyHandler, Handler } from 'aws-lambda'; import express from 'express'; import { AppModule } from './app.module'; let cachedServer: Handler; const bootstrapServer = async (): Promise<Handler> => { const expressApp = express(); const app = await NestFactory.create( AppModule, new ExpressAdapter(expressApp), ); app.useGlobalPipes(new ValidationPipe()); app.enableCors(); await app.init(); return serverlessExpress({ app: expressApp, }); }; export const handler: APIGatewayProxyHandler = async ( event, context, callback, ) => { if (!cachedServer) { cachedServer = await bootstrapServer(); } return cachedServer(event, context, callback); };
Serverless.yml 配置
service: test provider: name: aws runtime: nodejs14.x stage: '' # profile: local # 配置你的AWS Profile environment: # 服务全局环境变量 NODE_ENV: local GLOBAL_PREFIX: graphql PORT: 4000 plugins: # - serverless-plugin-typescript # - serverless-plugin-optimize # - serverless-offline - serverless-localstack custom: localstack: debug: true stages: - local - dev endpointFile: localstack_endpoints.json individually: true # serverless-offline: # httpPort: 3000 functions: main: handler: dis/index.handler # local: # handler: dist/main.handler # events: # - http: # path: / # method: any # cors: true
Docker Compose Localstack 配置
version: '3.8' services: localstack: image: localstack/localstack:latest environment: - AWS_DEFAULT_REGION=us-east-1 - EDGE_PORT=4566 - SERVICES=lambda,s3,cloudformation,sts ports: - '4566-4597:4566-4597' volumes: - "${TMPDIR:-/tmp}/localstack:/tmp/localstack" - "/var/run/docker.sock:/var/run/docker.sock"
服务端点配置
{ "CloudFormation" : "http://localhost:4566", "CloudWatch" : "http://localhost:4566", "Lambda" : "http://localhost:4566", "S3" : "http://localhost:4566" }
执行命令
执行
serverless deploy --stage local命令时出现报错
执行serverless info --stage local命令时返回对应信息
执行serverless invoke local -f main -l命令时返回对应结果
内容的提问来源于stack exchange,提问作者user9213248
相关产品推荐
相关产品推荐

