我正在使用Terraform连续部署lambda函数。 lambda模块创建函数和初始别名[DEV,QA,PROD]。进行更改后,source_code_hash
会更新,Terraform会更新代码。挑战是当我想要从DEV更新别名到QA时,它会更新整个堆栈。代码如下。感谢您的帮助。
$ cat main.tf
module "sample" {
source = "./lambda"
name = "sample"
runtime = "nodejs6.10"
role = "${aws_iam_role.iam_role_for_lambda.arn}"
filename = "../Archive.zip"
source_code_hash = "${base64sha256(file("../Archive.zip"))}"
source_dir = "../sample"
alias = "${var.env_name}"
}
$ cat module/main.tf
resource "aws_lambda_function" "lambda" {
filename = "${var.filename}"
function_name = "${var.name}"
role = "${var.role}"
handler = "${var.name}.${var.handler}"
runtime = "${var.runtime}"
source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
publish = "true"
}
resource "aws_lambda_alias" "lambda_alias" {
count = "2"
name = "${element(var.alias, count.index)}"
#name = "${var.alias}"
description = "${var.name}"
function_name = "${aws_lambda_function.lambda.arn}"
function_version = "${aws_lambda_function.lambda.version}"
}
答案 0 :(得分:0)
您需要针对不同环境使用不同的tfvars
和tfstate
文件。
假设您使用最新的terraform版本。
创建环境文件夹(例如env
)并为每个环境设置<env>-backend.tf
和<env>.tfvars
个文件
$ cat main.tf
terraform {
required_version = ">= 0.9.1"
backend "s3" {
encrypt = "true"
}
}
$ cat env/dev-backend.tf
bucket = "terraform-<change-to-s3-global-unique-id>"
key = "terraform/dev/terraform.tfstate"
kms_key_id = "xxxx-xxxx-xxxx-xxxx"
$ cat env/dev.tfvars
env_name = dev
对qa和prod环境也这样做。
因此,您应该可以在命令下运行以使相同的lambda
tf代码在不同的环境中工作
rm -rf .terraform
export env="dev"
terraform init -backend=true -backend-config=env/${env}-backend.tf
terraform plan -var-file=./env/${env}.tfvars