Skip to content

Instantly share code, notes, and snippets.

@4l3j4ndr0
Last active October 19, 2024 17:00
Show Gist options
  • Select an option

  • Save 4l3j4ndr0/b915999004c651dc338ba6d36260e54e to your computer and use it in GitHub Desktop.

Select an option

Save 4l3j4ndr0/b915999004c651dc338ba6d36260e54e to your computer and use it in GitHub Desktop.
CloudFormation template to deploy a full serverless virus scan solution.
Transform: AWS::Serverless-2016-10-31
Resources:
VPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: 192.190.0.0/24
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: clam-av-scan-vpc
InternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Key: Name
Value: clam-av-scan-igw
AttachGateway:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicSubnet1:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
CidrBlock: 192.190.0.0/26
MapPublicIpOnLaunch: true
AvailabilityZone: !Select [0, !GetAZs ""]
Tags:
- Key: Name
Value: public-clam-av-1
NatEIP:
Type: "AWS::EC2::EIP"
Properties:
Domain: vpc
NatGateway:
Type: "AWS::EC2::NatGateway"
Properties:
AllocationId: !GetAtt NatEIP.AllocationId
SubnetId: !Ref PublicSubnet1
PrivateSubnet1:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
CidrBlock: 192.190.0.65/26
MapPublicIpOnLaunch: true
AvailabilityZone: !Select [0, !GetAZs ""]
Tags:
- Key: Name
Value: private-clam-av-1
PrivateSubnet2:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
CidrBlock: 192.190.0.129/26
MapPublicIpOnLaunch: true
AvailabilityZone: !Select [1, !GetAZs ""]
Tags:
- Key: Name
Value: private-clam-av-2
PrivateRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
PrivateRoute:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: "0.0.0.0/0"
NatGatewayId: !Ref NatGateway
PrivateSubnet1RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PrivateSubnet1
RouteTableId: !Ref PrivateRouteTable
PrivateSubnet2RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PrivateSubnet2
RouteTableId: !Ref PrivateRouteTable
RouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: RouteTable
PublicRoute:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref RouteTable
ClamAvFileSystem:
Type: AWS::EFS::FileSystem
Properties:
FileSystemTags:
- Key: Name
Value: clam-av-efs
ClamAvEFSAccessPoint:
Type: AWS::EFS::AccessPoint
Properties:
FileSystemId: !Ref ClamAvFileSystem
PosixUser:
Uid: "1001"
Gid: "1001"
RootDirectory:
CreationInfo:
OwnerGid: "1001"
OwnerUid: "1001"
Permissions: "755"
Path: /clam-av
ClamAvSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: clam-av-sg
GroupDescription: Security group for clamav virus scan serverless.
VpcId: !Ref VPC
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 2049
ToPort: 2049
CidrIp: 192.190.0.0/24
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: efs-clam-av-sg
EfsClamAvSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: efs-clam-av-sg
GroupDescription: Security group for EFS clamav virus scan serverless.
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 2049
ToPort: 2049
CidrIp: 192.190.0.0/24
Tags:
- Key: Name
Value: efs-clam-av-sg
EfsMountTarget1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref ClamAvFileSystem
SecurityGroups:
- !Ref EfsClamAvSecurityGroup
SubnetId: !Ref PrivateSubnet1
EfsMountTarget2:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref ClamAvFileSystem
SecurityGroups:
- !Ref EfsClamAvSecurityGroup
SubnetId: !Ref PrivateSubnet2
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: clam-av-function-execution-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: clam-av-function-execution-role
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AllowECRActions
Effect: Allow
Action:
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:BatchCheckLayerAvailability
- ecr:GetAuthorizationToken
Resource: "*"
- Sid: AllowS3Actions
Effect: Allow
Action:
- s3:GetObject
- s3:ListBucket
- s3:PutObjectTagging
Resource: "*"
- Sid: AllowEFSActions
Effect: Allow
Action:
- elasticfilesystem:ClientMount
- elasticfilesystem:ClientWrite
Resource: !GetAtt ClamAvFileSystem.Arn
- Sid: AllowEC2Actions
Effect: Allow
Action:
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
Resource: "*"
- Sid: AllowCloudWatchLogs
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- arn:aws:logs:*:*:*
VirusScanClamAvFunction:
Type: AWS::Serverless::Function
DependsOn:
- EfsMountTarget1
- EfsMountTarget2
Properties:
FunctionName: virus-scan-clam-av
Description: virus scan clamav
MemorySize: 3008
Timeout: 800
Tracing: Active
PackageType: Image
ImageUri: 590183999298.dkr.ecr.us-east-1.amazonaws.com/virus-scan-clam-av:latest
FileSystemConfigs:
- Arn: !GetAtt ClamAvEFSAccessPoint.Arn
LocalMountPath: /mnt/clam-av
Environment:
Variables:
EFS_MOUNT_PATH: /mnt/clam-av
EFS_DEF_PATH: virus_database/
POWERTOOLS_METRICS_NAMESPACE: serverless-clamscan
POWERTOOLS_SERVICE_NAME: virus-scan
VpcConfig:
SecurityGroupIds:
- !Ref ClamAvSecurityGroup
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
Role: !GetAtt LambdaExecutionRole.Arn
VirusScanClamAvDatabaseFunction:
Type: AWS::Serverless::Function
DependsOn:
- EfsMountTarget1
- EfsMountTarget2
Properties:
FunctionName: clam-av-database
Description: virus scan clamav
MemorySize: 3008
Timeout: 800
Tracing: Active
PackageType: Image
ImageUri: 590183999298.dkr.ecr.us-east-1.amazonaws.com/virus-scan-clam-av-database:latest
FileSystemConfigs:
- Arn: !GetAtt ClamAvEFSAccessPoint.Arn
LocalMountPath: /mnt/clam-av
Environment:
Variables:
EFS_MOUNT_PATH: /mnt/clam-av
EFS_DEF_PATH: virus_database/
POWERTOOLS_SERVICE_NAME: freshclam-update
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: rate(12 hours)
Name: ClamAvDatabaseUpdateEvent
Description:
Trigger Lambda VirusScanClamAvDatabaseFunction every 12 hours to
update the database virus scan.
VpcConfig:
SecurityGroupIds:
- !Ref ClamAvSecurityGroup
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
Role: !GetAtt LambdaExecutionRole.Arn
VirusScanClamAvLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${VirusScanClamAvFunction}
RetentionInDays: 30
VirusScanClamAvDatabaseLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${VirusScanClamAvDatabaseFunction}
RetentionInDays: 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment