Terraform:运行空资源后如何强制资源更新?
Posted
技术标签:
【中文标题】Terraform:运行空资源后如何强制资源更新?【英文标题】:Terraform: How do I force a resource update after running a null resource? 【发布时间】:2022-01-12 08:45:17 【问题描述】:这是我的 terraform 代码:
resource "aws_s3_bucket_object" "file_upload"
for_each = fileset("init_conf/", "*")
bucket = aws_s3_bucket.conf_bucket.id
acl = "private"
key = "config/$local.service_name/$each.value"
source = "init_conf/$each.value"
source_hash = filemd5("init_conf/$each.value")
kms_key_id = "arn:aws:kms:##################:###################"
server_side_encryption = "aws:kms"
tags = merge(tomap(
"Name" = local.service_name,
), local.default_tags)
resource "null_resource" "cert"
triggers =
always_run = "$timestamp()"
provisioner "local-exec"
command = "openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout $path.cwd/init_conf/$var.cname.key -out $path.cwd/init_conf/$var.cname.crt"
terraform apply 执行后,首先刷新:resource "aws_s3_bucket_object" "file_upload" 的状态,然后才执行:provisioner "local-exec"。然而,我想要的是:provisioner“local-exec”首先执行,它为我生成一个证书并存储它,然后才使用资源“aws_s3_bucket_object”“file_upload”上传文件。
有人可以帮忙吗?
【问题讨论】:
在aws_s3_bucket_object
上添加depends_on
属性。
进展如何?还不清楚你能做什么?
【参考方案1】:
您可以添加:
depends_on = [null_resource.cert]
致您的aws_s3_bucket_object.file_upload
:
resource "aws_s3_bucket_object" "file_upload"
for_each = fileset("init_conf/", "*")
bucket = aws_s3_bucket.conf_bucket.id
acl = "private"
key = "config/$local.service_name/$each.value"
source = "init_conf/$each.value"
source_hash = filemd5("init_conf/$each.value")
kms_key_id = "arn:aws:kms:##################:###################"
server_side_encryption = "aws:kms"
depends_on = [null_resource.cert]
tags = merge(tomap(
"Name" = local.service_name,
), local.default_tags)
【讨论】:
不错的想法,但不幸的是不起作用。这是 Terraform 的输出:module.fargate_deployment.aws_s3_bucket_object.file_upload["nginx.conf"]: Refreshing state... [id=config/backend/nginx.conf] module.fargate_deployment.aws_s3_bucket_object.file_upload[".gitkeep"]: Refreshing state... [id=config/backend/.gitkeep]
@RELiABLE 我没有看到任何错误。对我来说似乎很好。
然后:module.fargate_deployment.null_resource.cert: Provisioning with 'local-exec'... module.fargate_deployment.null_resource.cert (local-exec): Executing: ["/bin/sh" "-c" "openssl req -new -newkey rsa.....................
@RELiABLE 你一定是用错了我的答案,或者你的问题不能代表你的真实代码。
证书建在最后,不再上传。以上是关于Terraform:运行空资源后如何强制资源更新?的主要内容,如果未能解决你的问题,请参考以下文章