Query data with outputs
In this tutorial, you will use output values to display data about your infrastructure back to the Terraform user.
If you haven't yet completed the Define Input Variables tutorial, do so before following this one.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorial, which we highly recommend. If not, we've summarized the prerequisites here for your convenience. To follow this tutorial you will need:
OCI Tenancy. Note your region, you will use it throughout the tutorial.
The Terraform CLI installed.
The OCI CLI installed. Configure the CLI with a token by following the session authentication flow.
$ oci session authenticate
Create a directory named
learn-terraform-oci
and paste the following configuration into a file namedmain.tf
.terraform { required_providers { oci = { source = "oracle/oci" } }} provider "oci" { region = "us-sanjose-1" auth = "SecurityToken" config_file_profile = "learn-terraform"} resource "oci_core_vcn" "internal" { dns_label = "internal" cidr_block = "172.16.0.0/20" compartment_id = "<your_compartment_OCID_here>" display_name = "My internal VCN"} resource "oci_core_subnet" "dev" { vcn_id = oci_core_vcn.internal.id cidr_block = "172.16.0.0/24" compartment_id = "<your_compartment_OCID_here>" display_name = "Dev subnet" prohibit_public_ip_on_vnic = true dns_label = "dev"}
Customize the
region
and both instances ofcompartment_id
to match the ones listed on your OCI tenancy page in the web console.Initialize the configuration.
$ terraform init
Apply the configuration.
$ terraform apply
Once you have successfully initialized the configuration, you can continue with the rest of this tutorial.
Output VCN state and CIDR
Create a file called outputs.tf
in your learn-terraform-oci
directory.
Add outputs to the new file for your VCN's status and CIDR block.
output "vcn_state" { description = "The state of the VCN." value = oci_core_vcn.internal.state} output "vcn_cidr" { description = "CIDR block of the core VCN" value = oci_core_vcn.internal.cidr_block}
Inspect output values
You must apply this configuration before you can use these output values. Apply
your configuration now. Respond to the confirmation prompt with yes
.
$ terraform applyoci_core_vcn.internal: Refreshing state... [id=ocid1.vcn.oc1.us-sanjose-1.amaaaaaapqqlmeyagheb4xftk3iqcjph3qhjaqoannwsbxq7bvcvyum53yba]oci_core_subnet.dev: Refreshing state... [id=ocid1.subnet.oc1.us-sanjose-1.aaaaaaaaciqn5icxe2x3jbe6hzbjrle7ghbspn5s4d3ybpxr2ofuzudr4vya] Changes to Outputs: + vcn_cidr = "172.16.0.0/20" + vcn_state = "AVAILABLE" You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: vcn_cidr = "172.16.0.0/20"vcn_state = "AVAILABLE"
Terraform prints output values to the screen when you apply your configuration.
Query the outputs with the terraform output
command.
$ terraform outputvcn_cidr = "172.16.0.0/20"vcn_state = "AVAILABLE"
You can use Terraform outputs to connect your Terraform projects with other parts of your infrastructure, or with other Terraform projects. To learn more read the documentation on Outputs.
Destroy infrastructure
Destroy your infrastructure. Respond to the confirmation prompt with yes
.
$ terraform destroy An execution plan has been generated and is shown below.Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # oci_core_subnet.dev will be destroyed - resource "oci_core_subnet" "dev" { - cidr_block = "172.16.0.0/24" -> null - compartment_id = "ocid1.tenancy.oc1...." -> null - defined_tags = { - "Oracle-Tags.CreatedBy" = "oracleidentitycloudservice/redacted" - "Oracle-Tags.CreatedOn" = "2021-04-08T18:55:39.869Z" } -> null - dhcp_options_id = "ocid1.dhcpoptions.oc1.us-sanjose-1.aaaaaaaadq5ehpklbvpzto5uvi6x7s3s3lzwi6z6jboyavrlr7y2hrdhsiia" -> null - display_name = "Dev subnet" -> null - dns_label = "dev" -> null - freeform_tags = {} -> null - id = "ocid1.subnet.oc1.us-sanjose-1.aaaaaaaaciqn5icxe2x3jbe6hzbjrle7ghbspn5s4d3ybpxr2ofuzudr4vya" -> null - prohibit_public_ip_on_vnic = true -> null - route_table_id = "ocid1.routetable.oc1.us-sanjose-1.aaaaaaaapi3xavnr7izylw5iztlfggeqoql6l6gq7shychb5jc32ipgvhpna" -> null - security_list_ids = [ - "ocid1.securitylist.oc1.us-sanjose-1.aaaaaaaajrxazkgqbck7qmhtuevozf3rdeap7w3c5p2s2gkuhifyshvcj4ta", ] -> null - state = "AVAILABLE" -> null - subnet_domain_name = "dev.internal.oraclevcn.com" -> null - time_created = "2021-04-08 18:55:39.894 +0000 UTC" -> null - vcn_id = "ocid1.vcn.oc1.us-sanjose-1.amaaaaaapqqlmeyagheb4xftk3iqcjph3qhjaqoannwsbxq7bvcvyum53yba" -> null - virtual_router_ip = "172.16.0.1" -> null - virtual_router_mac = "00:00:17:9C:FD:5A" -> null } # oci_core_vcn.internal will be destroyed - resource "oci_core_vcn" "internal" { - cidr_block = "172.16.0.0/20" -> null - cidr_blocks = [ - "172.16.0.0/20", ] -> null - compartment_id = "ocid1.tenancy.oc1...." -> null - default_dhcp_options_id = "ocid1.dhcpoptions.oc1.us-sanjose-1.aaaaaaaadq5ehpklbvpzto5uvi6x7s3s3lzwi6z6jboyavrlr7y2hrdhsiia" -> null - default_route_table_id = "ocid1.routetable.oc1.us-sanjose-1.aaaaaaaapi3xavnr7izylw5iztlfggeqoql6l6gq7shychb5jc32ipgvhpna" -> null - default_security_list_id = "ocid1.securitylist.oc1.us-sanjose-1.aaaaaaaajrxazkgqbck7qmhtuevozf3rdeap7w3c5p2s2gkuhifyshvcj4ta" -> null - defined_tags = { - "Oracle-Tags.CreatedBy" = "oracleidentitycloudservice/redacted" - "Oracle-Tags.CreatedOn" = "2021-04-08T18:55:39.311Z" } -> null - display_name = "My internal VCN" -> null - dns_label = "internal" -> null - freeform_tags = {} -> null - id = "ocid1.vcn.oc1.us-sanjose-1.amaaaaaapqqlmeyagheb4xftk3iqcjph3qhjaqoannwsbxq7bvcvyum53yba" -> null - state = "AVAILABLE" -> null - time_created = "2021-04-08 18:55:39.317 +0000 UTC" -> null - vcn_domain_name = "internal.oraclevcn.com" -> null } Plan: 0 to add, 0 to change, 2 to destroy. Changes to Outputs: - vcn_cidr = "172.16.0.0/20" -> null - vcn_state = "AVAILABLE" -> null Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes oci_core_subnet.dev: Destroying... [id=ocid1.subnet.oc1.us-sanjose-1.aaaaaaaaciqn5icxe2x3jbe6hzbjrle7ghbspn5s4d3ybpxr2ofuzudr4vya]oci_core_subnet.dev: Destruction complete after 7soci_core_vcn.internal: Destroying... [id=ocid1.vcn.oc1.us-sanjose-1.amaaaaaapqqlmeyagheb4xftk3iqcjph3qhjaqoannwsbxq7bvcvyum53yba]oci_core_vcn.internal: Destruction complete after 1s Destroy complete! Resources: 2 destroyed.
Next steps
Over the course of these tutorials you created, modified, and destroyed infrastructure as code using the Terraform CLI. You made your configurations more flexible with inputs, and more informative with outputs.
That concludes the getting started tutorials for Terraform. Hopefully you're now able to not only see what Terraform is useful for, but you're also able to use this knowledge to improve building your own infrastructure.
To learn more of the Terraform basics, refer to the following resources:
Configuration language tutorial tutorials - Get more familiar with variables, outputs, dependencies, meta-arguments, and other language features to write more sophisticated Terraform configurations. Reference documentation is also available.
Modules documentation - Organize and re-use Terraform configuration with modules.
OCI provider documentation - Learn about all the resources and modules available in the OCI provider.
OCI documentation - Learn more about OCI itself.