【问题标题】:Terraform 14 template_file and null_resource issueTerraform 14 template_file 和 null_resource 问题
【发布时间】:2021-03-23 18:35:28
【问题描述】:

我正在尝试通过 local-exec-provisioner 使用 null 资源,以便使用模板文件在负载均衡器上启用 s3 存储桶日志记录。然而,terraform 文件和模板文件 (lb-to-s3-log.tpl) 都在同一目录 "/modules/lb-to-s3-log" 上收到错误。 Terraform 文件看起来是这样的:

data "template_file" "lb-to-s3-log" {
  template = file(".//modules/lb-to-s3-log/lb-to-s3-log.tpl")
  vars = {
    X_INFO1    = var.INFO1
    X_INFO2    = var.INFO2
    X_INFO3    = var.INFO3
  }
}
resource "null_resource" "lb-to-s3-log" {
  provisioner "local-exec" {
    command = "aws elb modify-load-balancer-attributes --load-balancer-name ${var.LOAD_BALANCER_NAME[0]} --load-balancer-attributes ${data.template_file.lb-to-s3-log.rendered}"
  }
} 

地点:

var.INFO1 = test1
var.INFO2 = test2
var.INFO3 = test3

模板 (TPL) 文件包含:

{
  "AccessLog": {
    "Enabled": true,
    "S3BucketName": "${X_INFO1}-${X_INFO2}-${X_INFO3}-logs",
    "EmitInterval": 5,
    "S3BucketPrefix": "${X_INFO1}-${X_INFO2}-${X_INFO3}-logs"
  }
}

收到错误消息:

Error: Error running command 'aws elb modify-load-balancer-attributes --load-balancer-name awseb-e-5-AWSEBLoa-ABCDE0FGHI0V --load-balancer-attributes {
  "AccessLog": {
    "Enabled": true,
    "S3BucketName": "test1-test2-test3-logs",
    "EmitInterval": 5,
    "S3BucketPrefix": "test1-test2-test3-logs"
  }
}
': exit status 2. Output: 
Error parsing parameter '--load-balancer-attributes': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
JSON received: {
/bin/sh: line 1: AccessLog:: command not found
/bin/sh: line 2: Enabled:: command not found
/bin/sh: line 3: S3BucketName:: command not found
/bin/sh: line 4: EmitInterval:: command not found
/bin/sh: line 5: S3BucketPrefix:: command not found
/bin/sh: -c: line 6: syntax error near unexpected token `}'
/bin/sh: -c: line 6: `  }'

问题/问题: 模板文件成功更新了变量赋值(X_INFO1、X_INFO2、X_INFO23)。似乎问题出在 aws cli 命令的 ${data.template_file.lb-to-s3-log.rendered} 上。

当我尝试将文件从 lb-s3log.tpl 替换为 lb-s3log.json 时出现同样的错误。

我使用的是 Terraform v0.14,我按照documentation 为亚马逊经典负载均衡器的日志存储启用 s3 存储桶的过程

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    发生错误是因为您需要将JSON to be escaped on the command linewrite the JSON as a file and then use file:// to refer to it 格式化。

    用单引号包裹你的 JSON 应该足以避免 shell 问题:

    data "template_file" "lb-to-s3-log" {
      template = file(".//modules/lb-to-s3-log/lb-to-s3-log.tpl")
      vars = {
        X_INFO1 = var.INFO1
        X_INFO2 = var.INFO2
        X_INFO3 = var.INFO3
      }
    }
    
    resource "null_resource" "lb-to-s3-log" {
      provisioner "local-exec" {
        command = "aws elb modify-load-balancer-attributes --load-balancer-name ${var.LOAD_BALANCER_NAME[0]} --load-balancer-attributes '${data.template_file.lb-to-s3-log.rendered}'"
      }
    }
    

    如果您愿意,可以使用local_file 资源来呈现文件:

    data "template_file" "lb-to-s3-log" {
      template = file(".//modules/lb-to-s3-log/lb-to-s3-log.tpl")
      vars = {
        X_INFO1 = var.INFO1
        X_INFO2 = var.INFO2
        X_INFO3 = var.INFO3
      }
    }
    
    resource "local_file" "elb_attributes" {
      content  = data.template_file.lb-to-s3-log.rendered
      filename = "${path.module}/elb-attributes.json"
    }
    
    resource "null_resource" "lb-to-s3-log" {
      provisioner "local-exec" {
        command = "aws elb modify-load-balancer-attributes --load-balancer-name ${var.LOAD_BALANCER_NAME[0]} --load-balancer-attributes file://${local_file.elb_attributes.filename}"
      }
    }
    

    不过,这里有一个更好的选择,除非有什么根本上的阻碍,否则 Terraform 会使用 the access_logs parameter to the resource 管理 ELB 访问日志:

    resource "aws_elb" "bar" {
      name               = "foobar-terraform-elb"
      availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
    
      access_logs {
        bucket        = "foo"
        bucket_prefix = "bar"
        interval      = 60
      }
    }
    

    您可能还需要考虑迁移到 Application Load BalancersNetwork Load Balancers,具体取决于您的使用情况,因为 ELB 是一项已弃用的服务。

    最后,还值得注意的是,template_file data source 自 0.12 起已弃用,而首选 templatefile function

    【讨论】:

    • 谢谢@ydaetskcoR,您的第一个建议是添加单引号修复它。
    猜你喜欢
    • 2016-11-25
    • 2021-12-06
    • 2020-03-23
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2021-09-30
    • 2021-12-07
    • 2020-03-22
    相关资源
    最近更新 更多