Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

Amazon EventBridge to AWS Batch

Created with SnapAmazon EventBridgeAWS Batch

Create an EventBridge event bus and route events to AWS Batch.

The AWS SAM template deploys the resources and the IAM permissions required to run the application.
The EventBridge rule specified in `template.yaml` filters the events based upon the criteria in the `EventPattern` section. When matching events are sent to EventBridge that trigger the rule, it triggers 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]]]] 
  

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/eventbridge-batch-sam

Deploy

sam deploy --guided


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the stack: sam delete.

Biswanath Mukherjee

Presented by Biswanath Mukherjee

Sr. Solutions Architect working at AWS India.

Follow on LinkedIn