Provides a Batch Job Definition resource.
resource "aws_batch_job_definition" "test" {
name = "tf_test_batch_job_definition"
type = "container"
container_properties = jsonencode({
command = ["ls", "-la"],
image = "busybox"
resourceRequirements = [
{
type = "VCPU"
value = "0.25"
},
{
type = "MEMORY"
value = "512"
}
]
volumes = [
{
host = {
sourcePath = "/tmp"
}
name = "tmp"
}
]
environment = [
{
name = "VARNAME"
value = "VARVAL"
}
]
mountPoints = [
{
sourceVolume = "tmp"
containerPath = "/tmp"
readOnly = false
}
]
ulimits = [
{
hardLimit = 1024
name = "nofile"
softLimit = 1024
}
]
})
}
resource "aws_batch_job_definition" "test" {
name = "tf_test_batch_job_definition_multinode"
type = "multinode"
node_properties = jsonencode({
mainNode = 0
nodeRangeProperties = [
{
container = {
command = ["ls", "-la"]
image = "busybox"
memory = 128
vcpus = 1
}
targetNodes = "0:"
},
{
container = {
command = ["echo", "test"]
image = "busybox"
memory = 128
vcpus = 1
}
targetNodes = "1:"
}
]
numNodes = 2
})
}
resource "aws_batch_job_definition" "test" {
name = " tf_test_batch_job_definition_eks"
type = "container"
eks_properties {
pod_properties {
host_network = true
containers {
image = "public.ecr.aws/amazonlinux/amazonlinux:1"
command = [
"sleep",
"60"
]
resources {
limits = {
cpu = "1"
memory = "1024Mi"
}
}
}
metadata {
labels = {
environment = "test"
}
}
}
}
}
resource "aws_iam_role" "ecs_task_execution_role" {
name = "tf_test_batch_exec_role"
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
resource "aws_batch_job_definition" "test" {
name = "tf_test_batch_job_definition"
type = "container"
platform_capabilities = [
"FARGATE",
]
container_properties = jsonencode({
command = ["echo", "test"]
image = "busybox"
jobRoleArn = "arn:aws:iam::123456789012:role/AWSBatchS3ReadOnly"
fargatePlatformConfiguration = {
platformVersion = "LATEST"
}
resourceRequirements = [
{
type = "VCPU"
value = "0.25"
},
{
type = "MEMORY"
value = "512"
}
]
executionRoleArn = aws_iam_role.ecs_task_execution_role.arn
})
}
The following arguments are required:
name
- (Required) Specifies the name of the job definition.type
- (Required) The type of job definition. Must be container
or multinode
.The following arguments are optional:
container_properties
- (Optional) A valid container properties provided as a single valid JSON document. This parameter is only valid if the type
parameter is container
.deregister_on_new_revision
- (Optional) When updating a job definition a new revision is created. This parameter determines if the previous version is deregistered
(INACTIVE
) or left ACTIVE
. Defaults to true
.node_properties
- (Optional) A valid node properties provided as a single valid JSON document. This parameter is required if the type
parameter is multinode
.eks_properties
- (Optional) A valid eks properties. This parameter is only valid if the type
parameter is container
.parameters
- (Optional) Specifies the parameter substitution placeholders to set in the job definition.platform_capabilities
- (Optional) The platform capabilities required by the job definition. If no value is specified, it defaults to EC2
. To run the job on Fargate resources, specify FARGATE
.propagate_tags
- (Optional) Specifies whether to propagate the tags from the job definition to the corresponding Amazon ECS task. Default is false
.retry_strategy
- (Optional) Specifies the retry strategy to use for failed jobs that are submitted with this job definition. Maximum number of retry_strategy
is 1
. Defined below.scheduling_priority
- (Optional) The scheduling priority of the job definition. This only affects jobs in job queues with a fair share policy. Jobs with a higher scheduling priority are scheduled before jobs with a lower scheduling priority. Allowed values 0
through 9999
.tags
- (Optional) Key-value map of resource tags. If configured with a provider default_tags
configuration block present, tags with matching keys will overwrite those defined at the provider-level.timeout
- (Optional) Specifies the timeout for jobs so that if a job runs longer, AWS Batch terminates the job. Maximum number of timeout
is 1
. Defined below.eks_properties
pod_properties
- The properties for the Kubernetes pod resources of a job. See pod_properties
below.pod_properties
containers
- The properties of the container that's used on the Amazon EKS pod. See containers below.dns_policy
- (Optional) The DNS policy for the pod. The default value is ClusterFirst
. If the host_network
argument is not specified, the default is ClusterFirstWithHostNet
. ClusterFirst
indicates that any DNS query that does not match the configured cluster domain suffix is forwarded to the upstream nameserver inherited from the node. For more information, see Pod's DNS policy in the Kubernetes documentation.host_network
- (Optional) Indicates if the pod uses the hosts' network IP address. The default value is true
. Setting this to false
enables the Kubernetes pod networking model. Most AWS Batch workloads are egress-only and don't require the overhead of IP allocation for each pod for incoming connections.metadata
- (Optional) Metadata about the Kubernetes pod.service_account_name
- (Optional) The name of the service account that's used to run the pod.volumes
- (Optional) Specifies the volumes for a job definition that uses Amazon EKS resources. AWS Batch supports emptyDir, hostPath, and secret volume types.containers
image
- The Docker image used to start the container.args
- An array of arguments to the entrypoint. If this isn't specified, the CMD of the container image is used. This corresponds to the args member in the Entrypoint portion of the Pod in Kubernetes. Environment variable references are expanded using the container's environment.command
- The entrypoint for the container. This isn't run within a shell. If this isn't specified, the ENTRYPOINT of the container image is used. Environment variable references are expanded using the container's environment.env
- The environment variables to pass to a container. See EKS Environment below.image_pull_policy
- The image pull policy for the container. Supported values are Always
, IfNotPresent
, and Never
.name
- The name of the container. If the name isn't specified, the default name "Default" is used. Each container in a pod must have a unique name.resources
- The type and amount of resources to assign to a container. The supported resources include memory
, cpu
, and nvidia.com/gpu
.security_context
- The security context for a job.volume_mounts
- The volume mounts for the container.eks_environment
name
- The name of the environment variable.value
- The value of the environment variable.eks_empty_dir
medium
- (Optional) The medium to store the volume. The default value is an empty string, which uses the storage of the node.size_limit
- The maximum size of the volume. By default, there's no maximum size defined.eks_host_path
path
- The path of the file or directory on the host to mount into containers on the pod.eks_secret
secret_name
- The name of the secret. The name must be allowed as a DNS subdomain name.optional
- (Optional) Specifies whether the secret or the secret's keys must be defined.retry_strategy
attempts
- (Optional) The number of times to move a job to the RUNNABLE
status. You may specify between 1
and 10
attempts.evaluate_on_exit
- (Optional) The evaluate on exit conditions under which the job should be retried or failed. If this parameter is specified, then the attempts
parameter must also be specified. You may specify up to 5 configuration blocks.evaluate_on_exit
action
- (Required) Specifies the action to take if all of the specified conditions are met. The values are not case sensitive. Valid values: retry
, exit
.on_exit_code
- (Optional) A glob pattern to match against the decimal representation of the exit code returned for a job.on_reason
- (Optional) A glob pattern to match against the reason returned for a job.on_status_reason
- (Optional) A glob pattern to match against the status reason returned for a job.timeout
attempt_duration_seconds
- (Optional) The time duration in seconds after which AWS Batch terminates your jobs if they have not finished. The minimum value for the timeout is 60
seconds.This resource exports the following attributes in addition to the arguments above:
arn
- The Amazon Resource Name of the job definition, includes revision (:#
).arn_prefix
- The ARN without the revision number.revision
- The revision of the job definition.tags_all
- A map of tags assigned to the resource, including those inherited from the provider default_tags
configuration block.In Terraform v1.5.0 and later, use an import
block to import Batch Job Definition using the arn
. For example:
import {
to = aws_batch_job_definition.test
id = "arn:aws:batch:us-east-1:123456789012:job-definition/sample"
}
Using terraform import
, import Batch Job Definition using the arn
. For example:
% terraform import aws_batch_job_definition.test arn:aws:batch:us-east-1:123456789012:job-definition/sample