https://pulumi.com logo
#golang
Title
# golang
m

magnificent-vase-49161

02/06/2024, 2:00 PM
Hi everyone, I'm having a strange issue with my Pulumi, I've just started using the following folder structure:
Copy code
IAC-PULUMI
├── dev
│   ├── eks
│   ├── iam
│   ├── rds
│   ├── vpc
│   └── dev.go
├── prod
│   ├── eks
│   ├── iam
│   ├── rds
│   ├── vpc
│   └── prod.go
├── shared
│   ├── eks
│   ├── iam
│   ├── rds
│   ├── vpc
│   └── shared.go
├── modules
│   ├── eks
│   ├── iam
│   ├── rds
│   ├── vpc
├── .gitignore
├── go.mod
├── go.sum
├── main.go
├── Pulumi.iac-pulumi-dev.yaml
├── Pulumi.iac-pulumi-prod.yaml
├── Pulumi.iac-pulumi-shared.yaml
├── Pulumi.yaml
└── vendor
My main.go is as follows:
Copy code
package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"

	"iac-pulumi/shared"
)

func runPulumiCommand(env string) {
	// Construct the stack name based on the environment variable
	stackName := fmt.Sprintf("<<MY-COMPANY>>/iac-pulumi/%s", env)
	// Select the stack
	cmd := exec.Command("pulumi", "stack", "select", stackName)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if err != nil {
		log.Fatalf("Failed to select Pulumi stack: %v", err)
	}
}

func main() {
    env := os.Getenv("DEPLOY_ENV")
    if env == "" {
        fmt.Println("Please run: export DEPLOY_ENV=<your-environment>")
        return
    }

    switch env {
    case "shared":
        shared.Deploy()
        runPulumiCommand(env)
    default:
        panic("Unknown environment")
    }
}
However, when running the command
pulumi up or pulumi preview
I receive the following error:
Copy code
Diagnostics:
   pulumi:pulumi:Stack (iac-pulumi-shared):
     error: Duplicate resource URN 'urn:pulumi:shared::iac-pulumi::pulumi:pulumi:Stack::iac-pulumi-shared'; try giving it a unique name
As if it is trying to recreate the stack but the stack already exists so in theory it should just select the existing one. I searched slack and the internet for the same problem and only found this: https://github.com/pulumi/pulumi/issues/13779 Does anyone have any insight into how to resolve this? Thanks!
s

salmon-account-74572

02/06/2024, 10:14 PM
I’d recommend you have a look at Automation API, which I think will be a far more elegant solution than
exec.Command
to perform operations on Pulumi stacks. Also, you’ll want to review the documentation around Pulumi projects and stacks, so that you understand how to manipulate them with Automation API. The docs have some great materials here; let me know if you need links and I’ll look them up (although they should be pretty easy to find). If you have further specific questions, let me know. I’m happy to help if I’m able.
m

magnificent-vase-49161

02/07/2024, 1:48 PM
Thank you @salmon-account-74572 I'll take a look
Problem solved, I had to share the Pulumi ctx between the resources, otherwise it ends up trying to recreate the stack with each new resource added
s

salmon-account-74572

02/07/2024, 8:16 PM
Gotcha---yep, that would definitely do it. Glad you found a solution!