1. Answers
  2. Passing Pulumi Output as Input to Another Resource

How do I pass Pulumi output as input to another resource?

In this guide, we will demonstrate how to pass the output of one Pulumi resource as the input to another resource. This is a common requirement when building cloud infrastructure, where the output from one service is often needed to configure another service.

We will create an S3 bucket and then use its name as an input to create an EC2 instance that tags the instance with the S3 bucket name.

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

// Create an S3 bucket
const bucket = new aws.s3.Bucket("my-bucket");

// Use the bucket name as an input to create an EC2 instance
const instance = new aws.ec2.Instance("my-instance", {
    ami: "ami-0c55b159cbfafe1f0", // Example AMI ID, replace with a valid one
    instanceType: "t2.micro",
    tags: {
        Name: "MyInstance",
        BucketName: bucket.bucket, // Pass the S3 bucket name as a tag
    },
});

// Export the bucket name and instance ID
export const bucketName = bucket.bucket;
export const instanceId = instance.id;

Key Points

  1. Creating Resources: We start by creating an S3 bucket using the aws.s3.Bucket resource.
  2. Using Output as Input: We pass the bucket.bucket output from the S3 bucket as an input to the tags property of the aws.ec2.Instance resource.
  3. Exporting Values: Finally, we export the bucket name and instance ID for easy access.

Summary

In this example, we demonstrated how to pass the output of one Pulumi resource (an S3 bucket) as an input to another resource (an EC2 instance). This technique is essential for building interconnected cloud infrastructure using Pulumi.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up