如何使用Terraform初始化(而不是重置)?

huangapple go评论63阅读模式
英文:

How to initialise (not reset) with Terraform?

问题

在Terraform中,是否可以:

  • 声明一个具有默认值的AWS SSM参数存储资源(即,如果需要创建参数,则同时分配给它,但如果云中的值“漂移”,则不更新它);
  • 在Terraform首次创建Lambda时立即触发Lambda函数(而不是在后续Terraform应用中当Lambda已经存在时触发);
  • 或者,仅在配置中的特定变量自从存储的当前状态以来已创建/修改时才产生副作用。

换句话说,使用Terraform进行持续部署,是否有一种方式在首次创建基础架构时初始化应用,而不会在下次Terraform运行时重置应用?

英文:

In Terraform is it possible to:

  • declare an AWS SSM Parameter Store resource with a default value (i.e. if the parameter needs to be created then also assign to it, but do not update it if the value in the cloud "drifts");
  • trigger a Lambda function immediately after Terraform first creates that Lambda (and not on subsequent Terraform applies when the Lambda already exists);
  • or, produce a side-effect only if a particular variable in the configuration has been created/modified since the stored current state.

In other words, using Terraform for continuous deployment, is there some way to initialise an application when its infrastructure is first created, without resetting the application the next time Terraform runs?

答案1

得分: 3

> ignore_changes 功能旨在在创建资源时引用将来可能更改但不应影响创建后的资源的数据时使用。

例如,对于您的 SSM 参数:

resource "aws_ssm_parameter" "example" {
  name  = "foo"
  type  = "String"
  value = "bar"

  lifecycle {
    ignore_changes = [
      "value"
    ]
  }
}
英文:

https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#ignore_changes

> The ignore_changes feature is intended to be used when a resource is created with references to data that may change in the future, but should not affect said resource after its creation.

For example, for your SSM parameter:

resource "aws_ssm_parameter" "example" {
  name  = "foo"
  type  = "String"
  value = "bar"

  lifecycle {
    ignore_changes = [
      "value"
    ]
  }
}

huangapple
  • 本文由 发表于 2023年3月21日 02:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定