【问题标题】:Terraform AWS Batch job definition parameters (aws_batch_job_definition)Terraform AWS Batch 作业定义参数 (aws_batch_job_definition)
【发布时间】:2021-05-02 02:09:47
【问题描述】:

我试图了解在启动 AWS Batch 作业时如何进行参数替换。我需要做的是为我的 AWS Batch 作业提供一个 S3 对象密钥。我还没有找到将参数传递给批处理作业的 Terraform 示例,我似乎无法让它工作。

aws_batch_job_definition 的文档包含以下示例:

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
}

假设我希望 VARNAME 成为一个参数,这样当我通过 AWS Batch API 启动作业时,我会指定它的值。这是如何实现的?根据aws_batch_job_definition 资源的文档,有一个名为parameters 的参数。然而,这是一张地图,而不是我所期望的列表。此映射中给出的键和值是什么?

【问题讨论】:

    标签: terraform terraform-provider-aws aws-batch


    【解决方案1】:

    aws_batch_job_definition.parameterslink 上的 Terraform 文档目前非常少。

    但是,通过在 AWS 中的现有作业上运行 aws batch describe-jobs --jobs $job_id,参数对象似乎需要一个映射:

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

    因此,您可以使用 Terraform 使用映射变量定义批处理参数,然后在批处理资源命令定义中使用 CloudFormation 语法,例如 Ref::myVariableKey,一旦提交 AWS 作业,就会正确插入。示例:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 2014-02-09
      • 2021-08-01
      • 2020-07-13
      • 2021-01-28
      • 2017-06-15
      • 2022-01-26
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多