Print
카테고리: [ Amazon Web Services ]
조회수: 7424

1. 개요

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

 

2. AWS SAM이란

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

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

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

특징

 

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의 주요 항목은 다음과 같다.

 

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의 장단점

장점

단점