Code Monkey home page Code Monkey logo

Comments (6)

bharathkkb avatar bharathkkb commented on June 30, 2024 1

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

from terraform-google-cloud-storage.

mike-19 avatar mike-19 commented on June 30, 2024

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

from terraform-google-cloud-storage.

MounicaRamini avatar MounicaRamini commented on June 30, 2024

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

from terraform-google-cloud-storage.

MounicaRamini avatar MounicaRamini commented on June 30, 2024

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

from terraform-google-cloud-storage.

bharathkkb avatar bharathkkb commented on June 30, 2024

@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.

from terraform-google-cloud-storage.

github-actions avatar github-actions commented on June 30, 2024

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

from terraform-google-cloud-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.