简体   繁体   中英

terraform aws s3 apply replication rule based on condition

I am new to terraform and I need some help.

I have replication_configuration like below and I want to apply a particular rule based on certain condition.

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. 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. Is there something like an if condition that I can mention before each rule inside replication_configuration to achieve this.

First of all, please note there is a change in the resource from AWS provider version 4 !!!

The replication_configuration argument is read-only as of version 4.0 of the Terraform AWS Provider. Start using separate resource aws_s3_bucket_replication_configuration for configuration details.

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"
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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