This sample provides an end-to-end walkthrough of the tasks required to create and deploy an Oracle Cloud Infrastructure compute instance using Resource Manager.
This page helps you get started with Resource Manager. Use this end-to-end walkthrough of tasks to create and deploy an Oracle Cloud Infrastructure compute instance using either a prebuilt Terraform configuration or your own Terraform configuration. For a brief introduction to Resource Manager, see Overview of Resource Manager.
Highlights
In addition to providing a prebuilt Terraform configuration for creating a Compute instance, this walkthrough provides samples that
demonstrate how to write a Terraform configuration. Whichever configuration you use
(prebuilt or your own), Resource Manager uses Terraform
to provision the defined resources . The resources are organized into stacks, which you
create and provision using jobs.
The walkthrough covers the following tasks:
Select or create a Terraform configuration.
Provision the infrastructure:
Create a stack in which to provision your infrastructure.
Run a plan job against your stack, which parses your configuration and creates an execution plan.
Review the generated execution plan.
Run an apply job against your stack, which provisions your resources. The apply job follows the execution plan, which is based on your Terraform configuration.
Review the resulting infrastructure.
Optionally provision the infrastructure in more environments, using the same
Terraform configuration.
Before You Begin 🔗
Ensure that you have installed, obtained, or created the prerequisites:
An Oracle Cloud Infrastructure tenancy for each environment where you want to provision resources. For example, you might provision the resources defined in a Terraform configuration to development, staging, and production environments.
Note
It is a best practice to locate each environment in its own tenancy.
The OCID for the compartment where you wish to create your stack.
If you want to use the Oracle Cloud Infrastructure CLI, install and configure the CLI first. See Quickstart and Configuring the CLI
Task 1a: Select a prebuilt Terraform Configuration 🔗
You can select the compute instance template with its prebuilt Terraform configuration instead of writing your own configuration. These steps guide you through the stack creation process.
Select the following link to launch the Create stack page with the compute instance template already selected.
In the Create stack page, enter a Name for the new stack (or accept the default name provided). Avoid entering confidential information.
From the Create in compartment drop-down, select the compartment where you want to create the stack.
A compartment from the list scope is set by default.
Select Next.
The Configure variables panel displays variables from the Terraform configuration.
Review the variables and make changes as necessary.
Important
Do not add your private key or other confidential information
to configuration variables.
Select Next.
In the Review panel, verify your stack configuration.
For purposes of this walkthrough, leave Run apply blank. (Use this option to automatically provision the infrastructure when the stack is created.)
Select Create to create your stack.
The stack details page for the new stack appears.
Congratulations! You have created a stack with the prebuilt Terraform configuration from the compute instance template. The next step is to provision the infrastructure.
Task 1b: Create Your Own Terraform Configuration 🔗
If you didn't select a prebuilt Terraform configuration, then follow these
steps to write your own.
A Terraform configuration is a file that codifies your infrastructure. The configuration
defines your Terraform provider, the resources you intend to provision, variables, and
specific instructions for provisioning the resources.
This page guides you through selecting the compute Instance template with its prebuilt Terraform configuration or alternatively writing your own configuration using several .tf files within a .zip file.
The following code sample creates a basic Oracle Cloud Infrastructure Terraform provider. You can provide values as variables that are defined either in a variables file or in the provider definition (.tf) file. For more information, see Provider Configuration.
Define the variables you want to use when provisioning your resources. A best practice is to create a "variables" file in the configuration package that you upload. Following is an example from a configuration file that we've named variables.tf. For more information about using variables, see Define input variables. See also Input Variables.
With a schema
document, you can reuse one, unedited Terraform configuration in development,
staging, and production environments. Resource Manager prompts
you for variable values when you create a stack with a Terraform configuration that includes a
schema document.
Schema documents are recommended for Terraform configurations when using Resource Manager. Including a schema document allows you to extend pages in the Oracle Cloud Infrastructure
Console. Facilitate variable entry in the Create stack page by surfacing SSH key controls and by naming, grouping, dynamically prepopulating values, and more. Define text in the Application Information tab of the Stack details page that opens for a created stack.
Following are contents of an example schema document (schema.yaml)
that covers the basic details in this scenario.
The following extended code example creates an Oracle Cloud Infrastructure compute instance. The code also references the image on which the compute instance is created, sets boot volume size, adds essential metadata, and applies both free-form and defined tags.
Copy
resource "oci_core_instance" "TFInstance" {
count = "${var.NumInstances}"
availability_domain = "${var.localAD}"
compartment_id = "${var.compartment_ocid}"
display_name = "TFInstance${count.index}"
shape = "${var.InstanceShape}"
create_vnic_details {
subnet_id = "${oci_core_subnet.ExampleSubnet.id}"
display_name = "primaryvnic"
assign_public_ip = true
hostname_label = "tfexampleinstance${count.index}"
}
source_details {
source_type = "image"
source_id = "${var.InstanceImageOCID[var.region]}"
# Apply this to set the size of the boot volume that's created for this instance.
# Otherwise, the default boot volume size of the image is used.
# This should only be specified when source_type is set to "image".
#boot_volume_size_in_gbs = "60"
}
# Apply the following flag only if you wish to preserve the attached boot volume upon destroying this instance
# Setting this and destroying the instance will result in a boot volume that should be managed outside of this config.
# When changing this value, make sure to run 'terraform apply' so that it takes effect before the resource is destroyed.
#preserve_boot_volume = true
metadata = {
ssh_authorized_keys = "${var.ssh_public_key}"
}
timeouts {
create = "60m"
}
}
Ensure that all of the configuration files are in a single directory. You can store your
Terraform configuration file locally or in a source code control system. For more
information on storing your file in a source code control system, see Managing Configuration Source Providers. Wherever your file is
stored, you can select it when creating a stack using the CLI or Console.
Use your Terraform configuration to build and deploy your infrastructure by taking the following actions:
If you created your own Terraform configuration, follow these
steps to create a stack in a tenancy compartment of your choosing. (If you
selected a prebuilt configuration, skip this step.)
A stack is a collection of resources that you can act on as a group. All of the
resources that you specify in your configuration are provisioned in the stack
that you create.
You can create a stack from a remote, versioned file in a source code control system (such as Git), an Object Storage bucket, or a locally accessed .zip file that you upload. Following are instructions for a local file.
On Windows, be sure the zip file and variables.json files are in the same directory from which you're running the CLI. The CLI currently has a limitation on Windows that prevents correct handling of the files if either one is in a subdirectory.
oci resource-manager stack create --compartment-id ocid1.tenancy.oc1..uniqueid --config-source vcn.zip --variables file://variables.json --display-name "My Example Stack" --description "My Tutorial to Create a VCN" --working-directory ""
{
"data": {
config-source":
{
"working-directory": null,
"config-source-type": "ZIP_UPLOAD"
},
"defined-tags": {},
"description": "My Tutorial to Create a VCN",
"display-name": "My Example Stack",
"freeform-tags": {},
"id": "ocid1.ormstack.oc1..uniqueid",
"lifecycle-state": "ACTIVE",
"time-created": "2019-04-03T18:26:56.299000+00:00",
"variables":
{
"compartment_ocid": "ocid1.compartment.oc1..uniqueid",
"region": "us-phoenix-1"
}
}
}
Generate an execution plan.
The plan job parses your configuration to create an "execution plan," which is a step-by-step representation of the planned deployment in job log entries. Once the plan job has completed, you can evaluate the execution plan by viewing the job's log entries to confirm that it performs the expected operations, and in the intended sequence.
Note
You can skip this step if you selected Run apply when you created the stack. In this case, the resources have already been provisioned.
Open the navigation menu and select Developer Services. Under Resource Manager, select Stacks.
Under List Scope, select a compartment that you have permission to work in.
Select the name of the stack that you want.
The Stack details page opens.
Select Plan.
In the Plan panel, review the Name and optionally change it.
Select Plan.
The plan job is created. The new job is listed under Jobs.
Monitor the job status (lifecycle state) by getting the job's details. Succeeded (SUCCEEDED) indicates that the job has completed. Depending on the complexity of the job, the operation can take some time. While the job runs, or after it finishes, you can get the job logs content.
Monitor the job status (lifecycle state) by getting the job's details. Succeeded (SUCCEEDED) indicates that the job has completed. Depending on the complexity of the job, the operation can take some time. While the job runs, or after it finishes, you can get the job logs content.
Review the execution plan to confirm that it represents your intentions.
The execution plan is represented in the log for the plan job you ran previously.
Note
You can skip this step if you selected Run apply when you created the stack. In this case, the resources have already been provisioned.
Open the navigation menu and select Developer Services. Under Resource Manager, select Jobs.
You can also access jobs from a stack detail page. Select Stacks and then select the name of the stack you want.
Under List Scope, select a compartment that you have permission to work in.
Select the name of the plan job that you ran.
The Job details page opens. Logs are visible (in the Logs section under Resources).
For plan jobs, the log file is the execution plan. View the log file for the plan job and note the "message" fields in the sequence of log entries of the log file. These values represent the sequence of operations specified in your configuration.
(Optional) Select Download logs (in the Logs section under Resources).
The command returns JSON objects that describe log entries. Each object has a message member with a property that displays one line of the execution plan. In this example, the plan job creates a single virtual cloud network (VCN); the remaining members show details about the VCN.
Provision your resources by running an apply job against the execution plan.
When satisfied with the execution plan, you're ready to do the work of provisioning the stack with the resources that you've defined. The apply job takes the execution plan and "applies" it to the stack. The result is a fully provisioned stack.
Note
You can skip this step if you selected Run apply when you created the stack. In this case, the resources have already been provisioned.
Open the navigation menu and select Developer Services. Under Resource Manager, select Stacks.
Under List Scope, select a compartment that you have permission to work in.
Select the name of the stack that you created.
The Stack details page opens.
Select Apply.
(Optional) In the Apply panel, review the apply job Name and other settings and update if needed.
Select Apply.
The apply job is created. The new job is listed under Jobs.
Monitor the job status (lifecycle state) by getting the job's details. Succeeded (SUCCEEDED) indicates that the job has completed. Depending on the complexity of the job, the operation can take some time. While the job runs, or after it finishes, you can get the job logs content.
Monitor the job status (lifecycle state) by getting the job's details. Succeeded (SUCCEEDED) indicates that the job has completed. Depending on the complexity of the job, the operation can take some time. While the job runs, or after it finishes, you can get the job logs content.
View the log file and note the "message" fields in the sequence of log entries of the log file. You can view the log file for the specified job as either a paged list of entries or in its raw form.
The command returns JSON objects that describe log entries. Each object has a message member with a property that displays one line of the execution plan. In this example, the plan job creates a single virtual cloud network (VCN); the remaining members show details about the VCN.
The job state file represents the job's output in JSON format.
The state file maps your stack's resources to your configuration and also maintains essential configuration metadata, such as resource dependencies. Resource Manager generates and updates state files automatically when you run jobs.
The Resource Manager supports state locking by allowing only one job at a time to run on a given stack. For more information about state files, see State.
You can also import state files for resources already managed by Terraform.
When you need to release the resources that you provisioned, run a destroy job on the stack.
A destroy job tears down the stack that you created and then cleans up associated resources without deleting them. For example, the destroy job terminates Compute instances associated with the stack.
Note
We recommend running a destroy job before deleting a stack to release associated resources first. When you delete a stack, its associated state file is also deleted; therefore, you lose track of the state of its associated resources. Cleaning up resources associated with a deleted stack can be difficult without the state file, especially when those resources are spread across multiple compartments. To avoid difficult cleanup later, we recommend that you release associated resources first by running a destroy job.
Data cannot be recovered from destroyed resources.
Open the navigation menu and select Developer Services. Under Resource Manager, select Stacks.
Under List Scope, select a compartment that you have permission to work in.
Select the name of the stack that you want.
The Stack details page opens.
Select Destroy.
(Optional) In the Destroy panel, review the apply job Name and other settings and update if needed.
Select Destroy.
The destroy job is created. The new job is listed under Jobs.
After running a destroy job, get the job to check its status. You can optionally view the Terraform state file, view the logs, and confirm deletion of the resources. You can also re-create destroyed resources.
Monitor the job status (lifecycle state) by getting the job's details. Succeeded (SUCCEEDED) indicates that the job has completed. Depending on the complexity of the job, the operation can take some time. While the job runs, or after it finishes, you can get the job logs content.
To view the Terraform state file (shows the state of your resources after running the job), select the name of the job to display the Job details page, then select View state under Resources. Optionally select Show changes in this version.
To view the logs for the job, select the job to open its details page, then select Logs under Resources.
To re-create a stack's resources after the resources are destroyed, run an apply job. The new resources differ from previously destroyed resources by their unique OCIDs and other metadata.
After running a destroy job, get the job to check its status.
Monitor the job status (lifecycle state) by getting the job's details. Succeeded (SUCCEEDED) indicates that the job has completed. Depending on the complexity of the job, the operation can take some time. While the job runs, or after it finishes, you can get the job logs content.
To re-create a stack's resources after the resources are destroyed, run an apply job. The new resources differ from previously destroyed resources by their unique OCIDs and other metadata.
Task 3: Repeat in More Environments 🔗
This section describes how to build and deploy infrastructure in multiple
environments.
In this scenario, you use the same Terraform configuration .zip file to provision a Compute instance in your development, staging, and
production environments.
Note
This scenario assumes that the Terraform configuration includes a schema document, which
allows you to change variable values when creating a stack in the Console.
Access the tenancy for the new environment where you want to provision the
infrastructure defined in your Terraform configuration.
For example, access the tenancy for your staging or production environment.
Open the Create stack page:
Open the navigation menu and select Developer Services. Under Resource Manager, select Stacks.
Under List Scope, select a compartment that you have permission to work in.The page updates to display only the resources in that compartment. If you're not sure which compartment to use, contact an administrator.
Select Create stack.
Using the same Terraform configuration as for the first environment, complete the Stack information tab:
On the Create stack page, select My configuration.
Under Stack configuration, select .Zip file and add the Terraform configuration.
You can either drag and drop your Terraform configuration .zip file onto the control or select Browse and navigate to the location of the .zip file.
Enter a Name for the new stack (or accept the default name provided). Avoid entering confidential information.
Optionally enter a Description.
From the Create in compartment drop-down, select the compartment where you want to create the stack.
Select Next.
The Configure variables panel displays variables from the selected Terraform configuration file.
Specify the variable values for this environment:
In the Configure variables panel, review the variables and make changes as necessary.
Do not add your private key or other confidential
information to configuration variables.
Select Next.
In the Review panel, verify your stack configuration.
To automatically provision resources on creation of the stack, select Run apply.
Select Create to create your stack.
The stack details page for the new stack appears.
If you selected Run apply, then Resource Manager runs the apply action on the new stack.
Congratulations, you have reused your Terraform configuration to create a stack in a new environment. If you selected Run apply, then you also provisioned resources in the new environment.
You can now generate and review an execution plan (and provision resources, if Run apply wasn't selected). To complete these items, repeat the steps from Task 2: Provision the Infrastructure in the new environment.