티스토리 뷰

1. AWS SAM CLI 설치( Mac )
2. AWS Toolkit 설치( IntelliJ )
3. 서버리스 애플리케이션 구성
4. 빌드 및 로컬 테스트
5. SAM 테스트 명령어

1. AWS SAM CLI 설치( Mac )

AWS SAM CLI는 Lambda 실행 환경을 제공해주고, 코드 디버깅을 할 수 있게 해줍니다.

 

AWS SAM CLI 설치를 위해 아래 명령을 수행합니다.

brew tap aws/tap
brew install aws-sam-cli

설치 확인을 위해 아래 명령을 수행합니다.

sam --version

정상적으로 설치가 완료되었으면 아래와 같이 버전이 표기됩니다.

SAM CLI, version 1.17.0

AWS SAM CLI 업그레이드를 위해서는 아래 명령을 수행합니다.

brew upgrade aws-sam-cli

2. AWS Toolkit 설치( IntelliJ )

Plugins에서 AWS 로 검색하여, AWS Toolkit 을 설치해 줍니다.


3. 서버리스 애플리케이션 구성

AWS Toolkit을 설치하고 나서, File > New > Project 에서 아래와 같이 선택을 해줍니다.

아래와 같이, 입력 및 선택을 해주고 Finish를 눌러 프로젝트를 생성해줍니다.


4. 빌드 및 로컬 테스트

Lambda 로컬 테스트는 Docker 컨테이너를 사용하여 수행하므로, Docker를 먼저 실행 시켜 줍니다.

 

App 파일로 이동하여, App 클래스 옆에 Lambda 아이콘에 마우스 왼쪽 버튼을 누르고 Run '[Local] App'을 수행해 줍니다.

 

아래와 같이 설정을 해주고, API 게이트웨이 호출을 시뮬레이션하기 위해 Input에 API Gateway AWS Proxy를 선택해 줍니다.

Run 을 수행하면, 빌드와 로컬 Lambda 호출이 수행 됩니다.

 

빌드 탭에서는 아래와 같이 sam build 로 빌드한 결과가 출력됩니다.

/usr/local/bin/sam build Function --template /Users/xxx/GIT/aws-lambda-developer-guide/sample-apps/goatee-IntelliJ/HelloWorldFunction/src/main/.aws-sam/temp-template.yaml --build-dir /Users/charlieshin/GIT/aws-lambda-developer-guide/sample-apps/goatee-IntelliJ/HelloWorldFunction/src/main/.aws-sam/build
Building codeuri: /Users/xxx/GIT/aws-lambda-developer-guide/sample-apps/goatee-IntelliJ/HelloWorldFunction runtime: java11 metadata: {} functions: ['Function']
Running JavaGradleWorkflow:GradleBuild
Running JavaGradleWorkflow:CopyArtifacts

Build Succeeded

Built Artifacts  : build
Built Template   : build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t build/template.yaml
[*] Deploy: sam deploy --guided --template-file build/template.yaml

Run 탭에서는 sam local inveke 로 Lambda 함수를 호출 및 결과를 확인할 수 있습니다.

/usr/local/bin/sam local invoke --template /Users/xxx/GIT/aws-lambda-developer-guide/sample-apps/goatee-IntelliJ/HelloWorldFunction/src/main/.aws-sam/build/template.yaml --event "/private/var/folders/c7/fr6_tlj10sj3b6rfjf1lc3m40000gn/T/[Local] App-event1.json"
Invoking helloworld.App (java11)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-java11:rapid-1.17.0.

Mounting /Users/xxx/GIT/aws-lambda-developer-guide/sample-apps/goatee-IntelliJ/HelloWorldFunction/src/main/.aws-sam/build/Function as /var/task:ro,delegated inside runtime container
START RequestId: 7cc1ed7b-8971-422a-b0b0-e1cb8b9176d0 Version: $LATEST
{"statusCode":200,"headers":{"X-Custom-Header":"application/json","Content-Type":"application/json"},"body":"{ \"message\": \"hello world\", \"location\": \"110.9.143.178\" }"}END RequestId: 7cc1ed7b-8971-422a-b0b0-e1cb8b9176d0
REPORT RequestId: 7cc1ed7b-8971-422a-b0b0-e1cb8b9176d0	Init Duration: 0.21 ms	Duration: 2323.27 ms	Billed Duration: 2400 ms	Memory Size: 128 MB	Max Memory Used: 128 MB	

빌드를 수행하면 .aws-sam 폴더의 build 폴더에는 패키징할 코드와 dependency들이 저장 됩니다.( 패키지 타입이 zip 인 경우 )

➜  goatee-IntelliJ git:(main) ✗ ls HelloWorldFunction/src/main/.aws-sam 
build              temp-template.yaml

sam local 명령어는 Docker container로 Lambda 실행 환경을 시뮬레이션하여 애플리케이션을 수행할 수 있게 해줍니다.

➜  goatee-IntelliJ git:(main) ✗ 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
2021-02-10 16:24:14  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

5. SAM 테스트 명령어

로컬에서 테스트 할 때, 로컬에서 API 호스팅 하는 방법과 함수를 직접 호출 하는 방법이 있습니다.

로컬에서 API 호스팅

로컬에서 API 를 호스팅 하여, curl을 사용하여 호스팅을 종료하기전까지 계속해서 Lambda를 호출 할 수 있습니다.

➜  goatee-IntelliJ git:(main) ✗ 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
2021-02-15 12:58:59  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking Container created from helloworldfunction:java11-gradle-v1
Building image.........
Skip pulling image and use local one: helloworldfunction:rapid-1.17.0.

START RequestId: c11fa187-1f79-4cec-a18d-5a8c5d796bed Version: $LATEST
END RequestId: c11fa187-1f79-4cec-a18d-5a8c5d796bed
REPORT RequestId: c11fa187-1f79-4cec-a18d-5a8c5d796bed  Init Duration: 0.40 ms  Duration: 2159.27 ms    Billed Duration: 2200 ms        Memory Size: 128 MBMax Memory Used: 128 MB  
2021-02-15 12:59:12 127.0.0.1 - - [15/Feb/2021 12:59:12] "GET /hello HTTP/1.1" 200 -
Invoking Container created from helloworldfunction:java11-gradle-v1
Building image.........
Skip pulling image and use local one: helloworldfunction:rapid-1.17.0.

START RequestId: ab6ab9c3-e96e-42c4-842e-702968456471 Version: $LATEST
END RequestId: ab6ab9c3-e96e-42c4-842e-702968456471
REPORT RequestId: ab6ab9c3-e96e-42c4-842e-702968456471  Init Duration: 0.60 ms  Duration: 2176.65 ms    Billed Duration: 2200 ms        Memory Size: 128 MBMax Memory Used: 128 MB  
2021-02-15 12:59:23 127.0.0.1 - - [15/Feb/2021 12:59:23] "GET /hello HTTP/1.1" 200 -
^C%                                                                                    
curl http://127.0.0.1:3000/hello

함수 직접 호출

invoke 함수를 직접 호출하여 이벤트( event.json ) 페이로드를 전달 할 수 있습니다.

➜  goatee-IntelliJ git:(main) ✗ sam local invoke "HelloWorldFunction" -e events/event.json
Invoking Container created from helloworldfunction:java11-gradle-v1
Building image.........
Skip pulling image and use local one: helloworldfunction:rapid-1.17.0.

START RequestId: 14453add-4049-459d-b5ad-93236e388a4e Version: $LATEST
END RequestId: 14453add-4049-459d-b5ad-93236e388a4e
REPORT RequestId: 14453add-4049-459d-b5ad-93236e388a4e  Init Duration: 0.53 ms  Duration: 2594.21 ms    Billed Duration: 2600 ms        Memory Size: 128 MBMax Memory Used: 128 MB  
{"statusCode":200,"headers":{"X-Custom-Header":"application/json","Content-Type":"application/json"},"body":"{ \"message\": \"hello world\", \"location\": \"110.9.143.178\" }"}%            
Usage: sam local invoke [OPTIONS] [FUNCTION_IDENTIFIER]

  You can use this command to execute your function in a Lambda-like
  environment locally. You can pass in an event body using the -e (--event)
  parameter. Logs from the Lambda function will be written to stdout.

Options:
   -e, --event PATH                JSON file containing event data passed to
                                   the Lambda function during invoke. If this
                                   option is not specified, no event is
                                   assumed. Pass in the value '-' to input JSON
                                   via stdin

참조

docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html

aws.amazon.com/ko/blogs/korea/aws-toolkit-for-intellij-now-generally-available/

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함