OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Terraform doesn't allow to use variable when creating resource. How to go around?

  • Thread starter Thread starter NME
  • Start date Start date
N

NME

Guest
I have two environments: 'dev' and 'prod'. I want to create AWS s3 bucket for each one using different configurations of lifecycle: 'false' for 'dev' and 'true' for 'prod'. Usually I use variables. But Terraform does not allow create a variable for 'lifecycle'. So I got little bit stuck. Please, help!

Code:
resource "aws_s3_bucket" "terraform_state" {
  bucket = "terraform-state-vpc-${var.env}"

  lifecycle {
    prevent_destroy = true
  }

  tags = {
    Name = "s3-state-file-${var.env}"
  }
}

Only I could came across is this. At the same file create:

Code:
resource "aws_s3_bucket" "terraform_state" {
  count = var.env == "prod" ? 1 : 0  # Create only if environment is 'prod'
  bucket = "terraform-state-vpc-${var.env}"

  lifecycle {
    prevent_destroy = true
  }

  tags = {
    Name = "s3-state-file-${var.env}"
  }
}

and

Code:
resource "aws_s3_bucket" "terraform_state" {
  count = var.env == "dev" ? 1 : 0  # Create only if environment is 'dev'
  bucket = "terraform-state-vpc-${var.env}"

  lifecycle {
    prevent_destroy = false
  }

  tags = {
    Name = "s3-state-file-${var.env}"
  }
}

but I can't create another resource for 'dev' environment. Because I am getting an error: 'Error: Duplicate resource "aws_s3_bucket" configuration'. Changing name of the bucket: "terraform_state" could solve it. But I need to use the same name.
<p>I have two environments: 'dev' and 'prod'. I want to create AWS s3 bucket for each one using different configurations of lifecycle: 'false' for 'dev' and 'true' for 'prod'. Usually I use variables. But Terraform does not allow create a variable for 'lifecycle'. So I got little bit stuck. Please, help!</p>
<pre><code>resource "aws_s3_bucket" "terraform_state" {
bucket = "terraform-state-vpc-${var.env}"

lifecycle {
prevent_destroy = true
}

tags = {
Name = "s3-state-file-${var.env}"
}
}
</code></pre>
<p>Only I could came across is this. At the same file create:</p>
<pre><code>resource "aws_s3_bucket" "terraform_state" {
count = var.env == "prod" ? 1 : 0 # Create only if environment is 'prod'
bucket = "terraform-state-vpc-${var.env}"

lifecycle {
prevent_destroy = true
}

tags = {
Name = "s3-state-file-${var.env}"
}
}
</code></pre>
<p>and</p>
<pre><code>resource "aws_s3_bucket" "terraform_state" {
count = var.env == "dev" ? 1 : 0 # Create only if environment is 'dev'
bucket = "terraform-state-vpc-${var.env}"

lifecycle {
prevent_destroy = false
}

tags = {
Name = "s3-state-file-${var.env}"
}
}
</code></pre>
<p>but I can't create another resource for 'dev' environment. Because I am getting an error: 'Error: Duplicate resource "aws_s3_bucket" configuration'.
Changing name of the bucket: "terraform_state" could solve it. But I need to use the same name.</p>
Continue reading...
 

Latest posts

Top