Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control resource creation order in Pulumi

Tags:

pulumi

I'm trying to create some resources and need to enforce some sort of creation order. e.g. creating an aws.s3.Bucket for storing the logs before it can be used as an input to aws.cloudfront.Distribution.

How do I control resource creation order when using Pulumi?

like image 771
Chris Smith Avatar asked Dec 24 '22 06:12

Chris Smith


1 Answers

Generally, Pulumi handles the ordering of resource creation automatically. In TypeScript this is even enforced by the language's type system via pulumi.Input<T> and pulumi.Output<T> types. But understanding the details of those types isn't actually necessary.

The Pulumi engine will resolve all "parameters" or "inputs" to a resource. So if you use one resource as a parameter in configuring another, the dependent resource will be created first. i.e. it works the way you would want it to.

However, there are situations where you need to explicitly mark one resource as being dependent upon another. This will happen when there is some sort of coupling that exists outside of the Pulumi program.

To specify an explicit dependency, you can provide an instance of pulumi.ResourceOptions to the resource, and set its dependsOn property. The Pulumi engine will resolve all of the resources in the dependsOn array before processing the resource.

Here's a simple example showing these two ways the Pulumi determines ordering. An AWS S3 bucket is a resource that contains files, called objects. The bucket must be created before any objects can be created inside of it.

// Create a bucket named "example-bucket", available at s3://example-bucket.
let bucket = new aws.s3.Bucket("bucket",
    {
        bucket: "example-bucket",
    });

let file1 = new aws.s3.BucketObject("file1", {
    // The bucket field of BucketObjectArgs is an instance of
    // aws.s3.Bucket. Pulumi will know to create the "bucket"
    // resource before this BucketObject resource.
    bucket: bucket,
});

let file2 = new aws.s3.BucketObject("file2",
    {
        // The bucket field of BucketObjectArgs is a string. So
        // Pulumi does not know to block creating the file2 resource
        // until the S3 bucket exists.
        bucket: "example-bucket",
    } as aws.s3.BucketArgs,
    {
        // By putting "bucket" in the "dependsOn" array here,
        // the Pulumi engine will create the bucket resource before
        // this file2 resource.
        dependsOn: [ bucket ],
    } as pulumi.ResourceOptions);
like image 169
Chris Smith Avatar answered Apr 09 '23 02:04

Chris Smith