자습서: Step Functions와 AWS SAM CLI Local을 사용한 워크플로 테스트 - AWS Step Functions

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

자습서: Step Functions와 AWS SAM CLI Local을 사용한 워크플로 테스트

둘 다 AWS Step Functions 로컬 AWS Lambda 시스템에서 실행되므로 코드를 배포하지 않고도 상태 머신과 Lambda 함수를 테스트할 수 있습니다. AWS

자세한 정보는 다음 주제를 참조하세요.

1단계: 설정 AWS SAM

AWS Serverless Application Model (AWS SAM) CLI 로컬을 사용하려면 AWS Command Line Interface, AWS SAM, Docker를 설치해야 합니다.

  1. 를 설치합니다. AWS SAM CLI

    참고

    를 AWS SAM CLI 설치하기 전에 AWS CLI 및 Docker를 설치해야 합니다. 설치를 위한 사전 요구 사항을 참조하십시오. AWS SAM CLI

  2. AWS SAM 빠른 시작 설명서를 살펴봅니다. 다음 단계에 따라 다음을 수행하십시오.

    이렇게 하면 sam-app 디렉터리가 생성되고 Python 기반 Hello World Lambda 함수가 포함된 환경이 빌드됩니다.

2단계: 로컬 테스트 AWS SAM CLI

Hello World Lambda 함수를 AWS SAM 설치하고 생성했으므로 이제 함수를 테스트할 수 있습니다. sam-app 디렉터리에서 다음 명령을 입력합니다.

sam local start-api

그러면 Lambda 함수의 로컬 인스턴스가 시작됩니다. 다음과 유사한 출력이 표시되어야 합니다.

2019-01-31 16:40:27 Found credentials in shared credentials file: ~/.aws/credentials 2019-01-31 16:40:27 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET] 2019-01-31 16:40:27 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 2019-01-31 16:40:27 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

브라우저를 열고 다음을 입력합니다.

http://127.0.0.1:3000/hello

다음과 유사한 응답이 출력됩니다.

{"message": "hello world", "location": "72.21.198.66"}

CTRLAPILambda를 종료하려면 +C를 입력합니다.

3단계: 로컬 시작 AWS SAM CLI

함수가 작동하는지 테스트했으니 이제 AWS SAM CLI Local을 시작하세요. sam-app 디렉터리에서 다음 명령을 입력합니다.

sam local start-lambda

그러면 AWS SAM CLI Local이 시작되고 다음 출력과 마찬가지로 사용할 엔드포인트가 제공됩니다.

2019-01-29 15:33:32 Found credentials in shared credentials file: ~/.aws/credentials 2019-01-29 15:33:32 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint. 2019-01-29 15:33:32 * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)

4단계: Step Functions Local 시작

JAR파일

Step Funtions Local의 .jar 파일 버전을 사용하는 경우 Step Functions를 시작하고 Lambda 엔드포인트를 지정합니다. .jar 파일 압축을 해제한 디렉터리에 다음 명령을 입력합니다.

java -jar StepFunctionsLocal.jar --lambda-endpoint http://localhost:3001

Step Functions Local이 시작되면 환경을 확인한 다음 ~/.aws/credentials 파일에 구성된 보안 인증 정보를 확인합니다. 기본적으로 가상의 사용자 ID를 사용하기 시작하며 region us-east-1로 나열됩니다.

2019-01-29 15:38:06.324: Failed to load credentials from environment because Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)) 2019-01-29 15:38:06.326: Loaded credentials from profile: default 2019-01-29 15:38:06.326: Starting server on port 8083 with account 123456789012, region us-east-1

Docker

Step Functions Local의 Docker 버전을 사용하는 경우 다음 명령으로 Step Functions를 시작합니다.

docker run -p 8083:8083 amazon/aws-stepfunctions-local

Step Functions의 Docker 버전 설치 방법은 Docker에서 Step Functions 로컬 (다운로드 가능한 버전) 설정하기 섹션을 참조하세요.

참고

.jar 파일에서 Step Functions를 시작하면 명령줄을 통해 또는 환경 변수를 설정하여 엔드포인트를 지정할 수 있습니다. Docker 버전의 경우 텍스트 파일에서 엔드포인트와 자격 증명을 지정해야 합니다. Step Functions Local의 구성 옵션 설정을 참조하세요.

5단계: AWS SAM CLI 로컬 함수를 참조하는 스테이트 머신 만들기

Step Functions Local이 실행 중이면 1단계: 설정 AWS SAM에서 초기화한 HelloWorldFunction을 참조하는 상태 시스템을 만듭니다.

aws stepfunctions --endpoint http://localhost:8083 create-state-machine --definition "{\ \"Comment\": \"A Hello World example of the Amazon States Language using an AWS Lambda Local function\",\ \"StartAt\": \"HelloWorld\",\ \"States\": {\ \"HelloWorld\": {\ \"Type\": \"Task\",\ \"Resource\": \"arn:aws:lambda:us-east-1:123456789012:function:HelloWorldFunction\",\ \"End\": true\ }\ }\ }\ }}" --name "HelloWorld" --role-arn "arn:aws:iam::012345678901:role/DummyRole"

그러면 상태 머신이 생성되고 실행을 시작하는 데 사용할 수 있는 Amazon 리소스 이름 (ARN) 이 제공됩니다.

{ "creationDate": 1548805711.403, "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld" }

6단계: 로컬 상태 머신의 실행 시작

상태 시스템을 만들었으면 실행을 시작합니다. 다음 aws stepfunctions 명령을 사용할 ARN 때는 엔드포인트와 상태 머신을 참조해야 합니다.

aws stepfunctions --endpoint http://localhost:8083 start-execution --state-machine arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld --name test

이렇게 하면 HelloWorld 상태 시스템의 test 실행이 시작됩니다.

{ "startDate": 1548810641.52, "executionArn": "arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test" }

이제 Step Functions가 로컬에서 실행되므로 를 사용하여 Step Functions와 상호 작용할 수 AWS CLI있습니다. 예를 들어 이 실행에 대한 정보를 얻으려면 다음 명령을 사용합니다.

aws stepfunctions --endpoint http://localhost:8083 describe-execution --execution-arn arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test

실행에 대해 describe-execution을 직접적으로 호출하면 다음 출력과 같이 더욱 완전한 세부 정보가 제공됩니다.

{ "status": "SUCCEEDED", "startDate": 1549056334.073, "name": "test", "executionArn": "arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test", "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld", "stopDate": 1549056351.276, "output": "{\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"hello world\\\", \\\"location\\\": \\\"72.21.198.64\\\"}\"}", "input": "{}" }