env0 / terraform-provider-env0

Terraform Provider for env0
https://env0.com
Mozilla Public License 2.0
39 stars 14 forks source link

resource `env0_module` creation causes 409 error. #915

Closed away168 closed 2 months ago

away168 commented 3 months ago

Describe the bug When using env0_module resource to create modules

Plan will create 4 environments

OpenTofu will perform the following actions:

  # env0_module.aws["ec2"] will be created
  + resource "env0_module" "aws" {
      + github_installation_id    = 11551359
      + id                        = (known after apply)
      + is_azure_devops           = false
      + module_name               = "ec2"
      + module_provider           = "aws"
      + module_test_enabled       = true
      + opentofu_version          = "latest"
      + path                      = "modules/ec2"
      + repository                = "https://github.com/env0/acme-fitness"
      + run_tests_on_pull_request = true
      + tag_prefix                = "ec2"
    }

  # env0_module.aws["eks_data"] will be created
  + resource "env0_module" "aws" {
      + github_installation_id    = 11551359
      + id                        = (known after apply)
      + is_azure_devops           = false
      + module_name               = "eks_data"
      + module_provider           = "aws"
      + module_test_enabled       = true
      + opentofu_version          = "latest"
      + path                      = "modules/eks_data"
      + repository                = "https://github.com/env0/acme-fitness"
      + run_tests_on_pull_request = true
      + tag_prefix                = "eks_data"
    }

  # env0_module.aws["s3"] will be created
  + resource "env0_module" "aws" {
      + github_installation_id    = 11551359
      + id                        = (known after apply)
      + is_azure_devops           = false
      + module_name               = "s3"
      + module_provider           = "aws"
      + module_test_enabled       = true
      + opentofu_version          = "latest"
      + path                      = "modules/s3"
      + repository                = "https://github.com/env0/acme-fitness"
      + run_tests_on_pull_request = true
      + tag_prefix                = "s3"
    }

  # env0_module.aws["vpc"] will be created
  + resource "env0_module" "aws" {
      + github_installation_id    = 11551359
      + id                        = (known after apply)
      + is_azure_devops           = false
      + module_name               = "vpc"
      + module_provider           = "aws"
      + module_test_enabled       = true
      + opentofu_version          = "latest"
      + path                      = "modules/vpc"
      + repository                = "https://github.com/env0/acme-fitness"
      + run_tests_on_pull_request = true
      + tag_prefix                = "vpc"
    }

On Apply :


> /opt/tofuenv/bin/tofu apply -auto-approve /tmp/d5e10a30-1dcc-405d-98ec-adc56200ca2a/.env0workdir/.tf-plan
env0_module.aws["vpc"]: Creating...
env0_module.aws["ec2"]: Creating...
env0_module.aws["eks_data"]: Creating...
env0_module.aws["s3"]: Creating...
env0_module.aws["ec2"]: Creation complete after 5s [id=6c655019-a9c8-4e77-9848-9af46fe266cb]
env0_module.aws["s3"]: Creation complete after 6s [id=6e0dcf40-d8c6-4328-942d-895b0e43bdda]
env0_module.aws["vpc"]: Still creating... [10s elapsed]
env0_module.aws["eks_data"]: Still creating... [10s elapsed]
╷
│ Warning: Redundant empty provider block
│ 
│   on ../../modules/random_pet/provider.tf line 10:
│   10: provider "random" {
│ 
│ Earlier versions of OpenTofu used empty provider blocks ("proxy provider
│ configurations") for child modules to declare their need to be passed a
│ provider configuration by their callers. That approach was ambiguous and is
│ now deprecated.
│ 
│ If you control this module, you can migrate to the new declaration syntax
│ by removing all of the empty provider "random" blocks and then adding or
│ updating an entry like the following to the required_providers block of
│ module.pet-name:
│     random = {
│       source = "hashicorp/random"
│     }
╵
╷
│ Error: could not create module: 409 Conflict: A module of provider: 'aws' and name: 'eks_data' already exists. Module provider and name must be unique in your organization.
│ 
│   with env0_module.aws["eks_data"],
│   on env0_modules.tf line 6, in resource "env0_module" "aws":
│    6: resource "env0_module" "aws" {
│ 
╵
╷
│ Error: could not create module: 409 Conflict: A module of provider: 'aws' and name: 'vpc' already exists. Module provider and name must be unique in your organization.
│ 
│   with env0_module.aws["vpc"],
│   on env0_modules.tf line 6, in resource "env0_module" "aws":
│    6: resource "env0_module" "aws" {
│ 
╵

In the UI, the modules were created successfully.

CleanShot 2024-07-30 at 12 50 59

but now, the state is out of sync, and thinks that the vpc and eks_data modules still need to be created

To Reproduce

locals {
  aws_modules = ["vpc", "ec2", "s3", "eks_data"]
  # aws_modules = []  # used to remove all modules
}

resource "env0_module" "aws" {
  for_each = toset(local.aws_modules)

  module_name         = each.key
  module_provider     = "aws"
  repository          = data.env0_template.this.repository
  path                = "${local.modules_dir}/${each.key}"
  tag_prefix          = each.key

  # enable module testing
  module_test_enabled       = true
  run_tests_on_pull_request = true
  opentofu_version          = "latest"

  # vcs configuration
  github_installation_id = var.vcs == "github" ? data.env0_template.this.github_installation_id : null
  bitbucket_client_key   = var.vcs == "bitbucket" ? data.env0_template.this.bitbucket_client_key : null
  is_azure_devops = var.vcs == "azure" ? data.env0_template.this.is_azure_devops : null
  token_id        = var.vcs == "gitlab" || var.vcs == "azure" ? data.env0_template.this.token_id : null
  token_name      = var.vcs == "gitlab" ? data.env0_template.this.token_name : null
  # gitlab_project_id    = data.env0_template.this.gitlab_project_id
}

Expected behavior Modules should all just created without 409 error.

Provider Version 1.19.7

Screenshots see above

Additional context n/a

TomerHeber commented 3 months ago

@away168 - why aren't you doing an import for existing resources?

away168 commented 3 months ago

These are all modules newly created. But when it's creating multiple modules at a time this error occurs

yaronya commented 2 months ago

This is an env0 backend issue, so no need to address it in the provider. Closing.