Applicable when
- API Gateway is used to receive events and put them into Firehose
- No complex request pre-processing required
Non-applicable when
- The request should be handler by lambda before inserting into Firehose
Implementation
The code below will show how to add API Gateway AWS integration to avoid using lambdas and directly insert data into Firehose
this.restAPI
.getResource('event', { // Create resource to receive events
defaultCorsPreflightOptions: {
allowMethods: ['POST'],
allowOrigins: Cors.ALL_ORIGINS,
allowHeaders: [...Cors.DEFAULT_HEADERS, 'Api-Key'], // Setup CORS and headers
},
})
.addMethod('POST', new Integration({
type: IntegrationType.AWS, // Add AWS Integration
uri: `arn:aws:apigateway:${this.region}:firehose:action/PutRecord`,
integrationHttpMethod: 'POST',
options: {
credentialsRole: new Role(this, withEnv('event-role'), { // Create role to put events
roleName: withEnv('event-role'),
assumedBy: new ServicePrincipal('apigateway.amazonaws.com'),
inlinePolicies: {
allowFirehosePutRecord: PolicyDocument.fromJson({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: ['firehose:PutRecord'],
Resource: '*',
},
],
}),
},
}),
requestTemplates: { // Template mapping - transform our ApiGateway request into proper format for Firehose
'application/json': `
#set( $key = $input.params().header['Api-Key'] )
#set( $keyname = "apiKey" )
#set( $body = $input.body )
#set( $bodyobj = $util.parseJson($body) )
#set( $event = $bodyobj.event )
#set( $eventname = "event" )
#set( $userid = $bodyobj.userId )
#set( $useridname = "userid" )
#set( $productid = $bodyobj.productId )
#set( $productidname = "productid" )
#set( $price = $bodyobj.price )
#set( $pricename = "price" )
#set( $currency = $bodyobj.currency )
#set( $currencyname = "currency" )
#set( $quote = '"' )
#set( $b64 = $util.base64Encode("{$quote$keyname$quote:$quote$key$quote,$quote$eventname$quote:$quote$event$quote,$quote$useridname$quote:$quote$userid$quote,$quote$productidname$quote: $quote$productid$quote,$quote$pricename$quote: $quote$price$quote,$quote$currencyname$quote:$quote$currency$quote}") )
{
"DeliveryStreamName": "<delivery-stream-name>",
"Record": { "Data": "$b64" }
}
`,
},
requestParameters: {
'integration.request.header.Content-Type': "'application/x-amz-json-1.1'",
},
integrationResponses: [
{
statusCode: '200',
responseTemplates: {
'application/json': '{"status": "OK"}',
},
},
],
},
}), {
methodResponses: [{ statusCode: '200' }],
requestModels: {
'application/json': ..., // Optional JSONSchema input event model
},
requestValidator: ..., // Optional request validator
});
Comments
0 comments
Please sign in to leave a comment.