- serverless application 을 손쉽게 배포 할 수 있게 하는 프레임워크
- 링크
-
npm run build
를 이용하여 배포 파일을 생성한다.
- 개발용 배포를 위해서는
sls deploy -s dev --alias [여러분의 github id]. 모두 소문자로 작성하셔야합니다. ex) atercatus
- 서버용 배포를 위해서는
sls deploy -s prod --retain
retain은 버전 유지를 의미합니다.
service: wedev-api # service name. Lambda 이름 제일 앞에 위치
custom:
STAGE: ${self:provider.stage, 'dev'}
CONFIG: ${file(./config/config.js):CONFIG.${self:custom.STAGE}}
provider:
name: aws
runtime: nodejs10.x
region: ap-northeast-1
profile: default
stage: ${opt:stage, 'dev'} # API Gateway stage // 우선 자신의 github id로 작성
apiName: ${self:custom.CONFIG.API_NAME} # API Gateway name
plugins:
- serverless-offline
- serverless-aws-alias
package:
exclude:
- .git/**
- src/**
- test/**
- e2e/**
- README.md
functions:
index: # Lambda 이름 마지막에 추가
name: ${self:custom.CONFIG.API_NAME}
handler: dist/src/index.handler
vpc:
securityGroupIds:
- sg-00716c37241964512
subnetIds:
- subnet-018f7fd8aa3ae2c39
- subnet-0479aca67cf6ca711
- subnet-0ef00aa4fb1674c5b
# - subnet-0d58d96b6c7495387
events:
- http:
cors: true
path: '/'
method: any
- http:
cors: true
path: '{proxy+}'
method: any
retain: false
# npm run build
# sls deploy --alias {github id}
module.exports.CONFIG = serverless => ({
dev: {
API_NAME: 'wedev-api-dev',
},
prod: {
API_NAME: 'wedev-api-prod',
},
});
import { Context } from 'aws-lambda';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Server } from 'http';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as serverless from 'aws-serverless-express';
import * as express from 'express';
import * as helmet from 'helmet';
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
let cachedServer: Server;
async function bootstrapServer(): Promise<Server> {
const expressApp = express();
const adapter = new ExpressAdapter(expressApp);
return NestFactory.create(AppModule, adapter)
.then(app => {
app.enableCors({
origin: process.env.CORS_ALLOWED_ORIGINS.split(','),
credentials: true,
preflightContinue: false,
optionsSuccessStatus: 204,
});
app.use(helmet());
app.use(bodyParser.text());
app.use(cookieParser());
return app;
})
.then(app => app.init())
.then(() => serverless.createServer(expressApp));
}
// tslint:disable-next-line: no-any
export const handler = (event: any, context: Context) => {
if (!cachedServer) {
bootstrapServer().then(server => {
cachedServer = server;
return serverless.proxy(server, event, context);
});
} else {
return serverless.proxy(cachedServer, event, context);
}
};