Search code examples
c#amazon-web-services.net-coreaws-batch

Run an AWS Batch Job from C# API


I am trying to call an AWS Batch Job programmatically from a C# API. What we have done is create an AWS Batch job and now we want to run that job from a C# API which will provide parameters to the job.

How do I go on about doing this?

I have seen the documentation here: https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html

But I am struggling to make it work, seeing the documentation doesn't have examples and have only gone as far as create an AmazonBatchClient with the creds. Are there are sources where I can see a basic example on how to set up such a project?


Solution

  • From your note saying you've created a client, I'm going to assume you've added the AWSSDK.Batch NuGet package to your project.

    Using the Batch console, I walked through the Getting Started example to create a job definition with a couple of parameters (p1, p2) and then submitted the job with the code below. Hope this helps point you in the right direction. The API reference material for Batch can be found at https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html, which corresponds to https://docs.aws.amazon.com/sdkfornet/v3/apidocs/index.html?page=Batch/TSubmitJobRequest.html&tocid=Amazon_Batch_Model_SubmitJobRequest

    using Amazon.Batch;
    using Amazon.Batch.Model;
    
    // Load credentials from a local credential profile and create service client in
    // same region as batch job is configured
    var chain = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain();
    if (chain.TryGetAWSCredentials("steve-demo", out var awsCredentials))
    {
        var batchClient = new AmazonBatchClient(awsCredentials, Amazon.RegionEndpoint.USWest2);
    
        /* Alternate, if you're running with a credential profile named 'default', or running
           the code on an compute instance with an attached role vending temporary credentials,
           you can omit the credentials object and use:
           
           var batchClient = new AmazonBatchClient(Amazon.RegionEndpoint.USWest2);
         */
    
        var request = new SubmitJobRequest
        {
            JobName = "getting-started-job-definition",
    
            // definition and queue accept either simple name or, as shown here, the ARN of the resources
            JobDefinition = "arn:aws:batch:us-west-2:123456789012:job-definition/getting-started-job-definition:1",
            JobQueue = "arn:aws:batch:us-west-2:123456789012:job-queue/getting-started-job-queue",
    
            // parameter overrides for the job are passed in 
            // simple dictionary
            Parameters = new Dictionary<string, string>
            {
                { "p1", "override-value" }
            }
        };
    
        try
        {
            var response = await batchClient.SubmitJobAsync(request);
    
            Console.WriteLine($"Submitted job yielding job id {response.JobId}");
        }
        catch (AmazonBatchException e)
        {
            Console.WriteLine(e);
            throw;
        }
    }