It enables real-time processing of user requests. This setup allows API Gateway to invoke a Step Function synchronously, wait for its completion, and return the result to the user.
Part 1: Setting Up the Express Workflow
Create a State Machine:
Navigate to the AWS Step Functions console.
Click on "Create state machine."
Choose "Design your workflow visually."
Select "Express" as the type.
Click "Next."
Design the Workflow:
Use the Workflow Studio to build your state machine by dragging and dropping elements.
Configure each step as needed.
After designing, click "Next."
Review the Definition:
Examine the generated definition for accuracy.
Make any necessary adjustments.
Click "Next."
Configure Settings:
Assign a unique name to the state machine.
Specify an IAM role that grants the state machine access to required resources.
Enable logging and X-Ray tracing if desired.
Add tags as needed.
Click "Create state machine."
Part 2: Setting Up the REST API
Create the API:
Go to the API Gateway console.
Click "Create API."
Under "REST API," click "Build."
Select "New API."
Provide a name, description (optional), and choose the endpoint type.
Click "Create API."
Configure the POST Method:
In the new API, select "Resources."
Create a "POST" method.
Set the integration type to "AWS Service."
Choose the appropriate AWS region.
Select "Step Functions" as the AWS Service.
Set the action to "StartSyncExecution."
Provide an execution role ARN that allows API Gateway to invoke the Step Function.
Set Up Mapping Templates:
In the "Integration Request," add a mapping template for "application/json."
Use the following template, replacing REPLACE_STEPFUNCTION_ARN with your Step Function's ARN:
shell
Copy
Edit
#set($body= $input.json('$'))
#set($inputRoot='{ "data" :'+$body+',"apiInfo":{"httpMethod" :"'+ $context.httpMethod+'", "apiKey":"'+ $context.identity.apiKey+'"}}')
#set($apiData=$util.escapeJavaScript($inputRoot))
#set($apiData=$apiData.replaceAll("\\'","'"))
{
"input" :"$apiData",
"stateMachineArn": "REPLACE_STEPFUNCTION_ARN"
}
This template structures the input data and includes API information such as the HTTP method and API key.
Configure Method Request and Integration Response:
In "Method Request," set "API Key Required" to true.
In "Integration Response," expand the default method response.
Add a mapping template for "application/json" with the following content:
swift
Copy
Edit
#set ($bodyObj = $util.parseJson($input.body))
#if ($bodyObj.status == "SUCCEEDED")
$bodyObj.output
#elseif ($bodyObj.status == "FAILED")
#set($context.responseOverride.status = 500)
{
"cause": "$bodyObj.cause",
"error": "$bodyObj.error"
}
#else
#set($context.responseOverride.status = 500)
$input.body
#end
This template processes the Step Function's response, handling success and failure scenarios appropriately.
Deploy the API:
Deploy the API to a stage of your choice.
Set Up API Keys and Usage Plans:
Create an API key in the API Gateway console.
Define a usage plan with appropriate throttling and quota settings.
Associate the API key with the usage plan and the deployed API stage.
By following these steps, you can integrate API Gateway's REST API with AWS Step Functions' Synchronous Express Workflow, enabling efficient and real-time processing of API requests.