简体   繁体   English

terraform aws s3 根据条件应用复制规则

[英]terraform aws s3 apply replication rule based on condition

I am new to terraform and I need some help.我是 terraform 的新手,我需要一些帮助。

I have replication_configuration like below and I want to apply a particular rule based on certain condition.我有如下所示的 replication_configuration,我想根据特定条件应用特定规则。

resource "aws_s3_bucket" "bucket" {
    replication_configuration {
        rules {
            id = "rule1"
        }
        rules {
            id = "rule2"
        }
        rules {
            id = "rule3"
        }
    }
}

i want rule1 to be considered only for dev environment, rule2 for stage and rule3 for prod and I already have an environment variable which will indicate from which environment this script is being run.我希望 rule1 仅用于开发环境,rule2 用于阶段,rule3 用于 prod,并且我已经有一个环境变量,它将指示该脚本正在从哪个环境运行。 How can I achieve this?我怎样才能做到这一点?

Edit: I don't want terraform to execute rule2 and rule3 in case of a dev environment, similarly, for other 2 environments.编辑:我不希望 terraform 在开发环境的情况下执行 rule2 和 rule3,类似地,对于其他 2 个环境。 Is there something like an if condition that I can mention before each rule inside replication_configuration to achieve this.我可以在 replication_configuration 中的每个规则之前提到 if 条件之类的东西来实现这一点。

First of all, please note there is a change in the resource from AWS provider version 4 !!!首先,请注意 AWS 提供商版本 4 中的资源发生了变化!!!

The replication_configuration argument is read-only as of version 4.0 of the Terraform AWS Provider.从 Terraform AWS 提供商的 4.0 版开始, replication_configuration参数是只读的。 Start using separate resource aws_s3_bucket_replication_configuration for configuration details.开始使用单独的资源aws_s3_bucket_replication_configuration获取配置详细信息。

Coming to your question.. within rule, you can enable/disable the rule based on your env.来到你的问题......在规则中,你可以根据你的环境启用/禁用规则。

variable "env" {
  description = "Env type"
  type        = string
  default = "dev"
}

resource "aws_s3_bucket" "bucket" {
    replication_configuration {
        rules {
            id = "rule1"
            status = var.env == "dev" ? "Enabled" : "Disabled"
        }
        rules {
            id = "rule2"
            status = var.env == "stage" ? "Enabled" : "Disabled"
        }
        rules {
            id = "rule3"
            status = var.env == "prod" ? "Enabled" : "Disabled"
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM