1. Answers
  2. How To Pass Pulumi Output As Input To Another Resource?

How to Pass Pulumi Output as Input to Another Resource?

In this guide, we will demonstrate how to pass Pulumi output as input to another resource in TypeScript using Pulumi. This is a common requirement when building infrastructure as code, as many resources depend on the outputs of other resources. We will use AWS as the cloud provider and create an S3 bucket and an IAM user. The IAM user will have permissions to access the S3 bucket, and we will pass the bucket name as an output from the S3 bucket resource to the IAM user resource.

Introduction

Pulumi is an infrastructure as code tool that allows you to define and manage cloud resources using familiar programming languages. In this guide, we will focus on passing outputs from one resource as inputs to another resource in TypeScript. This is particularly useful when you have dependencies between resources, such as an IAM user needing access to an S3 bucket.

Step-by-Step Explanation

Step 1: Create an S3 Bucket

First, we will create an S3 bucket using the aws.s3.Bucket resource. This will be our source resource, and we will output the bucket name.

Step 2: Create an IAM User

Next, we will create an IAM user using the aws.iam.User resource. This user will need permissions to access the S3 bucket.

Step 3: Create an IAM Policy

We will create an IAM policy that grants the IAM user permissions to access the S3 bucket. The policy will use the bucket name output from the S3 bucket resource.

Step 4: Attach the Policy to the IAM User

Finally, we will attach the IAM policy to the IAM user, completing the setup.

Key Points

  • Pulumi allows you to pass outputs from one resource as inputs to another resource using the .apply method.
  • The .apply method takes a function that receives the output value and returns the input value for the dependent resource.
  • This approach ensures that dependencies between resources are correctly managed.

Conclusion

In this guide, we demonstrated how to pass Pulumi output as input to another resource in TypeScript. By following the steps outlined, you can manage dependencies between resources effectively, ensuring that your infrastructure is configured correctly. Pulumi’s use of familiar programming languages makes it easier to define and manage complex cloud infrastructure.

Full Code Example

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

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

// Create an IAM user
const user = new aws.iam.User("my-user");

// Create an IAM policy for the user to access the S3 bucket
const policy = new aws.iam.Policy("my-policy", {
    policy: bucket.bucket.apply(bucketName => JSON.stringify({
        Version: "2012-10-17",
        Statement: [
            {
                Action: ["s3:ListBucket"],
                Effect: "Allow",
                Resource: [
                    `arn:aws:s3:::${bucketName}`
                ]
            },
            {
                Action: ["s3:GetObject", "s3:PutObject"],
                Effect: "Allow",
                Resource: [
                    `arn:aws:s3:::${bucketName}/*`
                ]
            }
        ]
    }))
});

// Attach the policy to the user
new aws.iam.UserPolicyAttachment("my-user-policy-attachment", {
    user: user.name,
    policyArn: policy.arn,
});

// Export the bucket name and user name
export const bucketName = bucket.bucket;
export const userName = user.name;

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