Terraform AWS Batch job definition parameters (aws_batch_job_definition)

Viewed 1408

I'm trying to understand how to do parameter substitution when lauching AWS Batch jobs. What I need to do is provide an S3 object key to my AWS Batch job. I haven't managed to find a Terraform example where parameters are passed to a Batch job and I can't seem to get it to work.

The documentation for aws_batch_job_definition contains the following example:

resource "aws_batch_job_definition" "test" {
  name = "tf_test_batch_job_definition"
  type = "container"

  container_properties = <<CONTAINER_PROPERTIES
{
    "command": ["ls", "-la"],
    "image": "busybox",
    "memory": 1024,
    "vcpus": 1,
    "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
      }
    ]
}
CONTAINER_PROPERTIES
}

Let's say that I would like for VARNAME to be a parameter, so that when I launch the job through the AWS Batch API I would specify its value. How is this accomplished? According to the docs for the aws_batch_job_definition resource, there's a parameter called parameters. However, this is a map and not a list, which I would have expected. What are the keys and values that are given in this map?

1 Answers

Terraform documentation on aws_batch_job_definition.parameters link is currently pretty sparse.

But, from running aws batch describe-jobs --jobs $job_id over an existing job in AWS, it appears the the parameters object expects a map:

{
    "jobs": [
        {
            "parameters": {},
            "container": {
                "image": "",
                "command": []
            }
        }
    ]
}

So, you can use Terraform to define batch parameters with a map variable, and then use CloudFormation syntax in the batch resource command definition like Ref::myVariableKey which is properly interpolated once the AWS job is submitted. Example:

variable "batch_params" {
  type  = map
  default = {
    bucketName = "defaultBucketName",
  }
}

resource "aws_batch_job_definition" "test" {
  name = "tf_test_batch_job_definition"
  type = "container"

  parameters = var.batch_params

  container_properties = <<CONTAINER_PROPERTIES
{
    "command": ["Ref::bucketName"],
    "image": "busybox",
    "memory": 1024,
    "vcpus": 1,
    "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
      }
    ]
}
CONTAINER_PROPERTIES
}
Related