
Serverless Framework는 이름처럼 AWS lambda, Azure Function을 비롯한 다양한 Cloud Provider들의 서버리스 런타임 서비스를 쉽게 배포할 수 있도록 도와주는 서비스이다.
Serverless Framework에서는 사용자가 서비스를 구성하는 Function과 Event를 정의하는 방식으로 event-driven 서버리스 아키텍처를 생성/배포하게 된다.
Serverless Framework는 AWS, Azure, GCP등 다양한 클라우드 제공자에 대한 배포를 지원하지만, 이 글에서는 가장 익숙한 AWS를 예시로 하였다.
Function은 그 이름처럼 하나의 작업을 수행하는 코드에 대응되는 개념이다. Serverless Framework의 Function은 프로그래밍 언어로 작성한 함수와 대응 관계에 있다.
Event는 Function을 실행시키는 모든 일련의 사건들을 의미한다. AWS의, AWS Lambda를 실행과 관련된 다양한 이벤트 소스들이 Serverless Framework의 Event에 대응된다고 볼 수 있다.
AWS API Gateway HTTP 엔드포인트 요청이 발생한 경우S3 버킷에 파일이 업로드된 경우SNS Topic에 새로운 메시지가 생성된 경우Resource는 Function에서 사용하기 위해 선언된 AWS의 서비스들에 해당한다.
AWS DynamoDB 테이블AWS S3 버킷AWS SNS 토픽CloudFormation에서 지원하는 모든 리소스// handler.js
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v2.0!',
input: event,
},
null,
2
),
};
};
# serverless.yml
service: myService
provider:
name: aws
runtime: nodejs12.x
memorySize: 512 # optional, in MB, default is 1024
timeout: 10 # optional, in seconds, default is 6
versionFunctions: false # optional, default is true
tracing:
lambda: true # optional, enables tracing for all functions (can be true (true equals 'Active') 'Active' or 'PassThrough')
functions:
hello:
handler: handler.hello # required, handler set in AWS Lambda
name: ${sls:stage}-lambdaName # optional, Deployed Lambda name
description: Description of what the lambda function does # optional, Description to publish to AWS
runtime: nodejs14.x # optional overwrite, default is provider runtime
memorySize: 512 # optional, in MB, default is 1024
timeout: 10 # optional, in seconds, default is 6
provisionedConcurrency: 3 # optional, Count of provisioned lambda instances
reservedConcurrency: 5 # optional, reserved concurrency limit for this function. By default, AWS uses account concurrency limit
tracing: PassThrough # optional, overwrite, can be 'Active' or 'PassThrough'