AWS

Serverless Framework 도입기

ZzangHo 2023. 6. 2. 16:51
728x90

회사에서 AWS Cloud 전환 작업 중 Lambda로 처리하던 부분들이 있었는데 클라우드팀에서 Lambda 대신 Serverless Framework를  쓰는게 어떻냐고 제안해주셨다. 

 

그때부터 열심히 Serverless Framework를 찾아보았다.

 



 

Serverless Framework란

AWS의 CloudFormation을 이용하여 서버리스 서비스를 배포하는 방식이다.

AWS, Azure, GCP 등의 클라우드 환경에서 서버리스 서비스를 쉽게 사용할 수 있도록 도와주는 오픈소스 프레임워크이다.

 

장점

인프라를 코드로 관리 및 배포하기 때문에 소스의 형상관리 및 유지보수가 용이하다.

(기존 Lambda의 경우 콘솔 환경에서 직접 업로드를 하는 방식이였기 때문에 관리 측면에서 다소 불편한점이 있었다.)

 

Serverless Framework 설치

아래 페이지에 설치하는 방법이 잘 설명되어 있다.(그럼에도 설치 당시 문제가 많았다 ㅜㅜ)

https://www.serverless.com/framework/docs/getting-started#getting-started

 

Setting Up Serverless Framework With AWS

The Serverless Framework documentation for AWS Lambda, API Gateway, EventBridge, DynamoDB and much more.

www.serverless.com

 

기본적으로 아래 Tool들이 필요하다.

  • Nodejs(v16.20.0)
  • npm(9.6.7)

npm은 Node를 설치하면 자동으로 설치가 됨으로 실제적으로는 Nodejs가 설치되어 있어야 한다.

 

serverless 설치

serverless 라이브러리를 npm을 통해 설치를 하게 되는데 이때 버전문제가 있을 수 있으므로 당황하지 말고 에러메세지의 내용을 보면서 버전을 올리면 된다.
npm install -g serverless

 

자격 증명

serverless를 이용해 배포를 하기 위해서는 AWS 자격 증명이 필요하다.

기본적으로는 AWS AccessKey를 이용하도록 가이드되어 있지만 AccessKey는 보안적으로 매우 위험하여 필자의 경우 EC2에 정책을 연결하여 사용하였다.

 

템플릿을 이용하여 APP 생성

필자의 경우 Python으로 기본적인 Hello-world 프로젝트를 생성해보았다.
[root@ipserverless]# serverless

Creating a new serverless project

? What do you want to make? AWS - Python - Starter
? What do you want to call this project? hello-world

✔ Project successfully created in hello-world folder

? Do you want to login/register to Serverless Dashboard? No

? No AWS credentials found, what credentials do you want to use? Skip

You can setup your AWS account later. More details available here: http://slss.io/aws-creds-setup

 

그럼 기본적으로 아래와 같은 파일들이 생긴다.

 

[root@ip hello-world]# ls
README.md  handler.py  serverless.yml
  • handler.py : Lambda 함수에서 실행할 파일
  • serverless.yml : CloudFomation이 사용할 파일이라고 생각하면 된다. 이 파일에는 런타임, 공급자, 인프라, Region 등 다양한 정보들이 들어가며 이 파일을 이용해 인프라를 생성한다.

 

로컬 테스트

클라우드에 배포하기 전 잘 동작하는지 테스트 해볼 수 있다. (serverless = sls)
[root@ip hello-world]# sls invoke local -f hello
{
    "statusCode": 200,
    "body": "{\"message\": \"Go Serverless v3.0! Your function executed successfully!\", \"input\": {}}"
}

 

배포

[root@ip hello-world]# sls deploy --region ap-northeast-2

Deploying hello-world to stage dev (ap-northeast-2)

✔ Service deployed to stack hello-world-dev (64s)

functions:
  hello: hello-world-dev-hello (2.1 kB)

Need a faster logging experience than CloudWatch? Try our Dev Mode in Console: run "serverless dev"

 

위와 같이 배포하게 되면 클라우드 환경에 정상적으로 Lambda함수가 생성이 된다.

hello-world

 

테스트

배포를 했으니 테스트를 해보자
[root@ip hello-world]# sls invoke -f hello
{
    "statusCode": 200,
    "body": "{\"message\": \"Go Serverless v3.0! Your function executed successfully!\", \"input\": {}}"
}

 

주의사항은 기본적으로 리전이 us-east-1로 잡혀있으니 주의 하자

서울리전의 경우 아래와 같이 provider.region: northeast-2 로 설정해주면 된다.

service: hello-world

frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.9
  region: northeast-2

functions:
  hello:
    handler: handler.hello

 

트러블 슈팅

  • 버전 이슈
[root@ip ~]# npm install -g serverless
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated superagent@7.1.6: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731)
npm notice
npm notice New major version of npm available! 8.19.4 -> 9.6.7
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.7
npm notice Run npm install -g npm@9.6.7 to update!
npm notice
npm ERR! code 127
npm ERR! path /root/.nvm/versions/node/v16.20.0/lib/node_modules/serverless
npm ERR! command failed
npm ERR! command sh -c -- node ./scripts/postinstall.js
npm ERR! sh: node: command not found

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2023-06-02T06_48_06_751Z-debug-0.log
[root@ip ~]# npm install -g npm@9.6.7

removed 13 packages, changed 101 packages, and audited 264 packages in 2s

27 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

 

'AWS' 카테고리의 다른 글

EC2 사용자 데이터(User Data)  (0) 2023.05.23