How do I set up AWS Batch batch computing workloads with Pulumi?
Here’s how you can set up AWS Batch batch computing workloads with Pulumi. You’ll define a compute environment, a job queue, and a job definition. The compute environment specifies the resources available for batch jobs. The job queue manages the job’s order of execution, and the job definition outlines how each job should be run.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create IAM role for AWS Batch
const batchServiceRole = new aws.iam.Role("batch_service_role", {
name: "batch_service_role",
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: {
Service: "batch.amazonaws.com",
},
Action: "sts:AssumeRole",
}],
}),
});
// Attach the necessary permissions for the batch service role
const batchServicePolicy = new aws.iam.RolePolicyAttachment("batch_service_policy", {
role: batchServiceRole.name,
policyArn: "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole",
});
// Create an instance role for AWS Batch to use
const batchInstanceRole = new aws.iam.Role("batch_instance_role", {
name: "batch_instance_role",
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: {
Service: "ec2.amazonaws.com",
},
Action: "sts:AssumeRole",
}],
}),
});
// Attach the necessary permissions for the batch instance role
const batchInstancePolicy = new aws.iam.RolePolicyAttachment("batch_instance_policy", {
role: batchInstanceRole.name,
policyArn: "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role",
});
// Create Compute Environment for AWS Batch
const myComputeEnv = new aws.batch.ComputeEnvironment("my_compute_env", {
computeEnvironmentName: "my_compute_environment",
serviceRole: batchServiceRole.arn,
type: "MANAGED",
computeResources: {
type: "EC2",
minVcpus: 0,
maxVcpus: 16,
desiredVcpus: 8,
instanceRole: batchInstanceRole.arn,
instanceTypes: ["m4.large"],
subnets: ["subnet-0123456789abcdef0"],
securityGroupIds: ["sg-0123456789abcdef0"],
},
});
// Create a Job Queue
const myJobQueue = new aws.batch.JobQueue("my_job_queue", {
name: "my_job_queue",
state: "ENABLED",
priority: 1,
computeEnvironments: [myComputeEnv.arn],
});
// Create a Job Definition
const myJobDef = new aws.batch.JobDefinition("my_job_def", {
name: "my_job_definition",
type: "container",
containerProperties: pulumi.jsonStringify({
image: "busybox",
vcpus: 1,
memory: 256,
command: [
"sleep",
"10",
],
jobRoleArn: batchInstanceRole.arn,
}),
});
export const batchComputeEnvironmentArn = myComputeEnv.arn;
export const batchJobQueueArn = myJobQueue.arn;
export const batchJobDefinitionArn = myJobDef.arn;
In this guide, we created IAM roles for the Batch service and for the instances, set up a managed compute environment, created a job queue for managing job priorities, and defined a simple job with a container that runs a command. Finally, we output the ARNs for convenience. This script sets up a fully functional AWS Batch environment that can be used to run and schedule compute workloads.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.