Submit AWS Batch Job using GoLang
Recently, I got a use case where I need to submit jobs to AWS Batch in Golang. There are not many examples available online, So I am sharing some insights on it.
GoLang is a popular language for the development of high performance and highly scalable systems.

AWS batch is a very effective and efficient way to run batch computing jobs and decrease the total computational load from your application. AWS Batch plans, schedules, and executes your batch computing workloads across the full range of AWS compute services and features, such as Amazon EC2 and Spot Instances.

First, we need the Golang AWS SDK into our libraries
go get -u github.com/aws/aws-sdk-go/...
You need to programmatic access to use the AWS SDK so follow this:
https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/setting-up.html#get-aws-credentials
Lets checkout the code now, To submit a simple job the code is as follows:
package mainimport (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/batch"
)const (
accessKeyValue = "AKIAZUEXAMPLE"
secret = "v1TuS/gGD1D6VEXAMPLE"
jobDefinition = "my-job:1"
jobQueue = "my-job"
)
//Note: Never!!! Hardcode your AWS credentials in the codevar batchClient *batch.Batch
func main() {
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: accessKeyValue,
SecretAccessKey: secret,
}),
Region: aws.String("us-east-1")},
)
if err != nil {
fmt.Println("Session error", err)
}
batchClient = batch.New(sess)
err = submitBatchJob("testjob")
if err != nil {
fmt.Println("Batch job error", err)
}
}func submitBatchJob(jobName) error {
input := &batch.SubmitJobInput{
JobDefinition: aws.String(jobDefinition),
JobName: aws.String(jobName),
JobQueue: aws.String(jobQueue),
}
_, err := batchClient.SubmitJob(input)
if err != nil {
return err
}
return nil
}
If your job has some Environment variables you can also specify it, Look at the code as follows:
package mainimport (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/batch"
)const (
accessKeyValue = "AKIAZUEXAMPLE"
secret = "v1TuS/gGD1D6VEXAMPLE"
environmentVariableOne = "KEY-1"
environmentVariableTwo = "KEY-2"
jobDefinition = "my-job:1"
jobQueue = "my-job"
)
//Note: Never!!! Hardcode your AWS credentials in the code
func main() {
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: accessKeyValue,
SecretAccessKey: secret,
}),
Region: aws.String("us-east-1")},)if err != nil {
fmt.Println("Session error", err)
}batchClient = batch.New(sess)err = submitBatchJobWithEnvVar("testjob", "myEvnVar1", "myEnvVar2")
if err != nil {
fmt.Println("Batch job error", err)
}
}func submitBatchJobWithEnvVar(jobName, varOne, varTwo string) error {
environmentVariables := createJobEnvironment(varOne, varTwo)
containerVariables := &batch.ContainerOverrides{
Environment: environmentVariables,
}
input := &batch.SubmitJobInput{
JobDefinition: aws.String(jobDefinition),
JobName: aws.String(jobName),
JobQueue: aws.String(jobQueue),
ContainerOverrides: containerVariables,
}
_, err := batchClient.SubmitJob(input)
if err != nil {
return err
}
return nil
}func createJobEnvironment(valueOne, valueTwo string) []*batch.KeyValuePair {
varOneKeyValue := batch.KeyValuePair{}
varOneKeyValue.SetName(environmentVariableOne)
varOneKeyValue.SetValue(valueOne) varTwoKeyValue := batch.KeyValuePair{}
varTwoKeyValue.SetName(environmentVariableTwo)
varTwoKeyValue.SetValue(valueTwo)return []*batch.KeyValuePair{
&varOneKeyValue,
&varTwotKeyValue,
}
}
I hope this will get you going, For more details reference these links: