AWS는 서버리스 환경에서의 애플리케이션 구현 모델인 AWS SAM을 개발하고 있다. 여기에는 SAM 사양과 SAM 템플릿을 AWS CloudFormation으로 변환하는 코드, 프로그래밍 예제 등을 포함하고 있다. SAM Local은 SAM의 구현체다.
서비리스 응용 프로그램을 만들려면, 람다함수에 대한 사양을 저장하는 JSON이나 YAML 형식의 SAM 템플릿을 만든다. 그리고 SAM 구현체 중 하나인 SAM Local의 CLI를 이용해서 응용 프로그램을 테스트 하고 업로드 및 배포를 한다. SAM은 응용 프로그램의 사양을 클라우드포메이션 구문으로 변환하면서, 지정되지 않은 속성들을 기본 값으로 채우고 호출 권한등을 결정해서 람다를 배포 한다.
$ go get -u github.com/cpliakas/aws-sam-golang-example
셈플 패키지 디렉토리를 보면 run.sh가 있다. 내용을 보자.
GOOS=linux go build -o main
sam local start-api
main 이름으로 빌드하고 sam local start-api를 실행한다.
# sam local start-api
2018-06-14 13:54:14 Mounting ExampleAPI at http://127.0.0.1:3000/hello [GET]
2018-06-14 13:54:14 Mounting ExampleAPI at http://127.0.0.1:3000/goodbye [GET]
2018-06-14 13:54:14 Mounting ExampleAPI at http://127.0.0.1:3000/ [GET]
2018-06-14 13:54:14 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
2018-06-14 13:54:14 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2018-06-14 13:54:27 Invoking main (go1.x)
2018-06-14 13:54:27 Found credentials in shared credentials file: ~/.aws/credentials
Fetching lambci/lambda:go1.x Docker container image..............
2018-06-14 13:55:04 Mounting /home/yundream/golang/src/github.com/cpliakas/aws-sam-golang-example as /var/task:ro inside runtime container
START RequestId: 857bf496-779d-18c1-a151-8d00a7c4db3f Version: $LATEST
END RequestId: 857bf496-779d-18c1-a151-8d00a7c4db3f
REPORT RequestId: 857bf496-779d-18c1-a151-8d00a7c4db3f Duration: 1.12 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 6 MB
sam local 은 docker를 기반으로 하는 개발환경을 제공한다. 처음 실행 할 경우 lambci/lambda:go1.x 도커이미지를 설치한다. 이 후에는 현재 디렉토리를 컨테이너 볼륨으로 해서 컨테이너를 실행하고 main 파일을 실행한다.
테스트를 해보자.
DynamoDB는 로컬에 설치 해서 사용 할 수 있으며, 도커 이미지도 제공한다. DynamoDB 도커 버전을 이용해서 테스트하기로 했다.
$ docker run --name dynamo -p 8000:8000 dwmkerr/dynamodb -sharedDb
Initializing DynamoDB Local with the following configuration:
Port: 8000
InMemory: false
DbPath: null
SharedDb: true
shouldDelayTransientStatuses: false
CorsParams: *
Contents
AWS Serverless Application Model (AWS SAM)
SAM LOCAL 설치
Sample 테스트
테스트 API
쿼리 파라메터
DynamoDB 연동
DynamoDB Local 준비
API 개발
테스트와 배포
Recent Posts
Archive Posts
Tags