替换 terraform 文件中的代码块

replace code block in terraform file

我是 运行 我的 Jenkins 服务器上的构建,我希望使用当前构建的提交号动态填充 git_commit 字段。该文件中有多个函数,我想使用 sed 来匹配模块的 core-lambda-function1 名称,并使用当前构建中的提交编号更新 git_commit 字段。任何帮助表示赞赏。谢谢

module "core-lambda-function1" {
  source  = "./lambda"
  name    = "core-lambda-function"
  runtime = "nodejs6.10"
  role    = "${aws_iam_role.iam_role_for_lambda.arn}"
  filename = "../Archive.zip"
  source_code_hash  = "${base64sha256(file("../Archive.zip"))}"
  source_dir = "../"
  git_commit = ""
}

module "core-lambda-function2" {
  source  = "./lambda"
  name    = "core-lambda-function"
  runtime = "nodejs6.10"
  role    = "${aws_iam_role.iam_role_for_lambda.arn}"
  filename = "../Archive.zip"
  source_code_hash  = "${base64sha256(file("../Archive.zip"))}"
  source_dir = "../"
  git_commit = ""
}

这是我目前拥有的。

#!/bin/bash
set -e

while read p; do
  NAME=$p
  GIT_COMMIT=`git rev-parse HEAD`
  echo $NAME | grep `xargs` main.tf -A 7 | sed -ri '7s/git_commit = ""/git_commit\ = \"'$GIT_COMMIT'"/g'
done < build_name

为什么不Input Variables in Terraform

variable "git_commit" {}

module "core-lambda-function1" {
  source  = "./lambda"
  name    = "core-lambda-function"
  runtime = "nodejs6.10"
  role    = "${aws_iam_role.iam_role_for_lambda.arn}"
  filename = "../Archive.zip"
  source_code_hash  = "${base64sha256(file("../Archive.zip"))}"
  source_dir = "../"
  git_commit = "${var.git_commit}"      #### Use variable here.
}

因此,在您的包装器脚本中,您可以更新为:

#!/bin/bash
set -e

GIT_COMMIT=$(git rev-parse HEAD)

terraform plan -var 'git_commit=${GIT_COMMIT}' ...