Understanding Stack Outputs
We’ve created some resources. Now, let’s see how we can use outputs outside of Pulumi. In this part, we’re going to explore stack outputs. Stack outputs are, as you might guess, the values exported from any given stack. These values are shown during an update, can be retrieved with the Pulumi CLI, and are displayed in the Pulumi Service once you’ve exported them. Example values include resource IDs, computed IP addresses, and DNS names. They’re extremely useful when you want to run commands with the CLI that reference those values. Note, though, that stack outputs are for the current stack only. If you want to get values from another stack, you want to use stack references, which bridge different stacks through inter-stack dependencies.
Typically, you will pass some value from your resources into the output, but to illustrate how stack outputs work, we will set some stack outputs manually:
At the end of the index.js
index.ts
__main__.py
main.go
Program.cs
Program.fs
Program.vb
App.java
Pulumi.yaml
my-first-app
, add the following line:
export const url = pulumi.interpolate`http://localhost:${frontendPort}`;
pulumi.export("url", pulumi.Output.format("http://localhost:{0}", frontend_port))
ctx.Export("url", pulumi.Sprintf("http://localhost:%v", frontendPort))
Note that you’ll want to insert this just before the last return nil
statement.
return new Dictionary<string, object?>
{
["url"] = Output.Format($"http://localhost:{frontendPort}")
};
Add this return statement to the end of the deployment function.
outputs:
url: http://localhost:${frontendPort}
Replace the existing outputs: {}
line with this code.
Now, run pulumi up -y
$ pulumi up
Previewing update (dev)
View Live: https://app.pulumi.com/***/my-first-app/dev/previews/...
...
Updating (dev)
View Live: https://app.pulumi.com/***/my-first-app/dev/updates/3
...
pulumi:pulumi:Stack my-first-app-dev running
pulumi:pulumi:Stack my-first-app-dev
Outputs:
url: "http://localhost:3001"
...
Notice that there is now a stack output for the value of the key url
.
We can also get this value by running pulumi stack output <key>
on any
particular stack.
$ pulumi stack output url
http://localhost:3001
And we can use this in the curl
command to check our website:
$ curl $(pulumi stack output url)
Making a stack configurable
One of the main reasons to use stacks is to have different configurations
between them. In this example, we will set a configuration that varies between
our dev
and staging
stacks and set it programmatically.
First, we need to define the configuration. We have already set this in the
dev
stack in the Fundamentals tutorial. Let’s take a look! Make sure the
dev
stack is active:
$ pulumi stack select dev
Now, run the following command to get the values for this stack’s configuration:
$ pulumi config
KEY VALUE
backendPort 3000
database cart
frontendPort 3001
mongoHost mongodb://mongo:27017
mongoPort 27017
nodeEnvironment development
protocol http://
Let’s set the configuration for the staging
stack. We’ll use the same values
as dev
, except the frontendPort
will be set to 3002
.
$ pulumi stack select staging
$ pulumi config set frontendPort 3002
$ pulumi config set backendPort 3000
$ pulumi config set mongoPort 27017
$ pulumi config set mongoHost mongodb://mongo:27017
$ pulumi config set database cart
$ pulumi config set nodeEnvironment development
$ pulumi config set protocol http://
You should have two new files in your directory now: Pulumi.dev.yaml
and
Pulumi.staging.yaml
. If you take a look at them, you’ll see each one has the
value for frontendPort
set (along with some other values we set in the
Fundamentals tutorial):
$ cat Pulumi.staging.yaml
config:
my-first-app:backendPort: "3000"
my-first-app:database: cart
my-first-app:frontendPort: "3002"
my-first-app:mongoHost: mongodb://mongo:27017
my-first-app:mongoPort: "27017"
my-first-app:nodeEnvironment: development
my-first-app:protocol: http://
$ pulumi config
KEY VALUE
backend_port 3000
database cart
frontend_port 3001
mongo_host mongodb://mongo:27017
mongo_port 27017
node_environment development
protocol http://
Let’s set the configuration for the staging
stack. We’ll use the same values
as dev
, except the frontend_port
will be set to 3002
.
$ pulumi stack select staging
$ pulumi config set frontend_port 3002
$ pulumi config set backend_port 3000
$ pulumi config set mongo_port 27017
$ pulumi config set mongo_host mongodb://mongo:27017
$ pulumi config set database cart
$ pulumi config set node_environment development
$ pulumi config set protocol http://
You should have two new files in your directory now: Pulumi.dev.yaml
and
Pulumi.staging.yaml
. If you take a look at them, you’ll see each one has the
value for frontend_port
set (along with some other values we set in the
Fundamentals tutorial):
$ cat Pulumi.staging.yaml
config:
my-first-app:backend_port: "3000"
my-first-app:database: cart
my-first-app:frontend_port: "3002"
my-first-app:mongo_host: mongodb://mongo:27017
my-first-app:mongo_port: "27017"
my-first-app:node_environment: development
my-first-app:protocol: http://
$ pulumi config
KEY VALUE
backendPort 3000
database cart
frontendPort 3001
mongoHost mongodb://mongo:27017
mongoPort 27017
nodeEnvironment development
protocol http://
Let’s set the configuration for the staging
stack. We’ll use the same values as dev
, except the frontendPort
will be set to 3002
.
$ pulumi stack select staging
$ pulumi config set frontendPort 3002
$ pulumi config set backendPort 3000
$ pulumi config set mongoPort 27017
$ pulumi config set mongoHost mongodb://mongo:27017
$ pulumi config set database cart
$ pulumi config set nodeEnvironment development
$ pulumi config set protocol http://
You should have two new files in your directory now: Pulumi.dev.yaml
and
Pulumi.staging.yaml
. If you take a look at them, you’ll see each one has the
value for frontendPort
set (along with some other values we set in the
Fundamentals tutorial):
$ cat Pulumi.staging.yaml
config:
my-first-app:backendPort: "3000"
my-first-app:database: cart
my-first-app:frontendPort: "3002"
my-first-app:mongoHost: mongodb://mongo:27017
my-first-app:mongoPort: "27017"
my-first-app:nodeEnvironment: development
my-first-app:protocol: http://
$ pulumi config
KEY VALUE
backendPort 3000
database cart
frontendPort 3001
mongoHost mongodb://mongo:27017
mongoPort 27017
nodeEnvironment development
protocol http://
Let’s set the configuration for the staging
stack. We’ll use the same values
as dev
, except the frontendPort
will be set to 3002
.
$ pulumi stack select staging
$ pulumi config set frontendPort 3002
$ pulumi config set backendPort 3000
$ pulumi config set mongoPort 27017
$ pulumi config set mongoHost mongodb://mongo:27017
$ pulumi config set database cart
$ pulumi config set nodeEnvironment development
$ pulumi config set protocol http://
You should have two new files in your directory now: Pulumi.dev.yaml
and
Pulumi.staging.yaml
. If you take a look at them, you’ll see each one has the
value for frontendPort
set (along with some other values we set in the
Fundamentals tutorial):
$ cat Pulumi.staging.yaml
config:
my-first-app:backendPort: "3000"
my-first-app:database: cart
my-first-app:frontendPort: "3002"
my-first-app:mongoHost: mongodb://mongo:27017
my-first-app:mongoPort: "27017"
my-first-app:nodeEnvironment: development
my-first-app:protocol: http://
Now, if you run pulumi up
while in the staging
stack, we should see that the
frontend port is now set to 3002
:
$ pulumi stack output url
http://localhost:3002
Next up, we’ll explore how to share outputs with other stacks. Let’s go!
Thank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.