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
Resources:
ServiceRouterDynamoTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Ref ServiceRouterTableName
AttributeDefinitions:
- AttributeName: serviceId
AttributeType: S
KeySchema:
- AttributeName: serviceId
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
TransformerLambda:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: EventRelayFunction
Handler: index.lambdaHandler
Runtime: nodejs18.x
CodeUri: function-code
Description: A function that receives events from an EventBridge rule and makes an HTTP call
Policies:
- EventBridgePutEventsPolicy:
EventBusName: !Ref EventBus
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "dynamodb:GetItem"
- "dynamodb:BatchGetItem"
- "dynamodb:Query"
- 'dynamodb:Scan'
Resource:
- Fn::Sub:
- arn:aws:dynamodb:AWS::Region:{AWS::AccountId}:table/${tableName}
- tableName: !Ref ServiceRouterDynamoTable
Environment:
Variables:
EVENT_BUS_NAME: !Ref EventBus
SERVICE_ROUTER_TABLE: !Ref ServiceRouterDynamoTable
VpcConfig:
SecurityGroupIds:
- !Ref SecurityGroupId
SubnetIds: !Ref ListOfSubnetIDs
EventBus:
Type: 'AWS::Events::EventBus'
Properties:
Name: !Ref EventBusName
EventBridgeRuleInbound:
Type: 'AWS::Events::Rule'
Properties:
EventBusName: !Ref EventBus
EventPattern:
detail-type:
- inbound-event-sent
Targets:
- Arn: !GetAtt TransformerLambda.Arn
Id: LambdaFunction
InputTransformer:
InputPathsMap:
"event-payload" : "$"
InputTemplate: |
{
"url" : "",
"method" : "",
"headers": {
"user-agent": "Amazon/EventBridge/CustomEvent",
"Content-Type": "application/json"
},
"return-response-event": true,
"event":
}
- Arn: !GetAtt CloudWatchLogsGroup.Arn
Id: LogGroup
EventBridgeRuleOutbound:
Type: 'AWS::Events::Rule'
Properties:
EventBusName: !Ref EventBus
EventPattern:
detail-type:
- outbound-event-sent
Targets:
- Arn: !GetAtt CloudWatchLogsGroup.Arn
Id: LogGroup
EventsToInvokeLambdaPolicy:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref TransformerLambda
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt EventBridgeRuleInbound.Arn
CloudWatchLogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /aws/events/event-bus-relay-logs
EventsToCloudWatchLogGroupPolicy:
Type: AWS::Logs::ResourcePolicy
Properties:
PolicyName: EventBridgeToCWLogsPolicy
PolicyDocument: !Sub
- >
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EventBridgetoCWLogsPolicy2",
"Effect": "Allow",
"Principal": {
"Service": [
"delivery.logs.amazonaws.com",
"events.amazonaws.com"
]
},
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"{logArn}"
],
"Condition": {
"ArnEquals": {"aws:SourceArn": [ "{inboundRuleArn}", "${outboundRuleArn}" ]}
}
}
]
}
- { logArn: !GetAtt CloudWatchLogsGroup.Arn, inboundRuleArn: !GetAtt EventBridgeRuleInbound.Arn, outboundRuleArn: !GetAtt EventBridgeRuleOutbound.Arn}
convert this code to terraform