Terraform 14 template_file and null_resource issue



我正试图使用null资源,使用本地exec provisioner使用模板文件在负载均衡器上启用s3 bucket日志记录。terraform文件和模板文件(lb-to-s3-log.tpl(都在同一目录"模块/lb-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,我遵循了本文档中为亚马逊经典负载均衡器的日志存储启用s3 bucket的过程

发生此错误是因为您需要在命令行上格式化要转义的JSON,或者将JSON写入文件,然后使用file://引用它。

用单引号包装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通过对资源使用access_logs参数来管理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
}
}

您可能还需要考虑迁移到应用程序负载平衡器或网络负载平衡器,这取决于您的使用情况,因为ELB是一种不推荐使用的服务。

最后,还值得注意的是,template_file数据源由于0.12而被弃用,取而代之的是templatefile函数。

最新更新