Functions
A function packages a piece of logic so you can call it with different inputs instead of repeating the logic each time. This is the same job a Terraform module does at a coarser grain: you parameterize a group of resources once, then reuse it. In a general-purpose language, a function is the smallest unit of that reuse, and it works the same whether it computes a plain value or creates cloud resources.
Where you have seen this before
A Terraform module is a coarser version of the same idea: you declare inputs once, and every place that calls the module gets its own copy of the resources inside it with those inputs applied.
module "logging_bucket" {
source = "./modules/logging-bucket"
name = "app-logs"
}
module "audit_logging_bucket" {
source = "./modules/logging-bucket"
name = "audit-logs"
}A function is the same reuse mechanism at a smaller grain: no separate directory or source block, just a callable name in the same file.
The syntax
A function takes parameters, does something with them, and optionally returns a value.
function taggedName(prefix: string, environment: string): string {
return `${prefix}-${environment}`;
}
const name = taggedName("app", "production");In a Pulumi program
The same pattern applies when a function's job is to create resources rather than compute a string. A function that creates a bucket with a standard set of properties lets every call site stay short, and it keeps the tagging or naming convention in one place:
import * as aws from "@pulumi/aws";
function createLoggingBucket(name: string): aws.s3.Bucket {
return new aws.s3.Bucket(name, {
lifecycleRules: [{ enabled: true, expiration: { days: 90 } }],
});
}
const appLogs = createLoggingBucket("app-logs");
const auditLogs = createLoggingBucket("audit-logs");What to watch out for
Calling the same function twice creates two separate resources, so the name you pass in has to be unique each time, the same requirement covered in loops. A function is the right tool as long as the group of resources it creates doesn't need its own identity in Pulumi's state: no combined outputs, no shared configuration, nothing another part of the program needs to reference as a unit. Once it does, that's the signal to move to a class-based component instead.
Frequently asked questions
When should I use a function instead of a component?
Reach for a plain function when you just want to group or parameterize a handful of resource declarations, and reach for a component once that group needs its own identity, combined outputs, or the ability to be referenced elsewhere in the program as a single unit. A function is the lighter-weight option; a component is the one that behaves like a resource itself.
Are Pulumi functions the same as Terraform modules?
Not quite. The closest analogue to a Terraform module is a Pulumi component resource, which gets its own name, state entry, and combined outputs. A plain function in your programming language is a lighter-weight grouping mechanism with no state identity of its own; it just runs code and returns values.
Can I pass a resource output into a function?
Yes. Accept it as an ordinary function parameter and pass it straight through to whatever resource properties need it. You don't need to unwrap an output before handing it to a function; Pulumi resolves it later, once its value is known.
Next steps
Continue to classes and components to see how grouping resources into a class gives them a shared identity.