This topic describes how to add API Gateway as a custom event source in the EventBridge console.
Prerequisites
Step 1: Create a backend service
-
Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > Backend Services.
-
On the Backend Services page, click Create Backend Service in the upper-right corner. In the dialog box, enter a backend service name, select EventBridge as the Type, and click OK.
-
In the backend service list, find the backend service you created and click Configure and Associated APIs in the Actions column.
-
On the Backend Service Definition page, click the Production tab. In the Basic Information section, click Create.
-
On the Define Backend Service page, select the Region and Event Buses of the custom event bus you created. Set a name and description for the event source, and then click Publish.
Step 2: Create an API group
-
In the left-side navigation pane, choose Manage APIs > API Groups.
-
On the API Groups page, click Create Group in the upper-right corner. Select an Instance, set the Group Name, and then click OK.
Select Shared Instance (VPC) as the instance type and set BasePath to
/.ImportantThe API group must be in the same region as the custom event bus that you created.
Step 3: Create and authorize an API
-
On the API Groups page, find the API group that you created in Step 2 and click API Management in the Actions column.
-
On the APIs page, click Create API.
-
In the Basic Information wizard step, configure the following settings and click Next.
Set parameters such as Group (for example,
test_EB), API Name (for example,testAPI), Security Authentication (for example, select Alibaba Cloud APP), AppCode Authentication (for example, select Allow AppCode Authentication (Header & Query)), and Signature Algorithm (for example, select HMAC_SHA256). The key settings are API Name, Security Authentication, and AppCode Authentication. You can also select API Options (such as prevent replay attacks, disable public access, or allow listing on Cloud Marketplace) and enter a Description. When you have finished, click Next. -
In the Define API Request wizard step, configure the following settings and click Next.
For Protocol, select HTTP and HTTPS. For Request Path, enter
/test/eb/1and select Match all sub-paths. For HTTP Method, select GET. For Request Mode, select Pass-through.NoteThis step defines how EventBridge calls your API. You need to specify the request type, protocol, request path, HTTP method, request mode, and request parameter definitions.
-
In the Define API Backend Service wizard step, select the Backend Service Type and Backend Services, and then click Next.
In this example, set Backend Service Type to EventBridge, set Backend Service to test_eb, and use the default Backend Timeout of
10000ms. -
(Optional) In the Define Response wizard step, set response examples and error response examples to generate API documentation, and then click Create.
-
-
After you create the API, click Publish in the Actions column to publish it to the Production environment.
-
On the APIs page, click the
icon in the Actions column of the target API, and then click Grant. -
In the Grant panel, set the authorization validity period, add the applications to authorize, and then click OK.
Select an environment for authorization (Production, Staging, or Test) and a validity period in the Authorization valid for section. Then, search for the target application, and from the results list, click + Add to add the application.
Step 4: Debug the API
-
On the APIs page, find the API that you created in Step 3: Create and authorize an API. In the Actions column, click Debug.
-
Set the debugging environment to Production and the Verification Method to Use AppCode. Configure the request parameters and click Send Request.
Event details
-
Log on to the EventBridge console. In the left-side navigation pane, click Event Bus.
-
On the Event Bus page, click the name of the target custom event bus.
-
In the left-side navigation pane, click Event Tracking. Select Query By Time Range or Query By Event ID as the query method, and then click Query.
-
Find the target event ID and click Event Detail in the Actions column.
The following example shows the event details:
{ "datacontenttype": "application/json", "aliyunaccountid": "1431999136******", "data": { "headers": { "Authorization": "APPCODE 0624849af6ab4020a272fe9f7b******", "Proxy-Uid": "X-Ca-e605e437-b65d-4e52-9f8c-515c55******", "X-Ca-Stage": "RELEASE", "User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_172)", "Accept-Encoding": "gzip,deflate", "Content-Type": "text/html" }, "path": "/test/eb/1", "pathParameters": {}, "method": "GET", "queryParameters": {}, "body": "" }, "aliyunoriginalaccountid": "143199913******", "source": "time-source-ad2a0405-9a2a-4f86-87f1-c0ef89******", "type": "eventbridge:Events:ApiGateway", "aliyunpublishtime": "2024-11-14T07:11:33.287Z", "specversion": "1.0", "aliyuneventbusname": "ad2a0405-9a2a-4f86-87f1-c0ef89******", "id": "324213ab-cae5-4049-89a4-560ead******", "time": "2024-11-14T07:11:33.000Z", "aliyunregionid": "cn-hangzhou", "aliyunpublishaddr": "172.20.XX.XX" }For more information about the parameters defined in the CloudEvents specification, see Event overview.
The following table describes the parameters within the data field.
Parameter
Type
Example
Description
headers
Map
{ "Authorization": "APPCODE b6e439f27045443ca58e9136ef20****", "X-Ca-Dashboard-Uid": "115964845466****", "X-Ca-Dashboard-Action": "DEBUG", "X-Ca-Stage": "RELEASE", "X-Ca-Dashboard-Role": "USER", "User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_172)", "Accept-Encoding": "gzip,deflate", "Content-Type": "text/html; charset=utf-8" }The HTTP request header.
path
String
/test/eb/1
The HTTP request path.
pathParameters
Map
None
The path parameters.
body
String/Map
None
The HTTP request body. If the Content-Type field of the HTTP request is application/json, the body is parsed as a JSON object. Otherwise, it is parsed as a plain string.
method
String
GET
The HTTP request method.
queryParameters
Map
None
The HTTP request query parameters, excluding the token.