Skip to content

How Pulumi IaC works ​

This page describes how Pulumi Infrastructure as Code (IaC) turns a program into deployed cloud resources.

Running a Pulumi program ​

Consider the following Pulumi program, which creates two S3 buckets:

typescript
import * as aws from "@pulumi/aws";

const mediaBucket = new aws.s3.Bucket("media-bucket");
const contentBucket = new aws.s3.Bucket("content-bucket");

First, run pulumi stack init mystack. Since mystack is a new stack, the "last deployed state" has no resources.

Next, run pulumi up. Since this program is written in

JavaScript

TypeScript

Python

Go

C#

Java

YAML

, the Pulumi CLI launches the

Node

Node

Python

Go

.NET

JVM

YAML

language host and requests that it execute the program. When the first

aws.s3.bucket

aws.s3.bucket

s3.Bucket

s3.NewBucket

Aws.S3.Bucket

object is constructed, the language host sends a _resource registration_ request to the deployment engine and then continues executing the program. This is subtle, but important: _When the call to_

new aws.s3.bucket

new aws.s3.bucket

s3.Bucket

s3.NewBucket

new Aws.S3.Bucket

_returns, it does not mean that the actual S3 bucket has been created in AWS_, it only means the language host has expressed that this bucket is part of the desired state of your infrastructure. The language host continues to execute your program concurrently with the engine processing this request. (The language host and deployment engine used throughout this walkthrough are defined in detail under [Architecture](#architecture) below.)

In this case, since the last deployed state has no resources, the engine determines that it needs to create the media-bucket resource. It uses the AWS resource plugin to create the resource, and the AWS resource plugin uses the AWS SDK to create it. Note that the engine does not talk directly to AWS; instead, it asks the AWS resource plugin to create a bucket. As new resource types are added, you can update the version of a resource provider to gain access to these new resources without having to update the CLI itself. When the operation to create this bucket is complete, the engine records information about the newly created resource in its state file.

As the engine was creating the media-bucket bucket, the language host continued to execute the Pulumi program. This caused another resource registration to be generated (for content-bucket). Since there is no dependency between these two buckets, the engine is able to process that request in parallel with the creation of media-bucket.

After both operations have completed, the language host exits as the program has finished running. Then the engine and resource providers shut down. The pulumi up output reports the resources that were created:

Updating (mystack)

     Type                      Name                Status
 +   pulumi:pulumi:Stack       my-project-mystack  created (6s)
 +   ├─ aws:s3:Bucket          media-bucket        created (2s)
 +   └─ aws:s3:Bucket          content-bucket      created (2s)

Resources:
    + 3 created

Duration: 7s

The names shown above—media-bucket and content-bucket—are the logical names you gave the resources in your program. By default, Pulumi appends a random suffix to each resource's physical name in AWS (for example, media-bucket-653a4f2). This is due to a process called auto-naming, which Pulumi uses by default to allow you to deploy multiple copies of your infrastructure without creating name collisions for resources. This behavior can be disabled if desired.

Now, make a change to one of the resources and run pulumi up again. Since Pulumi operates on a desired state model, it uses the last deployed state to compute the minimal set of changes needed to update your deployed infrastructure. For example, suppose you want to add tags to the S3 media-bucket. Change the program to express this new desired state:

typescript
import * as aws from "@pulumi/aws";

const mediaBucket = new aws.s3.Bucket("media-bucket", {
    tags: {"owner": "media-team"},
});
const contentBucket = new aws.s3.Bucket("content-bucket");

When you run pulumi preview or pulumi up, the entire process starts over. The language host starts running your program and the media bucket's definition causes a new resource registration request to be sent to the engine. This time, however, the state already contains a resource named media-bucket, so the engine asks the resource provider to compare the existing state from the previous run of pulumi up with the desired state expressed by the program. The process detects that the tags property has changed from empty to a map assigning the owner tag. By again consulting the resource provider, the engine determines that it can update this property without creating a new bucket, and so it tells the provider to update the tags property to set the owner tag. When this operation completes, the current state is updated to reflect the change that had been made.

The engine also receives a resource registration request for content-bucket. However, since there are no changes between the current state and the desired state, the engine does not need to make any changes to the resource.

Now, suppose you rename content-bucket to app-bucket.

typescript
import * as aws from "@pulumi/aws";

const mediaBucket = new aws.s3.Bucket("media-bucket", {
    tags: {"owner": "media-team"},
});
const appBucket = new aws.s3.Bucket("app-bucket");

This time, the engine does not need to make any changes to media-bucket since its desired state matches its actual state. However, when the resource request for app-bucket is processed, the engine sees there's no existing resource named app-bucket in the current state so it must create a new S3 bucket. Once that process is complete and the language host has shut down, the engine looks for any resources in the current state which it did not see a resource registration for. In this case, since you removed the registration of content-bucket from your program, the engine calls the resource provider to delete the existing content-bucket bucket.

Resource operations ​

When you run pulumi up or pulumi preview, Pulumi displays the operations it will perform on each resource. Understanding these operations helps you interpret what changes will occur.

Operation types ​

Pulumi uses these operation types to indicate the changes it will make:

OperationSymbolDescription
same(none)No changes detected. The resource's current state matches the desired state.
create+A new resource will be created.
update~An existing resource will be modified in place.
delete-An existing resource will be removed.
replace+-The resource must be replaced. Pulumi will create a new resource, then delete the old one.
create-replacement++A new resource is being created as part of a replacement operation.
delete-replaced--An old resource is being deleted after its replacement was created.

Additional operations ​

These operations appear in specific scenarios:

OperationDescription
readAn existing resource is being imported or read into Pulumi's state.
read-replacementReading an existing resource as part of a replacement.
refreshThe resource's state is being synchronized with the actual cloud provider state.
importA resource is being imported into Pulumi management.
import-replacementAn imported resource is replacing an existing resource.

By default, Pulumi does not refresh state before an operation. The refresh operation occurs when you run pulumi refresh, pass the --refresh flag to pulumi up, pulumi preview, or pulumi destroy, or set refresh: always under options in Pulumi.yaml. To learn more about when and why to refresh, see Refreshing state.

Understanding replacements ​

When a resource property changes in a way that cannot be updated in place, Pulumi performs a replacement. This involves:

  1. Creating the new resource with the updated configuration (create-replacement)
  2. Deleting the old resource after the new one is ready (delete-replaced)

By default, Pulumi creates the replacement before deleting the original to minimize downtime. You can change this behavior with the deleteBeforeReplace option. Pulumi HCL keeps Terraform's ordering instead: it deletes the old resource first unless you set create_before_destroy = true in a lifecycle block. The AWS Cloud Control provider also deletes first for every replacement.

Creation and deletion order ​

Pulumi executes resource operations in parallel whenever possible, but understands that some resources may have dependencies on other resources. If an output of one resource is provided as an input to another, the engine records the dependency between these two resources as part of the state and uses these when scheduling operations. This list can also be augmented by using the dependsOn resource option.

By default, if a resource must be replaced, Pulumi attempts to create a new copy of the resource before destroying the old one. This is helpful because it allows updates to infrastructure to happen without downtime. This behavior can be controlled by the deleteBeforeReplace option. If you configure auto-naming so that names have no random part, Pulumi automatically deletes the old resource first, since the create operation for the new version would otherwise fail with the name in use. Providing a specific name for a resource can cause the same failure. Many providers avoid it by applying deleteBeforeReplace automatically, but not all do, so set the option on any resource you name that may need to be replaced.

Architecture ​

Three main components work together when you run pulumi up: a language host, a deployment engine, and resource providers. The following diagram illustrates how they interact. Because each language has its own language host and program entrypoint, select your language to see the corresponding diagram:

mermaid
flowchart LR
    subgraph host["Node language host"]
        program["index.ts"]
    end
    state[("Last deployed<br/>state")]
    engine["Pulumi CLI &<br/>deployment engine"]
    subgraph providers["Resource providers"]
        aws["AWS"]
        azure["Azure"]
        k8s["Kubernetes"]
    end
    program -->|"new aws.s3.Bucket()"| engine
    engine <-->|"read / write"| state
    engine -->|"create, update, delete"| aws
    engine -->|"create, update, delete"| azure
    engine -->|"create, update, delete"| k8s

Language hosts ​

The language host is responsible for running a Pulumi program and setting up an environment where it can register resources with the deployment engine. Language hosts are implemented via plugins (specifically, language plugins). The language host consists of two different pieces:

  1. A language executor, which is a binary named pulumi-language-<language-name>, that Pulumi uses to launch the runtime for the language your program is written in (for example, Node or Python). Most language executors are distributed with the Pulumi CLI; the CLI downloads the HCL executor, pulumi-language-hcl, automatically the first time you run an HCL project.
  2. A language SDK is responsible for preparing your program to be executed and observing its execution in order to detect resource registrations. When a resource is registered—by constructing a resource object in your program—the language SDK communicates the registration request back to the deployment engine. The language SDK is distributed as a regular package, just like any other code that might depend on your program. For example, the TypeScript and JavaScript SDK is contained in the @pulumi/pulumi package available on npm, and the Python SDK is contained in the pulumi package available on PyPI. Pulumi YAML and Pulumi HCL have no separate language SDK: the language executor reads the program files itself and sends each resource registration to the engine.

Deployment engine ​

The deployment engine is responsible for computing the set of operations needed to drive the current state of your infrastructure into the desired state expressed by your program. When a resource registration is received from the language host, the engine consults the existing state to determine if that resource has been created before. If it has not, the engine uses a resource provider to create it. If it already exists, the engine works with the resource provider to determine what, if anything, has changed by comparing the old state of the resource with the new desired state of the resource as expressed by the program. If there are changes, the engine determines if it can update the resource in place or if it must replace it by creating a new version and deleting the old version. The decision depends on what properties of the resource are changing and the type of the resource itself. When the language host communicates to the engine that it has completed the execution of the Pulumi program, the engine looks for any existing resources for which it did not see a new resource registration and schedules these resources for deletion.

The deployment engine is embedded in the pulumi CLI itself.

Resource providers ​

A resource provider is made up of two different pieces:

  1. A resource plugin is the binary used by the deployment engine to manage a resource. These plugins are stored in the plugin cache (located in ~/.pulumi/plugins) and can be managed using the pulumi plugin set of commands.
  2. An SDK which provides bindings for each type of resource the provider can manage.

Like the language runtime itself, the SDKs are available as regular packages. For example, there is a @pulumi/aws package for Node available on npm and a pulumi_aws package for Python available on PyPI. Adding one of these packages to your project doesn't download the resource plugin. pulumi install downloads it, and pulumi preview and pulumi up download it automatically if it's missing. Pulumi HCL has no per-provider SDK package: pulumi install downloads the resource plugin for each provider your program uses and writes a descriptor file for it to sdks/<provider>/hcl.sdk.json.

Pulumi Cloud architecture ​

The components described above—the language host, deployment engine, and resource providers—all run on the client, wherever the Pulumi CLI runs. When you use the default Pulumi Cloud backend to store state, the CLI also coordinates with Pulumi Cloud.

Pulumi Cloud comprises two Internet-accessible endpoints—a web application at app.pulumi.com and a REST API at api.pulumi.com—along with supporting cloud infrastructure.

Pulumi Cloud never acquires your cloud credentials and does not communicate with your cloud provider directly. Instead, the CLI coordinates with both Pulumi Cloud's API and your cloud provider's API. This client/server division means your IAM and key management do not need to change when adopting Pulumi: if you run Pulumi from within a CI/CD environment, you can rely on the security mechanisms your organization already has in place.

Because the server does not have direct access to your cloud credentials, runtime data, or PII, Pulumi Cloud has been used in organizations with advanced compliance needs, including SOC 2, PCI, ISO 27001, and HIPAA. It is also possible to run your own instance of Pulumi Cloud in a private cloud environment—the architecture is very similar to the hosted version, but does not depend on public Internet access. To learn more, see Self-Hosted Pulumi Cloud.

Content from the official Pulumi documentation (github.com/pulumi/docs).