Hi all, Pulumi noob here and looking for some info...
# getting-started
s
Hi all, Pulumi noob here and looking for some info on best practice around using variables. I asked chatgpt and it suggested the following Put stack variables in the stack yaml - eg myApp.dev.yaml Initialise a config within the pulumi context and have an explicit Require() for the required variables - this bit is something I haven't seen in the docs Eg: *myApp.de*v.yaml
Copy code
config:
  transactionsDb:
    locationId: nam5
firestore.go
Copy code
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v7/go/gcp/firestore"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)

func transactionsFirestoreDb() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		cfg := config.New(ctx, "")
		locationId := cfg.Require("transactionsDb:locationId")

		_, err := firestore.NewDatabase(ctx, "database", &firestore.DatabaseArgs{
			LocationId:                    pulumi.String(locationId),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
Is that right or is it fine to skip those Require()'s?
m
Relevant docs snippet: > Configuration values can be retrieved for a given stack using either
config.Get
or
config.Require
. Using
config.Get
will return
nil
if the configuration value was not provided, and
config.Require
will raise an exception with a helpful error message to prevent the deployment from continuing until the variable has been set using the CLI. https://www.pulumi.com/docs/concepts/config/#code
🙌 1
s
That's what I get for glancing at docs 🤦‍♂️ Thankyou!
So just in relation to my example, it should actually be
Copy code
cfg := config.New(ctx, "transactionsDb")
locationId := cfg.Require(locationId")
m
Yes, that seems right 👍 But your original code will work as well.
f
n.b. if your project name is
transactionsDb
then
New(ctx, "")
would provide the same result (i.e. if
""
then namespace is set to your project's name)
m
I think the key is to understand that
Copy code
cfg := config.New(ctx, "transactionsDb")
locationId := cfg.Require(locationId")
and
Copy code
cfg := config.New(ctx, "")
locationId := cfg.Require("transcationsDb:locationId")
give the same result, right?
💯 1
f
well wait you wouldn't need that namespace in the
Require
there if the config value is in the project namespace. it's a bit confusing
m
And
config.New(ctx, "name-of-my-project")
is identical to
config.New(ctx,"")
? That's something I did not know
s
Ah okay I wasn't sure if the ':' separator was valid inside the Require
m
I don't have the code right now, but I'm pretty sure I've used
config = pulumi.Config(); config.get("aws:region")
before
f
if the config key [you request] doesn't contain a
:
it will add the namespace implicitly
💡 1
s
yeah that makes sense
seems safer to me to load the namespace when initialising the config unless you need to reference variables outside of that scope
f
this ^ yeah. i like to keep as much vars in the (default) project scope and only namespace for providers or when absolutely necessary. ESC helps here too as you have even more control
s
ah so you prefer something like
Copy code
config:
  transactionsDbName: transactionsDb
  transactionsDbType: FIRESTORE_NATIVE
over
Copy code
config:
  transactionsDb:
    name: transactionsDb
    type: FIRESTORE_NATIVE
is that right?
f
it depends 🙂 e.g. if you've got some massive Pulumi monoproject, that could get annoying and repetitive. but that's another thing: I tend to prefer many smaller projects (like microservices microstacks? 😆 ) and stack refs
i don't feel either is intrinsically wrong, just different
s
Yeah I did a lot of "microstacking" with terraform modules, similar concept I guess
terraform tfvar files don't have that namespace concept though which I found really annoying
f
heh yeah and then you start bolting on something like terragrunt or start doing dark magic with shellscript and then... yeah 😄
s
We had 100's of environments/aws accounts due to tenancy restrictions so I ended up writing a script that read a JSON file that described every environment, and then built a gitlab-ci file and all the terraform files