AWS Lambda 성능: JavaScript vs Python vs Golang

AWS 람다에서 사용할 언어는 무엇인가요?

Page content

AWS로의 배포를 위해 람다 함수를 여러 언어로 작성할 수 있습니다.
자바스크립트, 파이썬, Golang으로 작성된 (거의 비어 있는) 함수의 성능을 비교해 보겠습니다.

AWS Lambda Function 로고

TL;DR

간단히 요약하면 다음과 같습니다:

언어 빌링된 지연 시간(워밍업) 빌링된 지연 시간 메모리 크기 최대 메모리 사용량
Python 197 ms 2 ms 128 MB 32 MB
Python S3 516 ms 35 ms 128 MB 77 MB
JavaScript 2 ms 2 ms 128 MB 63 MB
JavaScript S3 120 ms 72 ms 128 MB 89 MB
Golang 11 ms 1 ms 128 MB 20 MB
Golang S3 30 ms 29 ms 128 MB 38 MB
  • 128GB RAM에서 파이썬은 가장 긴 워밍업 시간을 보입니다.
  • 자바스크립트는 전체적으로 가장 느립니다.
  • Golang은 세 언어 중 가장 좋지만, 그 차이는 크지 않습니다.

흥미로운 점은 파이썬 람다 함수의 속도를 높이기 위해 더 많은 메모리와 함께 CPU를 추가하는 것입니다. 이는 워밍업 시간만 줄일 것으로 보입니다. S3에 대한 호출은 더 적은 지연 시간이 되지는 않을 것입니다.

AWS에서 진행한 합성 소수 테스트에 따르면, 128MB와 512MB RAM 제한을 사용하는 파이썬 람다의 실행 비용은 동일하며, 512MB 설정의 성능은 상당히 빠릅니다.

https://docs.aws.amazon.com/lambda/latest/operatorguide/computing-power.html

AWS SAM이란 무엇인가?

여기서는 다양한 람다 함수를 테스트하기 위해 SAM을 사용하고 있습니다.

AWS SAM은 Serverless Application Model입니다.

AWS SAM 프로젝트는 AWS SAM 템플릿과 함수 코드를 포함합니다.
SAM 템플릿은 람다 함수를 배포, 실행 및 테스트하는 방법, 권한 및 클라우드포메이션 스택을 생성하기 위한 기타 중요한 매개변수를 설명합니다.

사용하려면 aws clisam cli가 필요합니다.

AWS CLI 설치

로컬 설치 방법: Reinstall linux doc에서 AWS CLI 설치

또는 AWS 웹사이트에서: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

SAM CLI 설치

기본적으로 AWS 웹사이트에서 SAM CLI 설치자 zip 파일을 다운로드한 후 압축을 풀고 설치합니다.

wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
sudo ./sam-installation/install

# sam이 설치되었는지 확인
sam --version

설치자 파일 정리

rm -rf sam-installation
rm aws-sam-cli-linux-x86_64.zip

람다 함수 생성

다음 명령어를 세 번 실행하여

sam init

각각 hello-world 템플릿을 선택하고, 다른 언어를 사용하여 3개의 sam 애플리케이션을 생성합니다.

Python 3.12 기반 AWS 람다 함수

sam init의 출력:

    -----------------------
    Generating application:
    -----------------------
    Name: t1-py
    Runtime: python3.12
    Architectures: x86_64
    Dependency Manager: pip
    Application Template: hello-world
    Output Directory: .
    Configuration file: t1-py/samconfig.toml
    
    다음 단계는 t1-py/README.md 파일에서 찾을 수 있습니다.
        
Commands you can use next
=========================
[*] Create pipeline: cd t1-py && sam pipeline init --bootstrap
[*] Validate SAM template: cd t1-py && sam validate
[*] Test Function in the Cloud: cd t1-py && sam sync --stack-name {stack-name} --watch

JavaScript 및 nodejs20.x 기반 AWS 람다 함수

sam init의 출력:

    -----------------------
    Generating application:
    -----------------------
    Name: t2-js
    Runtime: nodejs20.x
    Architectures: x86_64
    Dependency Manager: npm
    Application Template: hello-world
    Output Directory: .
    Configuration file: t2-js/samconfig.toml
    
    다음 단계는 t2-js/README.md 파일에서 찾을 수 있습니다.
...

Golang (aws image al2023) 기반 AWS 람다 함수

sam init의 출력:

    -----------------------
    Generating application:
    -----------------------
    Name: t3-go
    Runtime: go (provided.al2023)
    Architectures: x86_64
    Dependency Manager: mod
    Application Template: hello-world
    Output Directory: .
    Configuration file: t3-go/samconfig.toml
    
    다음 단계는 t3-go/README.md 파일에서 찾을 수 있습니다.

빌드, 테스트, 배포 및 실행 - Python 기반 람다 함수

로컬에서 테스트

sam build --use-container

출력은 다음과 같습니다:

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided

세 번 실행한 후

sam local invoke

출력은 다음과 같습니다:

REPORT RequestId: 3b36575a-629d-40ba-adb7-018bdb8cae63  Init Duration: 0.09 ms  Duration: 46.95 ms      Billed Duration: 47 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: e1233b8a-99c6-49d9-ab91-f02408a5ec6f  Init Duration: 0.07 ms  Duration: 44.56 ms      Billed Duration: 45 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: 34772494-39ff-457d-a1c1-16cd5e177b42  Init Duration: 0.02 ms  Duration: 26.35 ms      Billed Duration: 27 ms  Memory Size: 128 MB     Max Memory Used: 128 MB

S3 읽기 호출을 추가하면 출력이 달라집니다.

...
REPORT RequestId: 158b806c-852d-4f6b-ab28-cdd6e0868fa1  Init Duration: 0.02 ms  Duration: 675.64 ms     Billed Duration: 676 ms Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: d4cc689c-f944-49fa-9254-555266816b97  Init Duration: 0.79 ms  Duration: 465.80 ms     Billed Duration: 466 ms Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: b8d30096-cd93-4787-84d7-e8f52d7e2e13  Init Duration: 0.02 ms  Duration: 447.83 ms     Billed Duration: 448 ms Memory Size: 128 MB     Max Memory Used: 128 MB

각 호출은 약 1/2초가 걸리며, 사용된 RAM은 128 MB입니다.

AWS에서 테스트

sam build --use-container
sam deploy --guided

SAM CLI는 클라우드포메이션 스택 이름을 묻고, 확인 후 AWS에 함수를 배포합니다.
SAM의 마지막 출력은 다음과 같을 것입니다:

Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: Y

2024-08-17 20:19:30 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 5.0 seconds)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                 ResourceType                                   LogicalResourceId                              ResourceStatusReason                         
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS                             AWS::CloudFormation::Stack                     t1-py                                          User Initiated                               
CREATE_IN_PROGRESS                             AWS::IAM::Role                                 HelloWorldFunctionRole                         -                                            
CREATE_IN_PROGRESS                             AWS::IAM::Role                                 HelloWorldFunctionRole                         Resource creation Initiated                  
CREATE_COMPLETE                                AWS::IAM::Role                                 HelloWorldFunctionRole                         -                                            
CREATE_IN_PROGRESS                             AWS::Lambda::Function                          HelloWorldFunction                             -                                            
CREATE_IN_PROGRESS                             AWS::Lambda::Function                          HelloWorldFunction                             Resource creation Initiated                  
CREATE_COMPLETE                                AWS::Lambda::Function                          HelloWorldFunction                             -                                            
CREATE_IN_PROGRESS                             AWS::ApiGateway::RestApi                       ServerlessRestApi                              -                                            
CREATE_IN_PROGRESS                             AWS::ApiGateway::RestApi                       ServerlessRestApi                              Resource creation Initiated                  
CREATE_COMPLETE                                AWS::ApiGateway::RestApi                       ServerlessRestApi                              -                                            
CREATE_IN_PROGRESS                             AWS::Lambda::Permission                        HelloWorldFunctionHelloWorldPermissionProd     -                                            
CREATE_IN_PROGRESS                             AWS::ApiGateway::Deployment                    ServerlessRestApiDeployment47fc2d5f9d          -                                            
CREATE_IN_PROGRESS                             AWS::Lambda::Permission                        HelloWorldFunctionHelloWorldPermissionProd     Resource creation Initiated                  
CREATE_COMPLETE                                AWS::Lambda::Permission                        HelloWorldFunctionHelloWorldPermissionProd     -                                            
CREATE_IN_PROGRESS                             AWS::ApiGateway::Deployment                    ServerlessRestApiDeployment47fc2d5f9d          Resource creation Initiated                  
CREATE_COMPLETE                                AWS::ApiGateway::Deployment                    ServerlessRestApiDeployment47fc2d5f9d          -                                            
CREATE_IN_PROGRESS                             AWS::ApiGateway::Stage                         ServerlessRestApiProdStage                     -                                            
CREATE_IN_PROGRESS                             AWS::ApiGateway::Stage                         ServerlessRestApiProdStage                     Resource creation Initiated                  
CREATE_COMPLETE                                AWS::ApiGateway::Stage                         ServerlessRestApiProdStage                     -                                            
CREATE_COMPLETE                                AWS::CloudFormation::Stack                     t1-py                                          -                                            
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CloudFormation outputs from deployed stack
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs                                                                                                                                                                                   
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
...
Key                 HelloWorldApi                                                                                                                                                         
Description         API Gateway endpoint URL for Prod stage for Hello World function                                                                                                      
Value               https://wq3w4d41j7.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/                                                                                               
...-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

여기서 HelloWorldApi 키와 설명 API Gateway endpoint URL for Prod stage for Hello World function이 있습니까? 우리의 공개 URL은 다음과 같습니다:

https://wq3w4d41j7.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/  

하지만 먼저 로그 리스너를 시작해야 합니다:

sam logs -n HelloWorldFunction --stack-name "t1-py" --tail

그리고 로그 리스너가 실행되는 동안, 다른 터미널 창에서 실행합니다:

curl https://wq3w4d41j7.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/
curl https://wq3w4d41j7.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/
curl https://wq3w4d41j7.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/

출력:

2024/08/18/[$LATEST]d86ecb94a39d44778f35bc2200b077a7 2024-08-18T00:46:22.468000 REPORT RequestId: 2e35fcf9-4d4b-4ced-962e-923adeddcbaa  Duration: 199.56 ms     Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 56 MB
...break...
2024/08/18/[$LATEST]d86ecb94a39d44778f35bc2200b077a7 2024-08-18T00:46:22.909000 REPORT RequestId: dd200d9d-cf35-4813-a243-ca8cf7435908  Duration: 194.60 ms     Billed Duration: 195 ms Memory Size: 128 MB Max Memory Used: 56 MB
...break...
2024/08/18/[$LATEST]661b5b35a7c54641adb72974b273b78a 2024-08-18T00:48:01.610000 REPORT RequestId: afae88d1-3873-40a3-8b82-caa7019ca45e  Duration: 1.94 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 32 MB  Init Duration: 117.62 ms
...
2024/08/18/[$LATEST]661b5b35a7c54641adb72974b273b78a 2024-08-18T00:48:03.292000 REPORT RequestId: d7011c87-e6a2-4d76-bb39-4c32b754b991  Duration: 1.71 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 32 MB
...
2024/08/18/[$LATEST]661b5b35a7c54641adb72974b273b78a 2024-08-18T00:48:06.905000 REPORT RequestId: 0eab1866-b11f-4a26-8234-be07f47ecb49  Duration: 1.62 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 32 MB
...
2024/08/18/[$LATEST]661b5b35a7c54641adb72974b273b78a 2024-08-18T00:48:12.696000 REPORT RequestId: f0cc5b4b-f3c3-49d5-9708-fce265581ed2  Duration: 1.51 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 32 MB
...
2024/08/18/[$LATEST]661b5b35a7c54641adb72974b273b78a 2024-08-18T00:48:13.202000 REPORT RequestId: 40367789-27c0-419b-8483-cf054978e9b6  Duration: 1.51 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 32 MB
...
2024/08/18/[$LATEST]661b5b35a7c54641adb72974b273b78a 2024-08-18T00:48:13.552000 REPORT RequestId: 6d166356-5a6c-44c7-b5dd-5c2808340b79  Duration: 1.38 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 32 MB

S3 호출 시 다음과 같습니다:

2024/08/18/[$LATEST]992dbfa1ec234d2aa1d9d6003de766de 2024-08-18T00:54:55.322000 REPORT RequestId: d97fd1cd-d9ef-4d16-adcb-514fa65ca6f8  Duration: 513.99 ms     Billed Duration: 514 ms Memory Size: 128 MB Max Memory Used: 77 MB
...
a break
...
2024/08/18/[$LATEST]992dbfa1ec234d2aa1d9d6003de766de 2024-08-18T00:55:02.063000 REPORT RequestId: e6e6884d-d6b6-4dc7-aede-f3863f3b331d  Duration: 519.35 ms     Billed Duration: 520 ms Memory Size: 128 MB Max Memory Used: 77 MB
...
2024/08/18/[$LATEST]992dbfa1ec234d2aa1d9d6003de766de 2024-08-18T00:55:02.202000 REPORT RequestId: 91eeaca0-c983-4e99-9608-36948511bd27  Duration: 23.95 ms      Billed Duration: 24 ms  Memory Size: 128 MB Max Memory Used: 77 MB
...
2024/08/18/[$LATEST]992dbfa1ec234d2aa1d9d6003de766de 2024-08-18T00:55:02.662000 REPORT RequestId: d7035256-53f8-463c-93fd-f5de608f47a6  Duration: 33.55 ms      Billed Duration: 34 ms  Memory Size: 128 MB Max Memory Used: 77 MB
...
2024/08/18/[$LATEST]992dbfa1ec234d2aa1d9d6003de766de 2024-08-18T00:55:03.082000 REPORT RequestId: a2e338b3-f18e-4644-8f6d-2ab151421996  Duration: 21.50 ms      Billed Duration: 22 ms  Memory Size: 128 MB Max Memory Used: 77 MB
...
2024/08/18/[$LATEST]992dbfa1ec234d2aa1d9d6003de766de 2024-08-18T00:55:03.562000 REPORT RequestId: 111665a1-f43c-488b-bc02-cd203fef478f  Duration: 42.58 ms      Billed Duration: 43 ms  Memory Size: 128 MB Max Memory Used: 77 MB

메모리 크기: 77 MB, 빌링된 지연 시간 약 35 ms…
워밍업 시간 1/2초

AWS 클라우드포메이션 스택 정리

sam delete --stack-name "t1-py"

빌드, 테스트, 배포 및 실행 - JavaScript 기반 Lambda 함수

JS 로컬

로컬에서 빌드 및 실행

sam build
sam local invoke

결과:

REPORT RequestId: 9e3c0a43-6432-4cc7-babf-f591773e5315  Init Duration: 0.05 ms  Duration: 126.34 ms     Billed Duration: 127 ms Memory Size: 128 MB     Max Memory Used: 128 MB
REPORT RequestId: 2856e8f1-166f-4a90-a094-d575ef00ff69  Init Duration: 0.02 ms  Duration: 43.73 ms      Billed Duration: 44 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
REPORT RequestId: f1156bd5-dd9f-4c1c-b65d-68c04df5a987  Init Duration: 0.02 ms  Duration: 42.79 ms      Billed Duration: 43 ms  Memory Size: 128 MB     Max Memory Used: 128 MB

로컬에서 S3 호출 지연 시간:

REPORT RequestId: 06c4a5a0-6f1a-4e09-9518-cf706d08c319  Init Duration: 0.13 ms  Duration: 280.33 ms     Billed Duration: 281 ms Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: 4393e8bd-c59b-42e5-83b5-1729d0fb4d8f  Init Duration: 0.08 ms  Duration: 270.42 ms     Billed Duration: 271 ms Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: 90979693-7245-4f88-9f6b-070be83f75df  Init Duration: 0.05 ms  Duration: 431.86 ms     Billed Duration: 432 ms Memory Size: 128 MB     Max Memory Used: 128 MB

AWS 상의 JS

배포하고 SAM이 요청하는 대로 허용하여, 우리의 Lambda 함수에 인증되지 않은 호출을 허용합니다.

sam deploy --guided
# 이제 로그를 확인합니다
sam logs -n HelloWorldFunction --stack-name t2-js --tail
# 또는
sam logs -n HelloWorldS3Function --stack-name t2-js --tail

다른 콘솔에서 cURL을 실행한 후 로그:

2024/08/18/[$LATEST]fe2dcf2abd9041a0b90630c2c8aa0005 2024-08-18T01:38:38.831000 REPORT RequestId: 24e86600-e316-4678-81db-41eafff81aa0  Duration: 4.92 ms       Billed Duration: 5 ms   Memory Size: 128 MB Max Memory Used: 62 MB  Init Duration: 139.82 ms
...
2024/08/18/[$LATEST]fe2dcf2abd9041a0b90630c2c8aa0005 2024-08-18T01:38:47.451000 REPORT RequestId: 0d269d7f-61f6-4b48-b3c2-6f83aaba5f45  Duration: 2.58 ms       Billed Duration: 3 ms   Memory Size: 128 MB Max Memory Used: 62 MB
...
2024/08/18/[$LATEST]fe2dcf2abd9041a0b90630c2c8aa0005 2024-08-18T01:38:58.622000 REPORT RequestId: f1bd0e5a-d1d0-4136-9b02-6fba4dcc9a70  Duration: 1.34 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 63 MB
...
2024/08/18/[$LATEST]fe2dcf2abd9041a0b90630c2c8aa0005 2024-08-18T01:38:59.089000 REPORT RequestId: baa791a7-4f9c-41be-b516-81213bc7a365  Duration: 1.44 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 63 MB
...
2024/08/18/[$LATEST]fe2dcf2abd9041a0b90630c2c8aa0005 2024-08-18T01:38:59.966000 REPORT RequestId: ea2a1515-1188-4a44-956f-91f2fc97491f  Duration: 1.46 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 63 MB
...
2024/08/18/[$LATEST]fe2dcf2abd9041a0b90630c2c8aa0005 2024-08-18T01:39:00.428000 REPORT RequestId: 0f536ec5-26cb-42b9-beaa-045984b8c468  Duration: 1.45 ms       Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 63 MB

좋습니다. 아무것도 하지 않는데 많은 시간을 보내지 않았지만, 잠들지도 않습니다.

S3 버킷에서 읽는 Lambda 함수의 JavaScript 성능

2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:42:45.155000 REPORT RequestId: c4630419-4502-467c-9cda-c5087d6b2743  Duration: 1051.97 ms    Billed Duration: 1052 ms        Memory Size: 128 MB Max Memory Used: 88 MB  Init Duration: 411.76 ms
...break...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:42:52.195000 REPORT RequestId: 4b881f2b-4b30-4890-afc2-b7299b6397b3  Duration: 259.87 ms     Billed Duration: 260 ms Memory Size: 128 MB Max Memory Used: 89 MB
...break...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:42:58.455000 REPORT RequestId: 77f4d820-2f2e-4190-a1e3-bc4821ccdb9b  Duration: 165.44 ms     Billed Duration: 166 ms Memory Size: 128 MB Max Memory Used: 89 MB
...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:43:03.095000 REPORT RequestId: 41ce9fd8-aaf4-46c4-a827-0a45592041c0  Duration: 63.74 ms      Billed Duration: 64 ms  Memory Size: 128 MB Max Memory Used: 89 MB
...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:43:03.695000 REPORT RequestId: 9494f176-3818-4fb3-a581-857f1eb2df23  Duration: 73.16 ms      Billed Duration: 74 ms  Memory Size: 128 MB Max Memory Used: 89 MB
...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:43:04.315000 REPORT RequestId: 528c909f-4a08-415e-a713-e3d9e9e4d615  Duration: 87.44 ms      Billed Duration: 88 ms  Memory Size: 128 MB Max Memory Used: 89 MB
...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:43:04.835000 REPORT RequestId: 9b636429-56f5-4974-afe9-e53e786adb61  Duration: 55.62 ms      Billed Duration: 56 ms  Memory Size: 128 MB Max Memory Used: 89 MB
...
2024/08/18/[$LATEST]8e22ce6f54074c6098cdbf89ed0ca0b9 2024-08-18T01:43:05.415000 REPORT RequestId: d4c298df-b237-4605-894f-fde05fe0461a  Duration: 69.88 ms      Billed Duration: 70 ms  Memory Size: 128 MB Max Memory Used: 89 MB

AWS JS 정리

삭제 중…

sam delete --stack-name t2-js

빌드, 테스트, 배포 및 실행 - Golang 기반 Lambda 함수

로컬 Golang Lambda 테스트

정확히 말하면, 테스트 호출을 하는 것이며, 단위 테스트 실행은 아닙니다.

sam build 명령은 Makefile에 포함되어 있습니다. 따라서 실행하는 것은 다음과 같습니다:

make

단일 함수 실행

sam local invoke HelloWorldFunction --event events/event.json
sam local invoke HelloWorldFunction --event events/event.json
sam local invoke HelloWorldFunction --event events/event.json

sam local invoke HelloWorldS3Function --event events/event.json
sam local invoke HelloWorldS3Function --event events/event.json
sam local invoke HelloWorldS3Function --event events/event.json

선택 2: 이제 로컬 서버를 시작합니다:

sam local start-api

출력:

이제 별도의 터미널 창에서:

curl http://localhost:3000/hello
curl http://localhost:3000/hello
curl http://localhost:3000/hello

curl http://localhost:3000/hellos3
curl http://localhost:3000/hellos3
curl http://localhost:3000/hellos3

로컬에서 Golang 기반 Lambda에 대한 간단한 호출 결과:

REPORT RequestId: 54cc3271-7f04-44a3-8d91-d5d69d2428ea  Init Duration: 0.03 ms  Duration: 2.24 ms       Billed Duration: 3 ms   Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: c753bdf9-1af0-4b40-9516-5fb2ad96ba00  Duration: 0.29 ms       Billed Duration: 1 ms   Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: edceb310-e6e9-4f0a-bd6e-58afd40e9b70  Duration: 0.28 ms       Billed Duration: 1 ms   Memory Size: 128 MB     Max Memory Used: 128 MB

로컬에서 Golang 기반 Lambda에 대한 “S3” 호출 결과:

REPORT RequestId: 1556e298-a17a-4d45-9fdc-b7f62118c246  Init Duration: 0.06 ms  Duration: 142.76 ms     Billed Duration: 143 ms Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: e4638fb4-52f4-4086-b673-c9de0f02d3f7  Duration: 86.75 ms      Billed Duration: 87 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
...
REPORT RequestId: b1bdcbf8-06f8-4605-a7da-d34f41198811  Duration: 105.07 ms     Billed Duration: 106 ms Memory Size: 128 MB     Max Memory Used: 128 MB

1ms의 지속 시간은 좋지만, Memory Size: 128 MB? 왜 그런가요? 아마도 최소값이기 때문입니다.

AWS Golang Lambda 테스트

sam deploy --guided

로그 리스너를 시작합니다

sam logs -n HelloWorldFunction --stack-name t3-go --tail

함수를 3회 호출합니다:

curl https://k92zaa81w8.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/
curl https://k92zaa81w8.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/
curl https://k92zaa81w8.execute-api.ap-southeast-2.amazonaws.com/Prod/hello/

로그에서 확인할 수 있습니다:

2024/08/18/[$LATEST]1f5b2a65bda1433789d9281d02cf4622 2024-08-18T04:18:02.908000 REPORT RequestId: 358b5762-dad7-4380-a83f-e21349fae20b  Duration: 11.71 ms      Billed Duration: 70 ms     Memory Size: 128 MB     Max Memory Used: 20 MB  Init Duration: 57.52 ms
...break...
2024/08/18/[$LATEST]1f5b2a65bda1433789d9281d02cf4622 2024-08-18T04:18:07.404000 REPORT RequestId: 3051d741-504a-49ae-95b3-077628b4b929  Duration: 0.99 ms       Billed Duration: 1 ms      Memory Size: 128 MB     Max Memory Used: 20 MB
...break...
2024/08/18/[$LATEST]1f5b2a65bda1433789d9281d02cf4622 2024-08-18T04:18:11.923000 REPORT RequestId: 51c38ca9-920d-4f30-be75-a1a44538c9ee  Duration: 1.02 ms       Billed Duration: 2 ms      Memory Size: 128 MB     Max Memory Used: 20 MB
...
2024/08/18/[$LATEST]1f5b2a65bda1433789d9281d02cf4622 2024-08-18T04:18:12.486000 REPORT RequestId: 98727f66-f3f1-46ad-9a5a-55a6243609ba  Duration: 0.96 ms       Billed Duration: 1 ms      Memory Size: 128 MB     Max Memory Used: 20 MB
...
2024/08/18/[$LATEST]1f5b2a65bda1433789d9281d02cf4622 2024-08-18T04:18:13.015000 REPORT RequestId: 9d8bfb28-d4eb-4868-9af7-3b3946acc92c  Duration: 0.98 ms       Billed Duration: 1 ms      Memory Size: 128 MB     Max Memory Used: 20 MB
  • 청구 지속 시간은 2ms입니다. 좋습니다.
  • 최대 메모리 사용량: 20MB. 예상됩니다.
  • 잠들지 않는 것처럼 보입니다.

이제 S3에 대한 호출이 포함된 Lambda 함수에 대해 동일한 작업을 수행해 보겠습니다.

2024/08/18/[$LATEST]8bd6f2bea333485aad2cbf5166433cf5 2024-08-18T04:22:57.711000 REPORT RequestId: 9f09db3e-d9bc-45fe-9674-df133f0f1dc2  Duration: 28.08 ms      Billed Duration: 29 ms     Memory Size: 128 MB     Max Memory Used: 37 MB
...
2024/08/18/[$LATEST]8bd6f2bea333485aad2cbf5166433cf5 2024-08-18T04:23:15.324000 REPORT RequestId: 4bc39553-6a42-4347-b322-6c80f24cdb01  Duration: 30.56 ms      Billed Duration: 31 ms     Memory Size: 128 MB     Max Memory Used: 38 MB
...
2024/08/18/[$LATEST]8bd6f2bea333485aad2cbf5166433cf5 2024-08-18T04:23:51.144000 REPORT RequestId: 55ac2b18-394c-4699-87a5-4ba52230f386  Duration: 27.01 ms      Billed Duration: 28 ms     Memory Size: 128 MB     Max Memory Used: 38 MB

정리

삭제를 잊지 마세요

sam delete --stack-name t3-go

유용한 링크