You can use event rules to filter events and route events to HTTP endpoints. This topic describes the prerequisites and procedure for routing custom events to HTTP endpoints. This topic also describes how to verify the result.
Before you start
Step 1: Create a custom event source
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region. On the Event Buses page, click the name of the custom event bus that you want to manage.
In the left-side navigation pane, click Event Sources and then click Add Event Source.
In the Add Custom Event Source panel, configure the Name and Description parameters, select Custom Application from the Event Provider drop-down list, and then click OK.
Step 2: Create an event rule
The event targets that you want to configure for an event rule must reside in the same region as the event rule.
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region. On the Event Buses page, click the name of the event bus that you want to manage.
In the left-side navigation pane, click Event Rules. On the page that appears, click Create Rule.
In the Create Rule panel, perform the following operations:
In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field. Then, click Next Step.
In the Configure Event Pattern step, set the Event Source Type parameter to Custom Event Source and the Event Source parameter to the custom event source that you created in Step 1: Create a custom event source. In the Pattern Content code editor, specify an event pattern and click Next Step.
For more information, see Event patterns.
In the Configure Targets step, configure an event target and click Create.
NoteYou can configure up to five event targets for an event rule.
Service Type: Select HTTP.
URL: Enter a URL.
Body: Select Complete Event.
For more information, see Event transformation.
Network Type: Select a network type.
Valid values:
Internet
VPC: If you select VPC, you must configure the VPC, vSwitch, and Security Group parameters.
ImportantThe security group must support the CIDR block of the vSwitch.
Token: the token that you can use to verify whether the requests that are sent by EventBridge are valid. The value of the Token parameter is also specified in the
x-eventbridge-signature-tokenHTTP header. The Token parameter appears only after you click Show Advanced Options.
Step 3: Publish an event
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
- In the top navigation bar, select a region.
- On the Event Buses page, find the event bus to which you want to publish an event and click Publish Event in the Operations column. Note You can publish events only to custom event buses in the EventBridge console.
- In the Publish Event to Custom Event Bus panel, select a custom event source from the Custom Event Source drop-down list, enter the event content in the Event Body code editor, and then click OK. For more information about the event parameters, see Overview.
Verify the result
You can use an HTTP gateway to receive the event and view the event content. The following code provides an example on how to use an HTTP gateway to receive an event:
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
@Slf4j
public class EventProcessingApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostMapping("/eventBridge/processEvent")
@ResponseBody
public String receiveMessage(@RequestBody String data) {
log.info("receiveEvent");
log.info(data);
return "received";
}
}