1. Answers
  2. Output an Object in TypeScript

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:

  1. Define Resources: Create a VPC resource in AWS.
  2. Extract Properties: Capture properties like VPC ID and CIDR block into an object.
  3. 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 up

New to Pulumi?

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

Sign up