Variables and Values
A variable gives a name to a value so you can reuse it and refer to it later. Every configuration language you already use has a version of this: a locals block in HCL, a variables: section in Pulumi YAML. In a general-purpose language, naming a value is the most basic thing you do, and it works the same way whether the value is a string, a number, or the result of creating a resource.
Where you have seen this before
In Pulumi YAML, variables names a value once so you can reference it elsewhere in the same file:
variables:
bucketName: my-app-data
resources:
bucket:
type: aws:s3:Bucket
properties:
bucket: ${bucketName}In Terraform HCL, a locals block does the same thing, and ${...} interpolation substitutes it into a string:
locals {
bucket_name = "my-app-data"
}
resource "aws_s3_bucket" "bucket" {
bucket = "${local.bucket_name}"
}The syntax
In a general-purpose language, you declare a variable and assign it a value in one statement. Most Pulumi languages infer the type from the value, so you rarely write the type out yourself.
const bucketName = "my-app-data";
const replicaCount = 3;Building a string out of a variable works the way you'd expect from ${...} interpolation, just with each language's own syntax: template literals in TypeScript, f-strings in Python, fmt.Sprintf in Go, $"..." in C#, and String.format in Java.
const label = `${bucketName}-${replicaCount}`;In a Pulumi program
Stack configuration is the language equivalent of a Pulumi.<stack>.yaml value you'd otherwise reference directly. You read it into a variable with pulumi.Config and use it the same way you'd use any other variable. Set the value first, since config.require fails if it isn't set:
pulumi config set environment productionimport * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const config = new pulumi.Config();
const environment = config.require("environment");
const bucket = new aws.s3.Bucket(`data-${environment}`);What to watch out for
Not every value on a resource is a plain string or number you can interpolate directly. Many resource properties are outputs: values Pulumi doesn't know until the resource is actually created, such as a generated ARN or a load balancer's DNS name. Ordinary string interpolation doesn't work on an output, because the value isn't available when your program runs. See inputs and outputs for how that model works, and each language's interpolation helper, such as pulumi.interpolate in TypeScript or pulumi.Output.format() in Python, for building strings out of them.
Frequently asked questions
What replaces Terraform locals and variables in Pulumi?
An ordinary variable in your programming language plays the role Terraform's locals and variable blocks play. Per-stack inputs that used to live in a variable block instead come from stack configuration, which you read at the top of your program and assign to a regular variable.
Why doesn't string interpolation work on a resource output?
Because an output value isn't known while your program runs; it only resolves once the resource is created or updated. Standard string interpolation needs a value in hand immediately, so each language SDK provides an output-aware helper instead: pulumi.interpolate in TypeScript, Output.format() in Python and Java, pulumi.Sprintf() in Go, and Output.Format() in C#.
Do I have to declare a type for every variable?
No. Type inference handles most cases, so you can assign a value and let the compiler work out its type. Declaring a type explicitly still pays off for public function signatures and component inputs, where it gives you IDE completion and catches mismatches at compile time rather than at deployment time.
Next steps
Continue to conditionals to see how if statements replace count-based conditionals.
Learn more
- Configuration for the full set of options for reading and validating stack configuration.