AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A Lambda function connected to a VPC that relays events from an EventBridge event bus to a given URL
Parameters:
EventBusName:
Description: Please enter the event bus name that you want to create.
Type: String
ServiceRouterTableName:
Description: Please enter the service router table name that you want to create.
Type: String
SecurityGroupId:
Description: Please enter the VPC security group id
Type: String
ListOfSubnetIDs:
Description: Array of Subnet IDs in the Availability Zones
Type: ListAWS::EC2::Subnet::Id
Here is the provided AWS CloudFormation template translated to Terraform (HCL). Please note that you might need to modify certain sections if there's a specific setup required or to match your environment, such as variables and provider details:
hcl
provider "aws" {
# Configuration for the AWS Provider
region = "us-west-2" # Example region
}
resource "aws_dynamodb_table" "service_router_dynamo_table" {
name = var.service_router_table_name
hash_key = "serviceId"
billing_mode = "PROVISIONED"
read_capacity = 5
write_capacity = 5
attribute {
name = "serviceId"
type = "S"
}
}
resource "aws_lambda_function" "transformer_lambda" {
function_name = "EventRelayFunction"
handler = "index.lambdaHandler"
runtime = "nodejs18.x"
filename = "path/to/function-code.zip" # Update with actual file path
environment {
variables = {
EVENT_BUS_NAME = aws_events_event_bus.event_bus.name
SERVICE_ROUTER_TABLE = aws_dynamodb_table.service_router_dynamo_table.name
}
}
vpc_config {
security_group_ids = [var.security_group_id]
subnet_ids = var.list_of_subnet_ids
}
# IAM role and policy creation will be in separate resources
}
resource "aws_events_event_bus" "event_bus" {
name = var.event_bus_name
}
resource "aws_events_rule" "event_bridge_rule_inbound" {
name = "InboundRule"
event_bus_name = aws_events_event_bus.event_bus.name
event_pattern = jsonencode({
"detail-type" : ["inbound-event-sent"]
})
targets {
arn = aws_lambda_function.transformer_lambda.arn
id = "LambdaFunction"
input_transformer {
input_paths = {
"event-payload" = "$"
}
input_template = <<EOF
{
"url" : "",
"method" : "",
"headers": {
"user-agent": "Amazon/EventBridge/CustomEvent",
"Content-Type": "application/json"
},
"return-response-event": true,
"event": <event-payload>
}
EOF
}
}
}
resource "aws_events_rule" "event_bridge_rule_outbound" {
name = "OutboundRule"
event_bus_name = aws_events_event_bus.event_bus.name
event_pattern = jsonencode({
"detail-type" : ["outbound-event-sent"]
})
# Assuming a CloudWatch Logs target. Modify accordingly
targets {
arn = aws_cloudwatch_log_group.cloudwatch_logs_group.arn
id = "LogGroup"
}
}
resource "aws_lambda_permission" "events_to_invoke_lambda_policy" {
statement_id = "AllowExecutionFromEventBridge"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.transformer_lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_events_rule.event_bridge_rule_inbound.arn
}
resource "aws_cloudwatch_log_group" "cloudwatch_logs_group" {
name = "/aws/events/event-bus-relay-logs"
}
resource "aws_cloudwatch_log_resource_policy" "events_to_cloudwatch_log_group_policy" {
policy_name = "EventBridgeToCWLogsPolicy"
policy_document = data.aws_iam_policy_document.events_to_cw_logs_policy.json
}
data "aws_iam_policy_document" "events_to_cw_logs_policy" {
statement {
sid = "EventBridgetoCWLogsPolicy2"
effect = "Allow"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com", "events.amazonaws.com"]
}
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
resources = [aws_cloudwatch_log_group.cloudwatch_logs_group.arn]
condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = [aws_events_rule.event_bridge_rule_inbound.arn, aws_events_rule.event_bridge_rule_outbound.arn]
}
}
}
variable "event_bus_name" {
description = "Please enter the event bus name that you want to create."
type = string
}
variable "service_router_table_name" {
description = "Please enter the service router table name that you want to create."
type = string
}
variable "security_group_id" {
description = "Please enter the VPC security group id"
type = string
}
variable "list_of_subnet_ids" {
description = "Array of Subnet IDs in the Availability Zones"
type = list(string)
}
output "dynamodb_table_name" {
value = aws_dynamodb_table.service_router_dynamo_table.name
}
output "lambda_function_name" {
value = aws_lambda_function.transformer_lambda.function_name
}
output "event_bus_name" {
value = aws_events_event_bus.event_bus.name
}
output "cloudwatch_log_group_name" {
value = aws_cloudwatch_log_group.cloudwatch_logs_group.name
}
This is the basic translation of the template, but you might need to add provider information or modify it further for your specific use case. For example, you might need to add the actual source code for the Lambda function (referenced as filename in the aws_lambda_function resource above) and adjust any other specific settings for your environment. Also, make sure you check the latest Terraform documentation for any possible changes or deprecations regarding the AWS resources.