How do I output an object in TypeScript?
In this example, we’ll learn how to define and output an object in TypeScript when managing infrastructure as code. We’ll use basic resource definitions in TypeScript and focus on creating and exporting an object that can be used for further processing or reference.
Steps Explained:
- Define Resources: Create a VPC resource in AWS.
- Extract Properties: Capture properties like VPC ID and CIDR block into an object.
- Output the Object: Export this object as a stack output for use in other parts of the application or future deployments.
Below is the TypeScript program that achieves this:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const example = new aws.ec2.Vpc("example", {
cidrBlock: "10.0.0.0/16",
tags: {
Name: "example-vpc",
},
});
export const vpcInfo = {
id: example.id,
cidrBlock: example.cidrBlock,
};
Summary
We created a VPC resource and exported an object containing its ID and CIDR block attributes. This enables us to reuse these attributes across our infrastructure securely and efficiently.
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.