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
- Creating Resources: We start by creating an S3 bucket using the
aws.s3.Bucket
resource. - Using Output as Input: We pass the
bucket.bucket
output from the S3 bucket as an input to thetags
property of theaws.ec2.Instance
resource. - 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 upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.