Create an EventBridge event bus and route events to AWS Batch.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
(uksb-1tthgi812) (tag:eventbridge-batch-sam)
Resources:
# Create a custom Event Bus
EventBus:
Type: AWS::Events::EventBus
Properties:
Name: "my-event-bus"
# Create Event Rule that filters events based on pattern and triggers AWS Batch for matching events
EventRule:
Type: AWS::Events::Rule
Properties:
Name: "my-event-rule"
EventBusName: !GetAtt EventBus.Arn
Description: "Send matched events to AWS Batch"
Targets:
- Arn: !Ref JobQueue
Id: "my-batch-target"
RoleArn: !GetAtt EventBridgeIAMrole.Arn
BatchParameters:
JobDefinition: !Ref JobDefinition
JobName: 'my-job'
RetryStrategy:
Attempts: 2
EventPattern:
source:
- "my-application"
# Create IAM Role that allows EventBridge to invoke AWS Batch
EventBridgeIAMrole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: !Sub events.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: AWSBatchSubmitJob
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'batch:SubmitJob'
Resource:
- !Ref JobQueue
- !Ref JobDefinition
- !Join
- ''
- - 'arn:'
- !Ref AWS::Partition
- ':batch:'
- !Ref AWS::Region
- ':'
- !Ref AWS::AccountId
- ':job/*'
# Create VPC, subnets, security groups, and single Batch Job Queue
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
RouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
VPCGatewayAttachment:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
SecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: EC2 Security Group for instances launched in the VPC by Batch
VpcId: !Ref VPC
SecurityGroupEgress:
- Description: Allow all outbound traffic
IpProtocol: "-1"
CidrIp: 0.0.0.0/0
Subnet:
Type: 'AWS::EC2::Subnet'
Properties:
CidrBlock: 10.0.0.0/24
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
Route:
Type: 'AWS::EC2::Route'
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
SubnetRouteTableAssociation:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
RouteTableId: !Ref RouteTable
SubnetId: !Ref Subnet
BatchServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: batch.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole'
IamInstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Roles:
- !Ref EcsInstanceRole
EcsInstanceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2008-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- >-
arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
JobDefinition:
Type: 'AWS::Batch::JobDefinition'
Properties:
Type: container
ContainerProperties:
Image: !Join
- ''
- - 137112412989.dkr.ecr.
- !Ref 'AWS::Region'
- '.amazonaws.com/amazonlinux:latest'
Vcpus: 2
Memory: 2000
Command:
- echo
- Hello World!
RetryStrategy:
Attempts: 1
JobQueue:
Type: 'AWS::Batch::JobQueue'
Properties:
Priority: 1
ComputeEnvironmentOrder:
- Order: 1
ComputeEnvironment: !Ref ComputeEnvironment
ComputeEnvironment:
Type: 'AWS::Batch::ComputeEnvironment'
Properties:
Type: MANAGED
ComputeResources:
Type: EC2
MinvCpus: 0
DesiredvCpus: 0
MaxvCpus: 64
InstanceTypes:
- optimal
Subnets:
- !Ref Subnet
SecurityGroupIds:
- !Ref SecurityGroup
InstanceRole: !Ref IamInstanceProfile
ServiceRole: !Ref BatchServiceRole
Outputs:
EventBusName:
Value: !GetAtt EventBus.Arn
JobQueueName:
Value: !Select [1, !Split ['/', !Ref JobQueue]]
JobDefinitionName:
Value: !Select [0, !Split [":", !Select [1, !Split ['/', !Ref JobDefinition]]]]
Visit the GitHub repo for this pattern.
git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/eventbridge-batch-sam
sam deploy --guided
sam delete
.