Search code examples
aws-cdkaws-codebuildaws-step-functions

What is the correct format of BatchGetBuilds parameters for 'AWS CDK - StepFunction - CallAwsService'


I'm working for a state machine by CDK.
And getting an issue to check the codebuild project status in the state machine...

Q. Could you let me know the correct format of batchGetBuilds parameters in CallAwsService?

enter image description here

import { CallAwsService } from "aws-cdk-lib/aws-stepfunctions-tasks"
import { JsonPath } from "aws-cdk-lib/aws-stepfunctions"

new CallAwsService(scope, "Check 1-1: Codebuild Status", {
        service: "codebuild",
        action: "batchGetBuilds",
        parameters: {
            Ids: [JsonPath.stringAt("$.results.codebuild.id")],
        },
        iamResources: ["*"],
        inputPath: "$",
        resultSelector: { "status.$": "$.builds[0].buildStatus" },
        resultPath: "$.results.bulidAmi",
    })

I tried 2 ways.

  1. JsonPath.stringAt("$.results.codebuild.id")
    Then it returns below and execution be failed.
"An error occurred while executing the state 'Check 1-1: Codebuild Status' (entered at the event id #9). 
The Parameters '{\"Ids\":\"******-generate-new-ami-project:05763ec2-89a6-4b56-8b44-************\"}' could not be used to start the Task:
[Cannot deserialize instance of `java.util.ArrayList<java.lang.Object>` out of VALUE_STRING token]"
  1. [JsonPath.stringAt("$.results.codebuild.id")]
    If I use array, it is failed in the build stage... (I'm using cdk pipeline to deploy this)
    error message is below
Cannot use JsonPath fields in an array, they must be used in objects



+ Extra Question
I found this during the search
https://stackoverflow.com/questions/70978385/aws-step-functions-wait-for-codebuild-to-finish

Can I use this `sync` on the `CallAwsService`? (Main 1... state is using `CallAwsService` also)
If yes, how can I use it..?
Or do I need to change the `CallAwsService` to `CodeBuildStartBuild`?

Solution

  • Could you let me know the correct format of batchGetBuilds parameters in CallAwsService?

    Use the States.Array intrinsic function. These CDK syntaxes are equivalent:

    parameters = {
      'Ids.$': 'States.Array($.results.codebuild.id)',
      Ids: JsonPath.stringAt('States.Array($.results.codebuild.id)'),
      Ids: JsonPath.array(JsonPath.stringAt('$.results.codebuild.id'))
    }
    

    Can I use this sync on the CallAwsService?

    No. The CallAwsService task implements the AWS SDK service integrations, which does not support .sync for CodeBuild actions. As of v2.15, CDK should throw an error if you pass the RUN_JOB (= .sync) pattern to CallAwsService. See this github issue for context.

    Or do I need to change the CallAwsService to CodeBuildStartBuild?

    Yes. CodeBuildStartBuild works as expected with the RUN_JOB integration pattern.