Skip to content

terraform-aws-modules/terraform-aws-batch

AWS Batch Terraform module

Terraform module which creates AWS Batch resources.

Usage

See examples directory for working examples to reference:

module "batch" {
  source = "terraform-aws-modules/batch/aws"

  compute_environments = {
    a_ec2 = {
      name_prefix = "ec2"

      compute_resources = {
        type           = "EC2"
        min_vcpus      = 4
        max_vcpus      = 16
        desired_vcpus  = 4
        instance_types = ["m5.large", "r5.large"]

        security_group_ids = ["sg-f1d03a88"]
        subnets            = ["subnet-30ef7b3c", "subnet-1ecda77b", "subnet-ca09ddbc"]

        # Note - any tag changes here will force compute environment replacement
        # which can lead to job queue conflicts. Only specify tags that will be static
        # for the lifetime of the compute environment
        tags = {
          # This will set the name on the Ec2 instances launched by this compute environment
          Name = "example"
          Type = "Ec2"
        }
      }
    }

    b_ec2_spot = {
      name_prefix = "ec2_spot"

      compute_resources = {
        type                = "SPOT"
        allocation_strategy = "SPOT_CAPACITY_OPTIMIZED"
        bid_percentage      = 20

        min_vcpus      = 4
        max_vcpus      = 16
        desired_vcpus  = 4
        instance_types = ["m4.large", "m3.large", "r4.large", "r3.large"]

        security_group_ids = ["sg-f1d03a88"]
        subnets            = ["subnet-30ef7b3c", "subnet-1ecda77b", "subnet-ca09ddbc"]

        # Note - any tag changes here will force compute environment replacement
        # which can lead to job queue conflicts. Only specify tags that will be static
        # for the lifetime of the compute environment
        tags = {
          # This will set the name on the Ec2 instances launched by this compute environment
          Name = "example-spot"
          Type = "Ec2Spot"
        }
      }
    }
  }

  # Job queus and scheduling policies
  job_queues = {
    low_priority = {
      name     = "LowPriorityEc2"
      state    = "ENABLED"
      priority = 1

      compute_environments = ["b_ec2_spot"]

      tags = {
        JobQueue = "Low priority job queue"
      }
    }

    high_priority = {
      name     = "HighPriorityEc2"
      state    = "ENABLED"
      priority = 99

      fair_share_policy = {
        compute_reservation = 1
        share_decay_seconds = 3600

        share_distribution = [{
          share_identifier = "A1*"
          weight_factor    = 0.1
          }, {
          share_identifier = "A2"
          weight_factor    = 0.2
        }]
      }

      tags = {
        JobQueue = "High priority job queue"
      }
    }
  }

  job_definitions = {
    example = {
      name           = "example"
      propagate_tags = true

      container_properties = jsonencode({
        command = ["ls", "-la"]
        image   = "public.ecr.aws/runecast/busybox:1.33.1"
        resourceRequirements = [
          { type = "VCPU", value = "1" },
          { type = "MEMORY", value = "1024" }
        ]
        logConfiguration = {
          logDriver = "awslogs"
          options = {
            awslogs-group         = "/aws/batch/example"
            awslogs-region        = "us-east-1"
            awslogs-stream-prefix = "ec2"
          }
        }
      })

      attempt_duration_seconds = 60
      retry_strategy = {
        attempts = 3
        evaluate_on_exit = {
          retry_error = {
            action       = "RETRY"
            on_exit_code = 1
          }
          exit_success = {
            action       = "EXIT"
            on_exit_code = 0
          }
        }
      }

      tags = {
        JobDefinition = "Example"
      }
    }
  }

  tags = {
    Terraform   = "true"
    Environment = "dev"
  }
}

Examples

Examples codified under the examples are intended to give users references for how to use the module(s) as well as testing/validating changes to the source code of the module. If contributing to the project, please be sure to make any appropriate updates to the relevant examples to allow maintainers to test your changes and to keep the examples up to date for users. Thank you!

Requirements

Name Version
terraform >= 1.0
aws >= 4.0

Providers

Name Version
aws >= 4.0

Modules

No modules.

Resources

Name Type
aws_batch_compute_environment.this resource
aws_batch_job_definition.this resource
aws_batch_job_queue.this resource
aws_batch_scheduling_policy.this resource
aws_iam_instance_profile.instance resource
aws_iam_role.instance resource
aws_iam_role.service resource
aws_iam_role.spot_fleet resource
aws_iam_role_policy_attachment.instance resource
aws_iam_role_policy_attachment.service resource
aws_iam_role_policy_attachment.spot_fleet resource
aws_iam_policy_document.instance data source
aws_iam_policy_document.service data source
aws_iam_policy_document.spot_fleet data source
aws_partition.current data source

Inputs

Name Description Type Default Required
compute_environments Map of compute environment definitions to create any {} no
create Controls if resources should be created (affects nearly all resources) bool true no
create_instance_iam_role Determines whether a an IAM role is created or to use an existing IAM role bool true no
create_job_definitions Determines whether to create the job definitions defined bool true no
create_job_queues Determines whether to create job queues bool true no
create_service_iam_role Determines whether a an IAM role is created or to use an existing IAM role bool true no
create_spot_fleet_iam_role Determines whether a an IAM role is created or to use an existing IAM role bool false no
instance_iam_role_additional_policies Additional policies to be added to the IAM role list(string) [] no
instance_iam_role_description Cluster instance IAM role description string null no
instance_iam_role_name Cluster instance IAM role name string null no
instance_iam_role_path Cluster instance IAM role path string null no
instance_iam_role_permissions_boundary ARN of the policy that is used to set the permissions boundary for the IAM role string null no
instance_iam_role_tags A map of additional tags to add to the IAM role created map(string) {} no
instance_iam_role_use_name_prefix Determines whether the IAM role name (instance_iam_role_name) is used as a prefix string true no
job_definitions Map of job definitions to create any {} no
job_queues Map of job queue and scheduling policy defintions to create any {} no
service_iam_role_additional_policies Additional policies to be added to the IAM role list(string) [] no
service_iam_role_description Batch service IAM role description string null no
service_iam_role_name Batch service IAM role name string null no
service_iam_role_path Batch service IAM role path string null no
service_iam_role_permissions_boundary ARN of the policy that is used to set the permissions boundary for the IAM role string null no
service_iam_role_tags A map of additional tags to add to the IAM role created map(string) {} no
service_iam_role_use_name_prefix Determines whether the IAM role name (service_iam_role_name) is used as a prefix bool true no
spot_fleet_iam_role_additional_policies Additional policies to be added to the IAM role list(string) [] no
spot_fleet_iam_role_description Spot fleet IAM role description string null no
spot_fleet_iam_role_name Spot fleet IAM role name string null no
spot_fleet_iam_role_path Spot fleet IAM role path string null no
spot_fleet_iam_role_permissions_boundary ARN of the policy that is used to set the permissions boundary for the IAM role string null no
spot_fleet_iam_role_tags A map of additional tags to add to the IAM role created map(string) {} no
spot_fleet_iam_role_use_name_prefix Determines whether the IAM role name (spot_fleet_iam_role_name) is used as a prefix string true no
tags A map of tags to add to all resources map(string) {} no

Outputs

Name Description
compute_environments Map of compute environments created and their associated attributes
instance_iam_instance_profile_arn ARN assigned by AWS to the instance profile
instance_iam_instance_profile_id Instance profile's ID
instance_iam_instance_profile_unique Stable and unique string identifying the IAM instance profile
instance_iam_role_arn The Amazon Resource Name (ARN) specifying the IAM role
instance_iam_role_name The name of the IAM role
instance_iam_role_unique_id Stable and unique string identifying the IAM role
job_definitions Map of job defintions created and their associated attributes
job_queues Map of job queues created and their associated attributes
scheduling_policies Map of scheduling policies created and their associated attributes
service_iam_role_arn The Amazon Resource Name (ARN) specifying the IAM role
service_iam_role_name The name of the IAM role
service_iam_role_unique_id Stable and unique string identifying the IAM role
spot_fleet_iam_role_arn The Amazon Resource Name (ARN) specifying the IAM role
spot_fleet_iam_role_name The name of the IAM role
spot_fleet_iam_role_unique_id Stable and unique string identifying the IAM role

License

Apache-2.0 Licensed. See LICENSE.