terraform-google-modules / terraform-google-cloud-storage

Creates one or more Cloud Storage buckets and assigns basic permissions on them to arbitrary users
https://registry.terraform.io/modules/terraform-google-modules/cloud-storage/google
Apache License 2.0
167 stars 548 forks source link

Create multiple buckets using same statefile and terraform script #56

Closed MounicaRamini closed 3 years ago

MounicaRamini commented 4 years ago

Hello,

     Could you please help me with below scenario

Scenario: To create multiple buckets using the same terraform script and same state file, for two different projects.

Issue: I have tried to pass bucket name through Jenkins Pipeline to terraform script, each time a new value is passed, the bucket which was created earlier gets destroyed and a new bucket is created with the value being passed from Jenkins pipeline, because of statefile. So, ultimately, there is only a single bucket being created through that terraform script each time it is run with the new value. I have tried to follow something similar to here - Using Modules. But, I was only passing single bucket_name value from pipeline.

mike-19 commented 4 years ago

can you be more specific what value are you referring to here as "whenever a new value is passed" I see moreorless your issue is the way you are structuring , we do the same scenario as that you referred but we extensively avoid modules but we have structured our infra entities through folder structures and our jenkins pipeline understands our folder structure, but it depends from use case to use case

MounicaRamini commented 4 years ago

I was passing buket_name from Jenkins pipeline to terraform, so that team members can create any bucket, with any value they want. Goal is to create multiple buckets using same terraform script for different projects.

config.tf

terraform {
  backend "gcs" {
    bucket = "test-bucket-state"
    prefix = "terraform/test-bucket/"  
  }
}

main.tf

module "usis-auto-test-bucket" {
 source  = "./modules/storage"
 names   = "${var.bucket_name}"
 project = "project_id_value
  labels =  merge(var.tf_labels, {
    app_id              = "" # app_id must be from SNow CMDB
    component_labels    = "none"
    tier                = "app"
    costcenter         = "$1234"
    name                = "none" # Name of the Microservice
  })

  versioning = var.versioning
  encryption = {
    default_kms_key_name = var.kms_key_name_auto
  }
  lifecycle_rule = [
    {
      condition = {
        matches_storage_class = ["REGIONAL"]
      }
      action = {
        type          = "Delete"      
      }
    }
  ] 
}

modules/storage/main.tf - resource being referred by above module

resource "google_storage_bucket" "bucket" {
  count         = var.enabled ? 1 : 0
  name          = var.names
  project       =  var.project

  dynamic "versioning" {
    for_each = var.versioning
    content {
      enabled = versioning.key == "enabled" ? versioning.value : false
    }
  }

  labels = merge(var.labels, {
    terraform_module_version = "v1-0-4"
    terraform_module_git     = "bitbucket_terraform_repo"
    is_public = "no"
  })

  dynamic "encryption" {
    for_each = var.encryption
    content {
      default_kms_key_name = encryption.key == "default_kms_key_name" ? encryption.value : null
    }
  }

  dynamic "lifecycle_rule" {
    for_each = var.lifecycle_rule
    content {
      action {
        type          = lookup(lifecycle_rule.value["action"], "type", null)
        storage_class = lookup(lifecycle_rule.value["action"], "storage_class", null)
      }
      condition {
        age                   = lookup(lifecycle_rule.value["condition"], "age", null)
        created_before        = lookup(lifecycle_rule.value["condition"], "created_before", null)
        with_state            = lookup(lifecycle_rule.value["condition"], "with_state", null)
        matches_storage_class = lookup(lifecycle_rule.value["condition"], "matches_storage_class", null)
        num_newer_versions    = lookup(lifecycle_rule.value["condition"], "num_newer_versions", null)
      }
    }
  }
}
bharathkkb commented 4 years ago

I have two projects, Project A and Project B and under each Project, I would like two create multiple buckets using same terraform script and state file

Based on your modules/storage/main.tf you will need to invoke module "usis-auto-test-bucket" twice to pass in different project_id.

module "usis-auto-test-bucket-project1"{
...
project_id = "project-1-id"
names      = ["foo","bar"]
}
module "usis-auto-test-bucket-project2"{
...
project_id = "project-2-id"
names      = ["baz","qux"]
}
MounicaRamini commented 4 years ago

I have two projects, Project A and Project B and under each Project, I would like two create multiple buckets using same terraform script and state file

Based on your modules/storage/main.tf you will need to invoke module "usis-auto-test-bucket" twice to pass in different project_id.

module "usis-auto-test-bucket-project1"{
...
project_id = "project-1-id"
names      = ["foo","bar"]
}
module "usis-auto-test-bucket-project2"{
...
project_id = "project-2-id"
names      = ["baz","qux"]
}

@bharathkkb Thanks for your response.I have followed what you have mentioned and see an error mentioned below, and am using terraform v 0.12.20 here.

Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

Error: Invalid expression

<value for var.names> line 1:
  (source code not available)

Expected the start of an expression, but found an invalid expression token.
Error: No value for required variable on vars.tf line 7: variable "names"

The root module input variable "names" is not set, and has no default value.
Use a -var or -var-file command line argument to provide a value for this
variable.

Here is my updated code

main.tf

module "usis-auto-test-bucket" {
 source  = "./modules/storage"
 names   = ["test-bucket-1", "test-bucket-2"]
.
.
.
}

modules/storage/main.tf - resource being referred by above module

resource "google_storage_bucket" "bucket" {
  count         = var.enabled ? 1 : 0
  name          = "${var.names}"
.
.
.
}

variables.tf

variable "names" {
  description = "Names of the buckets to create."
  type        = list(string)
}
bharathkkb commented 4 years ago

@MounicaRamini resource "google_storage_bucket" "bucket" cannot take in a list of strings as names. My example above was when using this cft module in particular. I would also recommend you use for_each instead of count to iterate over bucket names. It also seems to be complaining about interpolation, which can be fixed by"${var.names}" => var.names.

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days