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.
CodePudding user response:
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"
}
}
}