Amazon Web Services

AWS SAM이란

ssnowball·2020년 6월 16일·조회 8,116

1. 개요

AWS SAM에 대한 특징과 함께 사용법, 장단점에 대해 소개한다.

2. AWS SAM이란

AWS SAM(Serverless Application Model)은 AWS 환경에서 서버리스 애플리케이션을 구축할 때 사용할 수 있도록 AWS가 공식으로 제공하는 프레임워크이다.

SAM은 AWS CloudFormation이 확장된 형태로, 배포 과정에서 SAM Template이 CloudFormation Template으로 변환된다.

SAM을 통해 구축할 수 있는 AWS 서비스의 종류는 다음과 같다.

  • API Gateway
  • DynamoDB
  • Lambda Function/Layer/Application

특징

  • AWS SAM에서 지원하지 않는 리소스에 대해서는 CloudFormation 문법에 맞게 작성하여 함께 사용이 가능함.
  • DeploymentPreference 프로퍼티를 사용하여 서버리스 애플리케이션에 대한 Canary 배포가 가능함.
  • AWS X-Ray를 사용한 퍼포먼스 모니터링을 손쉽게 구현할 수 있음.
  • 서버리스 애플리케이션 레포지토리를 통해 다른 사람이 구현한 기능을 파라미터만 변경하여 재사용 가능.

3. SAM Template

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app

  Sample SAM Template for sam-app

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs10.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

SAM Template의 주요 항목은 다음과 같다.

  • Transform 선언: 'Transform: AWS::Serverless-2016-10-31'이라는 구문을 선언하여, SAM Template임을 명시해야 함.
  • Globals 섹션: 서버리스 애플리케이션에 공통적으로 적용되는 속성을 정의함.
  • Resources 섹션: CloudFormation 리소스와 SAM 리소스를 정의함.

4. SAM CLI

AWS에서는 SAM CLI라는 툴을 제공하고 있으며, 이 툴을 활용하여 로컬 환경에서 Lambda 함수를 실행하고, CloudFormation 코드를 검증할 수 있다.

SAM CLI 설치 및 테스트 애플리케이션 배포

sam cli는 로컬에서 함수 실행시 Docker 컨테이너를 실행시키기 때문에, 사전에 Docker가 설치되어 있어야 한다.

# sam cli 설치
$ pip install --user aws-sam-cli
$ sam --version # sam cli 버전 확인
$ node -v
v10.21.0
 
# nodejs 10버전의 예제 애플리케이션을 생성
$ sam init --runtime nodejs10.x

# 생성된 샘플 애플리케이션 디렉터리 구조
$ tree sam-app
sam-app
├── README.md
├── events
│   └── event.json
├── hello-world # 애플리케이션 코드
│   ├── app.js
│   ├── package.json
│   └── tests
│       └── unit
│           └── test-handler.js
└── template.yaml # SAM Template으로 작성된 Lambda 생성 코드

4 directories, 6 files

sam cli를 활용하여 애플리케이션을 빌드 및 배포한다.

# 애플리케이션 빌드
$ sam build

# 서버리스 애플리케이션(Lambda) 로컬 테스트
# local에서 접속할 수 있는 Endpoint 제공
$ sam local start-api
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2020-06-13 21:38:51  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking app.lambdaHandler (nodejs10.x)

Fetching lambci/lambda:nodejs10.x Docker container image......
START RequestId: cbfc9da1-89c1-1377-2a03-63420999e9b8 Version: $LATEST
END RequestId: cbfc9da1-89c1-1377-2a03-63420999e9b8
REPORT RequestId: cbfc9da1-89c1-1377-2a03-63420999e9b8  Init Duration: 197.79 ms    Duration: 8.19 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB 
No Content-Type given. Defaulting to 'application/json'.
2020-06-13 21:39:10 127.0.0.1 - - [13/Jun/2020 21:39:10] "GET /hello HTTP/1.1" 200 -
2020-06-13 21:39:11 127.0.0.1 - - [13/Jun/2020 21:39:11] "GET /favicon.ico HTTP/1.1" 403 -

# 서버리스 애플리케이션 배포
$ sam deploy --guided
...
Successfully created/updated stack - sam-app in ap-northeast-2

 

5. SAM의 장단점

장점

  • AWS에서 서버리스 시스템을 구성할 때 필요한 CloudFormation 코드가 추상화되어 있어, 좀 더 직관적으로 서버리스를 구성할 수 있음
  • 기존의 CloudFormation 코드와 함께 사용이 가능함
  • sam 명령어를 통해 Local 환경에서 Lambda 함수를 테스트해 볼 수 있어 개발에 편리함

단점

  • SAM으로 정의할 수 있는 리소스가 제한적임

    • API Gateway
    • DynamoDB
    • Lambda Function/Layer/Application
  • CloudFormation 코드에 익숙하지 않다면 러닝 커브가 있을 수 있음

댓글 0

로그인 후 댓글을 남길 수 있습니다.

아직 댓글이 없습니다.