This article is contributed by Alex Martinez, Senior Serverless Specialist. AWS Step Functions has now incorporated integration with Amazon EventBridge, simplifying the process of generating events during workflows.
With AWS Step Functions, you can create resilient serverless orchestration workflows utilizing AWS services such as AWS Lambda, Amazon SNS, Amazon DynamoDB, and more. The service offers a comprehensive history of executions for each state machine, accessible through the AWS Management Console or Amazon CloudWatch Logs.
Amazon EventBridge enables event routing between AWS services, integrated SaaS applications, and your own applications. Event producers can publish events to an event bus, which employs rules to determine event destinations. These rules can specify multiple targets, including other AWS services or API destinations. This model facilitates the development of scalable, distributed serverless applications by managing event routing and filtering.
What’s New?
The latest Step Functions integration with EventBridge introduces a new resource type – arn:aws:states:::events:putEvents
. This resource is compatible with both standard and Express Workflows, allowing users to publish events to a designated event bus directly from a workflow. There are two integration patterns available:
- Request-Response: This pattern allows Step Functions to move to the next state immediately after receiving an HTTP response when calling a service. It is supported by both standard and Express Workflows. An example to send an EventBridge custom event looks like this:
Type: Task
Resource: 'arn:aws:states:::events:putEvents'
Parameters:
Entries:
- Detail:
Message: 'Hello from Step Functions!'
DetailType: MyDetailType
EventBusName: MyEventBusName
Source: MySource
Next: NEXT_STATE
- Wait-for-callback: This pattern entails calling a service with a task token, where Step Functions waits until the token is returned with a payload. This option is available only for Standard Workflows. Here’s how to send an EventBridge custom event:
Type: Task
Resource: 'arn:aws:states:::events:putEvents.waitForTaskToken'
Parameters:
Entries:
- Detail:
Message: 'Hello from Step Functions!'
TaskToken.$: $$.Task.Token
DetailType: MyDetailType
EventBusName: MyEventBusName
Source: MySource
Next: NEXT_STATE
The new integration supports several Amazon States Language parameter fields, including Detail, DetailType, EventBusName, Source, Time (optional), and Resources (optional). For more information on EventBridge fields and concepts, check out this insightful blog post, here.
Getting Started
You can configure this integration using AWS Serverless Application Model (AWS SAM), the AWS Command Line Interface (AWS CLI), AWS CloudFormation, or directly through the AWS Management Console. To begin with the AWS Management Console:
- Navigate to the Step Functions console.
- Select “Run a sample project” and choose “Send a custom event to EventBridge.”
Once you run the sample project, the Definition section will display the Amazon States Language (ASL) that makes up the example workflow, including the new EventBridge resource and its parameters.
After reviewing the example Definition, click Next and choose “Deploy resources.” This action will deploy a standard Step Functions workflow, an EventBridge event bus, and a rule that routes events from Step Functions to three targets: an AWS Lambda function, an Amazon SNS topic, and an Amazon SQS queue. Additionally, an IAM role will be created with the necessary permissions to allow Step Functions to send messages to the event bus. Resource policies will also be established to enable EventBridge to send events to the respective targets.
Running the Workflow
Select the newly created state machine from the Step Functions console and click “Start execution.” You can leave the input field blank and click “Start execution” again. Then, choose the “Send a custom event” step and navigate to the Step output tab to check if events are successfully delivered to EventBridge.
Access Control
The EventBridge integration facilitates AWS Identity and Access Management (IAM) authentication and authorization. This includes IAM roles, policies, and tags, which offer flexible access controls applicable to the creation or access of EventBridge resources. For more information on IAM roles and policies, refer to this authoritative resource here.
Microservices Example
In many financial institutions, Know Your Customer (KYC) guidelines are essential for identifying customers and establishing risk profiles. This example illustrates how Step Functions can be utilized to model a KYC workflow and integrate various business domains, such as Accounts and Customer Service. The event-driven process promotes loose coupling, isolation, and autonomy, utilizing a low-code integration approach through the new EventBridge service integration.
The event flow commences with the accounts system emitting an event signifying a new account request. This event triggers the workflow in the KYC service, which verifies the customer’s identity and risk profile. Upon completion of the identity check, the KYC process emits an “Identity check completed” event. Depending on the outcome of the risk profile assessment, the KYC process publishes two events to signal approval or rejection of the new account request. These events are consumed by the Accounts and Customer Service domains, each having rules defined on a central event bus to process the results of the KYC processing.
By leveraging the new EventBridge service integrations, developers can effectively model events within state machines at any execution stage. This is a valuable method for signaling state transitions to interested consumers. For long-running processes, it eliminates the need for consumers to await notifications in a serious tone, making the overall interaction more seamless and efficient. For more insights into similar topics, check out this excellent resource on Reddit here.
Leave a Reply