Serverless
오버 헤드와 비용을 대폭 줄인 서버리스 앱을 구축 할 수 있습니다. 서버리스 애플리케이션을 개발, 배포, 테스트, 보안 및 모니터링 할 수있는 통합 된 환경을 제공합니다.
다양한 클라우드 제공 업체에서 서버리스 앱을 쉽게 개발, 배포 및 테스트 할 수있는 오픈 소스 CLI와 대시 보드로 구성되어 있습니다.

AWS 클라우드를 제공시에는 AWS CloudFormation Template 기반으로 구성됩니다.
커뮤니티가 활성화되어 있어 다양한 sample들이 있습니다.
관련 사이트들은 아래에 있습니다.
- serverless framework : https://serverless.com/
- serverless stack : https://serverless-stack.com/
- serverless vs. the others : https://serverless.com/learn/comparisons/
설치
아래 명령어로 설치해줍니다.
ubuntu:~/environment $ npm install serverless -g
|
워크 플로우
- 프로젝트 생성하기
- 인프라 구성하기
- lambda 구성하기
- 로컬 테스트하기
- 배포하기
프로젝트 생성하기
ubuntu:~/environment $ serverless create --template hello-world --path hello-world
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/home/ubuntu/environment/hello-world"
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v1. 63.0
-------'
Serverless: Successfully generated boilerplate for template: "hello-world"
ubuntu:~/environment $ ls
README.md hello-world
ubuntu:~/environment $ cd hello-world/
ubuntu:~/environment/hello-world $ ls
handler.js serverless.yml
|
인프라 구성하기
serverless.yml 파일을 통해서 인프라를 구성합니다.
현재는 hello-world template 를 이용해 생성된 프로젝트입니다.
helloWorld 람다 함수와 hello-world 경로의 get method 인 API Gateway 가 기본적으로 구성되어 있습니다.
serverless.yml 파일을 확인 합니다.
# Welcome to serverless. Read the docs
# https:
# Serverless.yml is the configuration the CLI
# uses to deploy your code to your provider of choice
# The `service` block is the name of the service
service: hello-world
# The `provider` block defines where your service will be deployed
provider:
name: aws
runtime: nodejs12.x
# The `functions` block defines what code to deploy
functions:
helloWorld:
handler: handler.helloWorld
# The `events` block defines how to trigger the handler.helloWorld code
events:
- http:
path: hello-world
method: get
cors: true
|
lambda 구성하기
serverless.yml 에 fuction 블록에 구성된 부분이 lambda 구성입니다.
handler.js 파일을 확인 합니다.
'use strict' ;
module.exports.helloWorld = (event, context, callback) => {
const response = {
statusCode: 200 ,
headers: {
'Access-Control-Allow-Origin' : '*' ,
},
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!' ,
input: event,
}),
};
callback( null , response);
};
|
로컬 테스트 하기
helloWorld 람다 함수를 실행하여 응답값을 확인해 봅니다.
ubuntu:~/environment/hello-world $ serverless invoke local --function helloWorld
{
"statusCode" : 200 ,
"headers" : {
"Access-Control-Allow-Origin" : "*"
},
"body" : "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":\"\"}"
}
ubuntu:~/environment/hello-world $
|
배포하기
자신의 AWS Account 계정에 배포합니다.
AWS CLI 에 설정해 둔 Access key ID 와 Secret access key 가 사용됩니다.
$ serverless deploy -v
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service hello-world.zip file to S3 ( 404 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................................
Serverless: Stack update finished...
Service Information
service: hello-world
stage: dev
region: us-east- 1
stack: hello-world-dev
resources: 12
api keys:
None
endpoints:
GET - https:
functions:
helloWorld: my-project-dev-helloWorld
layers:
None
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.
|
댓글
댓글 쓰기