1. 概要
前回は「AWS Step Functions」を触ってみる内容でした。今回は「AWS Step Functions」で「Lambda」を扱う内容となります。
2. AWSアカウントにサインアップ
2-1. 前提条件
3. Lambdaを作成
3-1. こちらを参考
- 【AWS】Lambdaを使ってみる – 株式会社isub
- ソースコード
export const handler = async (event, context) => {
const length = event.length;
const width = event.width;
let area = calculateArea(length, width);
console.log(`The area is ${area}`);
console.log('CloudWatch log group: ', context.logGroupName);
let data = {
"area": area,
};
return data;
function calculateArea(length, width) {
return length * width;
}
};
4. ASL(Amazon States Language)
4-1. こちらを参考
5. 処理(ステート)の流れ
5-1. 全体のフロー
5-2. States
5-2-1. Pass
"Getting started": {
"Type": "Pass",
"Parameters": {
"length.$": "$.length",
"width.$": "$.width"
},
"Next": "Lambda Invoke"
},
- 実行時にパラメータを受け取って、次に渡す。
- length
- width
- パラメータ名と値の変数に注意
5-2-2. Lambda Invoke
"Lambda Invoke": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload.$": "$",
"FunctionName": "arn:aws:lambda:ap-northeast-1:123456789012:function:myLambdaFunction:$LATEST"
},
"Retry": [
{
"ErrorEquals": [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 1,
"MaxAttempts": 3,
"BackoffRate": 2
}
],
"Next": "Over 100?"
},
- 実行Lambdaを指定
- OutputPath
- 次のステート(Over 100?)にLambdaの計算結果を渡す
5-2-3. Over 100?
"Over 100?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.area",
"NumericLessThan": 100,
"Next": "Failure"
},
{
"Variable": "$.area",
"NumericGreaterThanEquals": 100,
"Next": "Yes"
}
]
},
- 入力(area)が
- 100未満:Failureへ進む
- 100以上:Yesへ進む
5-2-4-1-1. Failure
"Failure": {
"Type": "Fail"
},
5-2-4-2-1. Yes
"Yes": {
"Type": "Pass",
"Next": "Wait 3 seconds"
},
- 次のステート(Wait 3 sec)に進む
5-2-4-2-2. Wait 3 sec
"Wait 3 seconds": {
"Type": "Wait",
"Seconds": 3,
"Next": "Success"
},
- 3秒Wait後、次のステート(Success)へ進む
5-2-4-2-3. Success
"Success": {
"Type": "Succeed"
}
6. 全体のASL
{
"Comment": "My state machine with Lambda.",
"StartAt": "Getting started",
"States": {
"Getting started": {
"Type": "Pass",
"Parameters": {
"length.$": "$.length",
"width.$": "$.width"
},
"Next": "Lambda Invoke"
},
"Lambda Invoke": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload.$": "$",
"FunctionName": "arn:aws:lambda:ap-northeast-1:123456789012:function:myLambdaFunction:$LATEST"
},
"Retry": [
{
"ErrorEquals": [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 1,
"MaxAttempts": 3,
"BackoffRate": 2
}
],
"Next": "Over 100?"
},
"Over 100?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.area",
"NumericLessThan": 100,
"Next": "Failure"
},
{
"Variable": "$.area",
"NumericGreaterThanEquals": 100,
"Next": "Yes"
}
]
},
"Failure": {
"Type": "Fail"
},
"Yes": {
"Type": "Pass",
"Next": "Wait 3 seconds"
},
"Wait 3 seconds": {
"Type": "Wait",
"Seconds": 3,
"Next": "Success"
},
"Success": {
"Type": "Succeed"
}
}
}
7. 実行
7-1. Failure
- 100未満
7-2. Success
- 100以上
8. 備考
「AWS Step Functions」で「Lambda」を扱う内容でした。
9. 参考
- What is Step Functions? – AWS Step Functions (amazon.com)
- Using Amazon States Language to define Step Functions workflows – AWS Step Functions
投稿者プロフィール
-
開発好きなシステムエンジニアです。
卓球にハマってます。