Call Lambda with Task states

A Task state does the work named in its Resource field. Here it calls the Data Checking function. The Parameters field builds the function input from the state input: a key ending in .$ takes its value from a JSONPath, so "name.$": "$.application.name" passes the applicant name.

  1. Get the ARN of the Data Checking function from the stack outputs:
cd ~/workshop-dir
REGION=$(grep region samconfig.toml | awk -F\= '{gsub(/"/, "", $2); gsub(/ /, "", $2); print $2}')
STACK_NAME=$(grep stack_name samconfig.toml | awk -F\= '{gsub(/"/, "", $2); gsub(/ /, "", $2); print $2}')
aws cloudformation describe-stacks --region $REGION --stack-name $STACK_NAME --query 'Stacks[0].Outputs[?OutputKey==`DataCheckingFunctionArn`].OutputValue' --output text
  • (1) Copy the ARN.

Function ARN

  1. Open Process_New_Account_Applications and choose Edit (1).

    Edit

  2. In Code, replace the definition with the JSON below.

    • (1)(2) Resource still holds a placeholder.
    • (3) The graph marks both Task states as invalid.
{
    "StartAt": "Check Name",
    "States": {
        "Check Name": {
            "Type": "Task",
            "Parameters": {
                "command": "CHECK_NAME",
                "data": {
                    "name.$": "$.application.name"
                }
            },
            "Resource": "REPLACE_WITH_DATA_CHECKING_LAMBDA_ARN",
            "Next": "Check Address"
        },
        "Check Address": {
            "Type": "Task",
            "Parameters": {
                "command": "CHECK_ADDRESS",
                "data": {
                    "address.$": "$.application.address"
                }
            },
            "Resource": "REPLACE_WITH_DATA_CHECKING_LAMBDA_ARN",
            "Next": "Approve Application"
        },
        "Approve Application": {
            "Type": "Pass",
            "End": true
        }
    }
}

Placeholder

  1. Replace both REPLACE_WITH_DATA_CHECKING_LAMBDA_ARN with the ARN from step 1 (1)(2). The states become Lambda: Invoke (3).

    ARN set

  2. Choose Save (1). The role is not changed: the console does not add permissions to a role you chose yourself. Then choose Execute (2).

    Saved

  3. Use the same input as before (1) and choose Start execution (2).

    Start

  4. This time the execution Failed (1).

    Failed

  5. Expand Cause (1). The role FCJ-SFN-Console-Role is not allowed to perform lambda:InvokeFunction on the Data Checking function.

    Cause

When a state machine runs, it assumes its IAM role, and every call it makes is checked against that role. The role has no permission to invoke the function yet. You fix this in the next step.