Use Amazon EventBridge and AWS Lambda to respond to AdvertiseByoipCidr events and validate if the address space should be advertised.
AdvertiseByoipCidr
call triggers a Lambda function to run and validate whether or not the address space is expected to be advertised by checking for the address space in a list of allowed CIDRs in a DynamoDB table.AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Removes public route advertisements for CIDR ranges not permitted to be advertised.
Parameters:
NotificationEmail:
Type: String
Description: "Email where notifications should be sent when route advertisements have been blocked."
AllowedPattern: '[^@]+@[^@]+\.[^@]+'
Resources:
SubmitFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/function.py
Description: "Removes public route advertisements for CIDR ranges not permitted to be advertised."
Handler: function.lambda_handler
PackageType: "Zip"
Tracing: Active
Runtime: "python3.12"
Environment:
Variables:
DynamoDBTable: !Ref DynamoDBTable
SNSTopic: !Ref SNSTopic
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DynamoDBTable
- SNSPublishMessagePolicy:
TopicName: !GetAtt SNSTopic.TopicName
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ec2:WithdrawByoipCidr
Resource: '*'
Events:
AdvertiseByoipEvent:
Type: EventBridgeRule
Properties:
Pattern:
source:
- aws.ec2
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- ec2.amazonaws.com
eventName:
- AdvertiseByoipCidr
DynamoDBTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
SNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref NotificationEmail
Protocol: email
Visit the GitHub repo for this pattern.
git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/cloudtrail-eventbridge-lambda-dynamodb-sam
sam deploy
sam delete
.