0

I want to use API gateway with AWS batch

I already know how to use API Gateway with AWS lambda naad since there is a limit of 250 MB in lambda, I am not able to use it for integration and now trying AWS batch

1 Answers1

1

I am assuming that you want to use API Gateway + Lambda to create an endpoint for submitting job requests to AWS Batch.

In order to do so create the following Lambda function, which submits a job to AWS Batch. Replace "jobQueueArn" with the arn of your job queue. Integrate the Lambda function with API Gateway.

import boto3

def lambda_handler(event, context):

    client = boto3.client('batch')

    JOB_NAME = event['JobName']
    JOB_QUEUE = "jobQueueArn"
    JOB_DEFINITION = "a-job-definition:1"

    response = client.submit_job(
        jobName = JOB_NAME,
        jobQueue = JOB_QUEUE,
        jobDefinition = JOB_DEFINITION,
        parameters = { 'key': 'value' }
        )
    print(response)
    return 0

Parameters can be passed using the parameters input.

parameters (dict) -- Additional parameters passed to the job that replace parameter substitution placeholders that are set in the job definition. Parameters are specified as a key and value pair mapping. Parameters in a SubmitJob request override any corresponding parameter defaults from the job definition.

Make sure to attach the proper IAM policy to the Lambda Function's role.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "batch:SubmitJob"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
Yuke
  • 51
  • 5
  • Thanks Yuke, but i have more question as to how I can pass input parameters coming from API to batch. Lets say a json goes as input into the API { "value" : 123"} how will it take this value as an input, since earlier lambda used to handle it – arpit agrawal Jul 23 '19 at 08:37
  • Hi Arpit, you can, simply add parameters as input for Client.submit_job function as described in the docs https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/batch.html#Batch.Client.submit_job – Yuke Jul 23 '19 at 10:18
  • I am not able to get the aws batch output in lambda. How can i see the batch result in lambda logs ? – arpit agrawal Jul 30 '19 at 12:50