How do I use Batch as a target for my EventBridge rule?

4 minute read
0

I want to use AWS Batch as a target for my Amazon EventBridge rule.

Resolution

The following example shows how to configure EventBridge to submit a Batch job with AWS Fargate Orchestration. The Batch job is submitted when a new Amazon Simple Storage Service (Amazon S3) bucket is created.

Create a job definition

Complete the following steps:

  1. Open the AWS Batch console.

  2. From the navigation bar, select your AWS Region.

  3. In the navigation pane, choose Job definitions, and then choose Create.

  4. Choose Fargate Orchestration type.

  5. For Job definition name, enter a unique name for your job definition.
    Note: You can use up to 128 letters (uppercase and lowercase), numbers, hyphens, and underscores in your unique name.

  6. For Execution role, choose an existing execution role or create a new one. Then, choose Next.

  7. For Container image, enter public.ecr.aws/amazonlinux/amazonlinux:latest.

  8. For Command, enter the following information:

    ["echo","Ref::S3bucket"]
  9. For vCPUs, enter 1.

  10. For Memory (GB), enter 2.

  11. Choose Next, and then choose Create job definition.

Get the ARN of your job queue and job definition

Complete the following steps:

  1. Open the AWS Batch console.
  2. In the navigation pane, choose Job queues.
  3. Choose your job queue.
  4. In the Job queue details section, copy the Queue ARN, and then save it for later.
  5. In the navigation pane, choose Job definitions, and then choose the job definition that you created.
  6. In the Job definition details section, copy the Job definition ARN, and then save it for later.

Turn on CloudTrail data events logging for objects in an S3 bucket

To invoke Batch jobs on Amazon S3 object-level operations, complete the steps in Turning on AWS CloudTrail event logging for S3 buckets and objects.

Create the EventBridge rule

Complete the following steps:

  1. Open the EventBridge console.
  2. Select EventBridge Rule and Create rule.
  3. Enter the following information:
    Enter a Name for your rule. You can optionally enter a Description.
    In Rule type, select Rule with an event pattern, and then choose Next.
    For Creation method, choose Use pattern form.
    For Event source, choose AWS services.
    For AWS Service, choose Simple Storage Service (S3).
    For Event type, choose Bucket-Level API Call via CloudTrail.
    Choose Any operation, and then choose Next.
  4. In the Select targets section, choose Batch job queue from the Target dropdown list.
  5. For Job queue, paste in the job queue ARN that you copied.
  6. For Job definition, paste in the job definition ARN that you copied earlier.
  7. For Job name, enter a name for your AWS Batch job.
  8. Choose either Create a new role for this specific resource or Use existing role.
    Note: For existing roles, the role must have an AWS Identity and Access Management (IAM) policy that allows the batch:SubmitJob action.
  9. In the Additional settings, Configure target input section, choose Input Transformer.
  10. Select Configure input transformer.
  11. In the Target input transformer input path, enter the S3 bucket values to be sent when the event is invoked:
    {"S3BucketNameValue":"$.detail.requestParameters.bucketName"}
    Note: Replace S3BucketNameValue with your own value.
  12. For the Template, enter the Parameters structure to be passed to the Batch job:
    {"Parameters" : {"S3bucket":"<S3BucketNameValue>"}}
    Note: Replace S3BucketNameValue with your own value. Replace S3bucket with the name of the parameter that you want to define in your AWS Batch job.
  13. Select Create rule.

Test the new rule that you created

Complete the following steps:

  1. Open the Amazon S3 console.
  2. Choose Create bucket.
  3. In the Bucket name field, type a unique DNS-compliant name for your new bucket.
  4. For Region, choose the same Region where you created the EventBridge rule.
  5. Choose Create.

Review your logs

Complete the following steps:

  1. Open the AWS Batch console.
  2. In the navigation pane, choose Jobs.
  3. Choose your job queue and check the recently submitted job with a Status of SUCCEEDED.
  4. Select the job and navigate to Logging. The log displays your bucket name as referenced from EventBridge.

Related information

Tutorial: Use input transformers to transform events in EventBridge

AWS OFFICIAL
AWS OFFICIALUpdated 7 months ago