Skip to content

Instantly share code, notes, and snippets.

@ohaval
Last active December 3, 2022 14:46
Show Gist options
  • Save ohaval/0984bc79864a908536870c5b98699891 to your computer and use it in GitHub Desktop.
Save ohaval/0984bc79864a908536870c5b98699891 to your computer and use it in GitHub Desktop.
An example of Terraform code shows how to integrate AWS API Gateway V2 with Event Bridge
# 3 important steps:
#
# A) Create a role for the integration (allowing PutEvents)
# B) Create the integration resource (specify that role)
# C) Create a route targeting that integration
resource "aws_apigatewayv2_integration" "api-gw-v2-integration-to-event-bridge" {
api_id = aws_apigatewayv2_api.some-api-gw-v2.id
integration_type = "AWS_PROXY"
integration_subtype = "EventBridge-PutEvents"
request_parameters = {
EventBusName = "some-bus-name"
Detail = jsonencode({
key1 = "val1"
})
DetailType = "some-detail-type"
Source = "some-source"
}
credentials_arn = aws_iam_role.api-gw-integration-to-event-bridge-role.arn
}
resource "aws_iam_role" "api-gw-v2-integration-to-event-bridge-role" {
name = "api-gw-v2-integration-to-event-bridge-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "apigateway.amazonaws.com"
}
}
]
})
inline_policy {
name = "api-gw-v2-integration-to-event-bridge-inline-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "events:PutEvents"
Effect = "Allow"
Resource = "arn:aws:events:*:*:event-bus/some-bus"
}
]
})
}
}
resource "aws_apigatewayv2_route" "api-gw-v2-route" {
api_id = aws_apigatewayv2_api.some-api-gw-v2.id
route_key = "GET /testroute"
target = "integrations/${aws_apigatewayv2_integration.api-gw-v2-integration-to-event-bridge.id}"
}
# More on API Gateway subtypes integration: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-aws-services-reference.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment