Creating nested compartments within an OCI tenancy
TypeScriptTo create nested compartments within an Oracle Cloud Infrastructure (OCI) tenancy using Pulumi, you'll use the
oci.Identity.Compartment
resource. This resource allows you to create and manage compartments, which are containers to organize your cloud resources. You use compartments to separate and control access to your resources, enabling you to manage resources' lifecycles and access control.Below is a Pulumi program in TypeScript that demonstrates how to create nested compartments in an OCI tenancy. The program assumes that you've already set up your OCI provider configuration. Each compartment creation requires the parent compartment's OCID and other details like the name and description for the new compartment. The
enableDelete
property allows you to specify if the compartment can be deleted. Setting it totrue
allows deletion whilefalse
prevents it.The OCI resources are defined as classes within a TypeScript program, and you create instances of these classes to build your desired infrastructure. In the provided example, we will define a top-level compartment and then a nested compartment within it.
Here's the program:
import * as pulumi from "@pulumi/pulumi"; import * as oci from "@pulumi/oci"; // Initialize a new Pulumi project for OCI. // The provider configuration like region, tenancy OCID, user OCID, private key path, and fingerprint // should be set up in the Pulumi configuration or environment variables. // Create a top-level compartment. const topLevelCompartment = new oci.Identity.Compartment("topLevelCompartment", { compartmentId: oci.config.tenancyOcid, // Replace with your tenancy OCID if not set in the Pulumi config name: "TopLevelCompartment", description: "This is a top-level compartment", // For demonstration purposes enableDelete is set to true. enableDelete: true, freeformTags: { "TagKey": "TagValue", }, definedTags: { "TagNamespace": { "TagKey": "TagValue", }, }, }); // Output the OCID of the created top-level compartment export const topLevelCompartmentOCID = topLevelCompartment.id; // Create a nested compartment within the top-level compartment. const nestedCompartment = new oci.Identity.Compartment("nestedCompartment", { compartmentId: topLevelCompartment.id, name: "NestedCompartment", description: "This is a nested compartment inside the TopLevelCompartment", // For demonstration purposes enableDelete is set to true. enableDelete: true, freeformTags: { "TagKey": "NestedTagValue", }, definedTags: { "TagNamespace": { "TagKey": "NestedTagValue", }, }, }, { dependsOn: [topLevelCompartment] }); // Ensure the nested compartment is created after the parent compartment // Output the OCID of the created nested compartment export const nestedCompartmentOCID = nestedCompartment.id;
In the above program:
- We create two compartments by making two instances of the
oci.Identity.Compartment
. - In the nested compartment resource, we use the
dependsOn
option to ensure that it doesn't get created until after the top-level compartment is created. This establishes an explicit dependency between the compartments. - The
compartmentId
is the parent compartment for which a new compartment is created. For the top-level compartment, it is thetenancyOcid
; for the nested compartment, it's the OCID of the parent compartment created before it. freeformTags
anddefinedTags
are optional and used for assigning tags to compartments.- We output the OCID of each compartment using
export
which enables the OCID to be retrieved by other Pulumi stacks or for reference once the deployment is completed.
Remember to replace
oci.config.tenancyOcid
with your actual tenancy OCID if you have not set it up in the Pulumi configuration.After writing this program, save it to a file (for example
index.ts
), and then run the following commands using your terminal:pulumi up
This command will start the provisioning process, creating the specified resources within your OCI tenancy. Make sure you review the plan shown by Pulumi before confirming the deployment to avoid any unintended changes to your cloud infrastructure.
After successful deployment, you will see the outputs including
topLevelCompartmentOCID
andnestedCompartmentOCID
, which are the OCIDs for the compartments you've just created. You can now use these OCID values to organize your resources in OCI.- We create two compartments by making two instances of the