⚙️

Step Functionsでパスを色々試す

2022/08/29に公開

概要

Step Functionsでタスクの実行結果をフィルターするのにResultSelectorという機能を使います。JSONPathを使用しますが、Step Functionsのドキュメント中ではそこまで詳しく解説されていないので色々試してみます。
https://github.com/json-path/JsonPath

日本語ドキュメント

2022/08/29現在、リファレンスパスについて日本語版ドキュメントが追いついていないので、英語版ドキュメントを参照してください。

英語版ドキュメント

https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html

A reference path is a path whose syntax is limited in such a way that it can identify only a single node in a JSON structure:

  • You can access object fields using only dot (.) and square bracket ([ ]) notation.
  • Functions such as length() aren't supported.
  • Lexical operators, which are non-symbolic, such as subsetof aren't supported.
  • Filtering by regular expression or by referencing another value in the JSON structure isn't supported.
  • The @ operator, matching the current node being processed in a filter, does not match scalar values. It only matches objects.

日本語版ドキュメント

https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/amazon-states-language-paths.html

リファレンスパスは、JSON 構造内の単一ノードのみを識別できるように構文が制限されたパスです。

  • オブジェクトのフィールドにアクセスするには、ドット (.) と角括弧 ([ ]) のみを使用して表記します。
  • 演算子 @ .. , : ? * はサポートされていません。
  • length() などの関数はサポートされていません。

フィルタしてみる

DescribeAutoScalingGroups アクションを実行し、2つのAutoScalingGroupにそれぞれ3つのインスタンスが含まれている結果が得られた、という想定のデータを用意しました。

DescribeAutoScalingGroups の結果
{
    "AutoScalingGroups": [
        {
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-3:123456789012:autoScalingGroup:12345678-0000-0000-0000-123456789012:autoScalingGroupName/groupA",
            "AutoScalingGroupName": "groupA",
            "AvailabilityZones": [
                "ap-northeast-3b",
                "ap-northeast-3c",
                "ap-northeast-3a"
            ],
            "CapacityRebalance": true,
            "CreatedTime": "2022-08-28T13:58:56.425Z",
            "DefaultCooldown": 300,
            "DesiredCapacity": 3,
            "EnabledMetrics": [],
            "HealthCheckGracePeriod": 300,
            "HealthCheckType": "EC2",
            "Instances": [
                {
                    "AvailabilityZone": "ap-northeast-3a",
                    "HealthStatus": "Healthy",
                    "InstanceId": "i-a1234567890123456",
                    "InstanceType": "t2.micro",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "1"
                    },
                    "LifecycleState": "InService",
                    "ProtectedFromScaleIn": true
                },
                {
                    "AvailabilityZone": "ap-northeast-3b",
                    "HealthStatus": "Healthy",
                    "InstanceId": "i-a2345678901234567",
                    "InstanceType": "t2.micro",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "1"
                    },
                    "LifecycleState": "InService",
                    "ProtectedFromScaleIn": true
                },
                {
                    "AvailabilityZone": "ap-northeast-3c",
                    "HealthStatus": "Healthy",
                    "InstanceId": "i-a3456789012345678",
                    "InstanceType": "t2.micro",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "1"
                    },
                    "LifecycleState": "InService",
                    "ProtectedFromScaleIn": true
                }
            ],
            "LoadBalancerNames": [],
            "MaxSize": 3,
            "MinSize": 3,
            "MixedInstancesPolicy": {
                "InstancesDistribution": {
                    "OnDemandAllocationStrategy": "prioritized",
                    "OnDemandBaseCapacity": 0,
                    "OnDemandPercentageAboveBaseCapacity": 100,
                    "SpotAllocationStrategy": "lowest-price",
                    "SpotInstancePools": 2
                },
                "LaunchTemplate": {
                    "LaunchTemplateSpecification": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "$Default"
                    },
                    "Overrides": [
                        {
                            "InstanceType": "t2.micro"
                        }
                    ]
                }
            },
            "NewInstancesProtectedFromScaleIn": true,
            "ServiceLinkedRoleARN": "arn:aws:iam::123456789012:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
            "SuspendedProcesses": [],
            "Tags": [],
            "TargetGroupARNs": [],
            "TerminationPolicies": [
                "Default"
            ],
            "VpcZoneIdentifier": "subnet-12345678901234567,subnet-23456789012345678,subnet-34567890123456789"
        },
        {
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-3:123456789012:autoScalingGroup:12345678-0000-0000-0000-123456789012:autoScalingGroupName/groupB",
            "AutoScalingGroupName": "groupB",
            "AvailabilityZones": [
                "ap-northeast-3b",
                "ap-northeast-3c",
                "ap-northeast-3a"
            ],
            "CapacityRebalance": true,
            "CreatedTime": "2022-08-28T13:58:56.425Z",
            "DefaultCooldown": 300,
            "DesiredCapacity": 3,
            "EnabledMetrics": [],
            "HealthCheckGracePeriod": 300,
            "HealthCheckType": "EC2",
            "Instances": [
                {
                    "AvailabilityZone": "ap-northeast-3a",
                    "HealthStatus": "Healthy",
                    "InstanceId": "i-b1234567890123456",
                    "InstanceType": "t2.micro",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "1"
                    },
                    "LifecycleState": "InService",
                    "ProtectedFromScaleIn": true
                },
                {
                    "AvailabilityZone": "ap-northeast-3b",
                    "HealthStatus": "Healthy",
                    "InstanceId": "i-b2345678901234567",
                    "InstanceType": "t2.micro",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "1"
                    },
                    "LifecycleState": "InService",
                    "ProtectedFromScaleIn": true
                },
                {
                    "AvailabilityZone": "ap-northeast-3c",
                    "HealthStatus": "Healthy",
                    "InstanceId": "i-b3456789012345678",
                    "InstanceType": "t2.micro",
                    "LaunchTemplate": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "1"
                    },
                    "LifecycleState": "InService",
                    "ProtectedFromScaleIn": true
                }
            ],
            "LoadBalancerNames": [],
            "MaxSize": 3,
            "MinSize": 3,
            "MixedInstancesPolicy": {
                "InstancesDistribution": {
                    "OnDemandAllocationStrategy": "prioritized",
                    "OnDemandBaseCapacity": 0,
                    "OnDemandPercentageAboveBaseCapacity": 100,
                    "SpotAllocationStrategy": "lowest-price",
                    "SpotInstancePools": 2
                },
                "LaunchTemplate": {
                    "LaunchTemplateSpecification": {
                        "LaunchTemplateId": "lt-12345678901234567",
                        "LaunchTemplateName": "test",
                        "Version": "$Default"
                    },
                    "Overrides": [
                        {
                            "InstanceType": "t2.micro"
                        }
                    ]
                }
            },
            "NewInstancesProtectedFromScaleIn": true,
            "ServiceLinkedRoleARN": "arn:aws:iam::123456789012:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
            "SuspendedProcesses": [],
            "Tags": [],
            "TargetGroupARNs": [],
            "TerminationPolicies": [
                "Default"
            ],
            "VpcZoneIdentifier": "subnet-12345678901234567,subnet-23456789012345678,subnet-34567890123456789"
        }
    ]
}

すべてのグループ名を取得

{
  "AutoScalingGroupNames.$": "$.AutoScalingGroups[*].AutoScalingGroupName"
}

結果

{
  "AutoScalingGroupNames": [
    "groupA",
    "groupB"
  ]
}

最初のグループ名を取得

{
  "AutoScalingGroupNames.$": "$.AutoScalingGroups[0].AutoScalingGroupName"
}

結果

{
  "AutoScalingGroupNames": "groupA"
}

すべてのインスタンスIDを取得

{
  "InstanceIds.$": "$.AutoScalingGroups[*].Instances[*].InstanceId"
}
もしくは
{
  "InstanceIds.$": "$..InstanceId"
}

結果

{
  "InstanceIds": [
    "i-a1234567890123456",
    "i-a2345678901234567",
    "i-a3456789012345678",
    "i-b1234567890123456",
    "i-b2345678901234567",
    "i-b3456789012345678"
  ]
}

ap-northeast-3a のインスタンスIDを取得

{
  "InstanceIds.$": "$.AutoScalingGroups[*].Instances[?(@.AvailabilityZone == 'ap-northeast-3a')].InstanceId"
}

結果

{
  "InstanceIds": [
    "i-a1234567890123456",
    "i-b1234567890123456"
  ]
}

Discussion