Integrating Amazon EventBridge with Amazon ECS: A Comprehensive Guide

Integrating Amazon EventBridge with Amazon ECS: A Comprehensive GuideMore Info

This article is a contribution from Jamie Parker, Senior Software Development Engineer.

AWS has announced the latest support for Amazon API Gateway as an event target within Amazon EventBridge. This enhancement opens up new integration possibilities for web applications and services, allowing users to easily connect their infrastructure, SaaS applications, and APIs hosted on AWS.

With API Gateway now serving as a target for EventBridge, developers can easily direct events to their applications hosted on Amazon ECS, Amazon Elastic Kubernetes Service (EKS), or Amazon EC2. In this post, I will demonstrate how to create an event-driven application that operates on ECS Fargate, processing events from EventBridge via API Gateway integration.

EventBridge serves as a serverless event bus that simplifies connecting applications. It utilizes data from your own applications, integrated SaaS solutions, and AWS services, making it easier to build event-driven architectures by decoupling event producers from consumers. This decoupling enhances scalability, updates, and deployments—boosting developer agility and application resilience.

API Gateway allows developers to create, publish, and maintain secure APIs at any scale. When paired with EventBridge, API Gateway authenticates and authorizes API calls while acting as an HTTP proxy layer for integrating with other AWS services or third-party applications.

Previously, customers utilizing EventBridge could handle events in ECS through Amazon SNS or Amazon SQS or by directly triggering an ECS task. With API Gateway as a target, these prior methods are enhanced with additional features like authentication and rate limiting, enabling the creation of more resilient and functional integrations. API Gateway’s throttling limits the maximum number of events processed simultaneously, whereas EventBridge retries event delivery for up to 24 hours.

To illustrate these concepts, this blog post uses an ecommerce application as an example of custom integration, responsible for processing customer orders. The system components’ interaction is depicted in the diagram below. The application is hosted as an ECS service on AWS Fargate.

To ensure high availability, the application cluster is spread across subnets in various Availability Zones. The Application Load Balancer efficiently distributes incoming traffic among the cluster nodes. API Gateway handles request authentication and routing to the backend, while the application logic manages event reception and persistence in Amazon DocumentDB.

The order event is structured as follows:

{
  "version": "0",
  "region": "us-east-1",
  "account": "123456789012",
  "id": "4236e18e-f858-4e2b-a8e8-9b772525e0b2",
  "source": "ecommerce",
  "detail-type": "CreateOrder",
  "resources": [],
  "detail": {
    "order_id": "ce4fe8b7-9911-4377-a795-29ecca9d7a3d",
    "create_date": "2020-06-02T13:01:00Z",
    "items": [
      {
        "product_id": "b8575571-5e91-4521-8a29-4af4a8aaa6f6",
        "quantity": 1,
        "price": "9.99",
        "currency": "CAD"
      }
    ],
    "customer": {
      "customer_id": "5d22899e-3ff5-4ce0-a2a3-480cfce39a56"
    },
    "payment": {
      "payment_id": "fb563473-bef4-4965-ad78-e37c6c9c0c2a"
    },
    "delivery_address": {
      "street": "510 W Georgia St",
      "city": "Vancouver",
      "state": "BC",
      "country": "Canada",
      "zip_code": "V6B0M7"
    }
  }
}

Application Layer

The order processing application is built using a reactive architecture through Spring Boot. Adopting a reactive design enables the development of a scalable application capable of handling thousands of transactions per second from a single instance—a critical requirement for high throughput applications.

Resource Handler

The application defines an OrderResource, serving as the entry point for receiving and processing events from EventBridge. The handler logic unmarshals the event and retrieves order details from the event payload, persisting the order in DocumentDB through a dedicated DAO instance.

@Slf4j
@RequestMapping("/orders")
@RestController
public class EventResource {
 
    private final OrderRepository orderRepository;
 
    public EventResource(OrderRepository orderRepository) {
        this.orderRepository = Objects.requireNonNull(orderRepository);
    }
 
    @RequestMapping(method = RequestMethod.PUT)
    public Mono> onEvent(@Valid @RequestBody Event event) {
 
        log.info("Processing order {}", event.getDetail().getOrderId());
 
        return orderRepository.save(event.getDetail())
                .map(order -> ResponseEntity.created(UriComponentsBuilder.fromPath("/events/{id}")
                        .build(order.getOrderId())).build())
                .onErrorResume(error -> {
                    log.error("Unhandled error occurred", error);
                    return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
                });
    }
}

The handler is mapped to handle requests at the /orders path. Upon successful processing, it unmarshals the event payload and stores it in DocumentDB, responding with a 201 Created HTTP status code.

You can store EventBridge events in a document database like Amazon DocumentDB, which allows for direct JSON content storage. This example utilizes DocumentDB for easy document writing and general event content querying.

Prerequisites

To build and deploy the application, ensure you have AWS CDK and JDK 11 installed. Begin by cloning the GitHub repository, which contains example code and supporting infrastructure for AWS deployment.

Step 1: Create Amazon ECR Repository

Start by establishing a dedicated Amazon ECR repository for Docker images. The AWS CDK template is included in the application code repository for this purpose. Install Node.js dependencies for executing the CDK command:

cd ../eventbridge-integration-solution-aws-api-cdk
npm install

Next, compile the CDK TypeScript template:

npm run build

Then synthesize the CloudFormation stack:

cdk synth "*"

Now bootstrap the CloudFormation resources needed for remaining templates:

cdk bootstrap

Finally, deploy the stack that creates the Amazon ECR registry:

cdk deploy EventsRegistry

Step 2: Build the Application

Before deployment, build and upload the application to Amazon ECR. Compile the source code and create the application distribution:

cd ../eventbridge-integration-solution-aws-api
./gradlew clean build

Step 3: Containerizing the Application

The build system includes a task for containerizing artifacts and creating the Docker image. To create a new Docker image from the build artifact, execute the following command:

./gradlew dockerBuildImage

This generates the Dockerfile based on the provided settings and creates a new Docker image named eventbridge-integration-solution-aws-api.

Step 4: Upload the Image to Amazon ECR

You can now upload the image to Amazon ECR. First, log into the Amazon ECR registry through Docker. Remember to replace AWS_ACCOUNT_ID with your actual account number.

For more in-depth information on this topic, check out this excellent resource from chvnci.com. If you’re interested in additional insights, you can also visit this blog post for further engagement.

In conclusion, integrating Amazon EventBridge with Amazon ECS provides a powerful framework for developing event-driven applications, enhancing operational efficiency and scalability.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *