1. How to pass Pulumi output as input to another resource?

    TypeScript

    You can pass an output of one resource as an input to another using the apply method of the Output class in Pulumi. The apply function is used to transform the data of Output<T> into Output<U>, making it easy to chain operations off of resources that do not have their values available until after the resource is created.

    Let's consider a scenario where you want to create an AWS S3 bucket and then an AWS S3 bucket policy that refers to this bucket.

    Here is a simple example:

    import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Create an AWS resource (S3 Bucket) let bucket = new aws.s3.Bucket("myBucket"); // Create an AWS S3 Bucket Policy which refers to the above created S3 bucket. let bucketPolicy = bucket.arn.apply(arn => new aws.s3.BucketPolicy("myBucketPolicy", { bucket: bucket.id, policy: pulumi.interpolate`{ "Version":"2012-10-17", "Statement":[{ "Effect":"Allow", "Principal": "*", "Action":["s3:GetObject"], "Resource":["${arn}/*"] }] }` }));

    In this code:

    • bucket.arn.apply is called on the ARN of the bucket. This ARN is not yet known and will only be known once the bucket is created. However, it is wrapped with Output<T>.
    • The apply function allows us to provide a callback (a function) which takes the bucket's ARN as input.
    • This callback creates a new aws.s3.BucketPolicy using this ARN.

    You can see more about using outputs in our official documentation.