Serverless 적용하기

Taro·2023년 11월 19일




Serverless 설치하기

 $ npm install -g serverless

설치 후 버전 확인

 $ sls -version

 

Serverless 로그인

 $ sls login
  • 위 명령어를 통해 로그인 할 수 있다

  • Serverless 플랫폼에 로그인을 하고 나중에 배포를 하게 된다면, 그동안 배포한 함수와 함수들을 위한 문서들도 확인가능


AWS credentials 설정

$ serverless config credentials --provider aws --key 액세스키ID --secret 비밀액세스키
Serverless: Setting up AWS...
Serverless: Saving your AWS profile in "~/.aws/credentials"...
Serverless: Success! Your AWS access keys were stored under the "default" profile.

 

Serverless 템플릿 사용하여 APP 생성

$ sls create -t
  • 위 명령어 실행시 사용가능한 템플릿 목록이 나타난다
Serverless: Generating boilerplate...

  Serverless Error ---------------------------------------

  Template "true" is not supported. Supported templates are: "aws-nodejs", "aws-nodejs-typescript", "aws-nodejs-ecma-script", "aws-python", "aws-python3", "aws-groovy-gradle", "aws-java-maven", "aws-java-gradle", "aws-kotlin-jvm-maven", "aws-kotlin-jvm-gradle", "aws-kotlin-nodejs-gradle", "aws-scala-sbt", "aws-csharp", "aws-fsharp", "aws-go", "aws-go-dep", "azure-nodejs", "google-nodejs", "kubeless-python", "kubeless-nodejs", "openwhisk-nodejs", "openwhisk-php", "openwhisk-python", "openwhisk-swift", "spotinst-nodejs", "spotinst-python", "spotinst-ruby", "spotinst-java8", "webtasks-nodejs", "plugin" and "hello-world".

aws_nodejs 사용할 경우

$ sls create -t aws-nodejs -p 프로젝트명이름
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/Users/example/test"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.26.0
 -------'

Serverless: Successfully generated boilerplate for template: "aws-nodejs"
  • 이후 test 경로에 handler.js 파일과 serverless.yaml 파일이 생성된다.

 

함수 호출하기

  • invoke 를 사용하여 serverless 를 통하여 함수 작용유무를 확인가능
$ serverless invoke local --function hello
{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":\"\"}"
}

Serverless.yml 파일 수정하기

region과 stage를 설정한다. region의 경우엔 AWS에서 배포할 지역을 설정한다. 기본적으로는 미국으로 설정되니, 이 값을 ap-northeast-2 로 입력하여 한국으로 변경한다. stage 는 현재 애플리케이션의 배포 상태를 의미하고 prod 로 하거나, dev로 설정할 수 있다.

  • hello 함수에서 events 값을 추가하여 API Gatway 와 연결하겠다. 코드 몇 줄이면 함수에 API를 달아줄 수 있다.
service: hello-serverless

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: ap-northeast-2

functions:
  hello:
    handler: handler.hello
    events: 
      - http:
          path: hello
          method: get

 

배포하기

함수 배포시에는 sls deploy 명령어를 입력

$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (409 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..........
Serverless: Stack update finished...
Service Information
service: hello-serverless
stage: dev
region: ap-northeast-2
stack: hello-serverless-dev
api keys:
  None
endpoints:
  GET - https://4mmtbsr9eg.execute-api.ap-northeast-2.amazonaws.com/dev/hello
functions:
  hello: hello-serverless-dev-hello
Serverless: Publish service to Serverless Platform...
Service successfully published! Your service details are available at:
https://platform.serverless.com/services/velopert/hello-serverless

 

Aws Lambda 에서 확인

AWS Lambda → 애플리케이션 에서 배포 상태를 확인가능

profile
기록하며 공부하는곳

0개의 댓글