AWS Eventbridge API Destination可以使用POST/PUT方法提交请求体。需要在事件规则中定义一个API目标,然后编写Lambda函数来处理请求。
示例代码:
{
"source": ["my.app"],
"detail-type": ["my.detail"],
"detail": {
"foo": ["bar"]
},
"targets": [
{
"id": "my-api",
"arn": "arn:aws:apigateway:us-west-2:123456789012:/restapis/xyz/stages/prod/resources/abc",
"input": "$.detail"
}
]
}
import json
import boto3
def lambda_handler(event, context):
# 获取请求体内容
request_body = json.dumps(event)
# 将请求发送到API目标
response = boto3.client('apigateway').put_rest_api(
restApiId='xyz',
mode='overwrite',
body=request_body
)
# 处理响应
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': response['id']
}
以上代码可以将事件规则中定义的请求体发送到API目标。注意,需要替换示例代码中的ARN和restApiId为自己的值。