8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Misc » Here

Terraform : Oracle Cloud Infrastructure (OCI) Object Storage Bucket

This article describes how to create an object storage bucket on Oracle Cloud Infrastructure (OCI) using Terraform.

Related articles.

Create Working Directory

Create a new working directory and switch to that directory.

mkdir \git\oraclebase\terraform\oci\oci_bucket
cd \git\oraclebase\terraform\oci\oci_bucket

In a previous article (here) we discussed the creation of an OCI provider. Copy the OCI provider information into this new working directory.

copy \git\oraclebase\terraform\oci\oci_provider\*.tf .
copy \git\oraclebase\terraform\oci\oci_provider\*.tfvars .

oci_bucket.tf

Create a file called "oci_container.tf" with the following contents.

# Variables
variable "compartment_id"   { type = string }
variable "bucket_name"      { type = string }
variable "bucket_namespace" { type = string }

variable "bucket_access_type" {
  type    = string
  default = "NoPublicAccess"
}


# Resources
resource "oci_objectstorage_bucket" "tf_bucket" {
  compartment_id = var.compartment_id
  name           = var.bucket_name
  namespace      = var.bucket_namespace
  access_type    = var.bucket_access_type
}


# Outputs
output "bucket_name" {
  value = oci_objectstorage_bucket.tf_bucket.name
}

output "bucket_id" {
  value = oci_objectstorage_bucket.tf_bucket.bucket_id
}

The file begins with variable definitions. We could set default values for these variables, or use literal values directly in the provider definition, but we don't want sensitive information checked into version control, so it makes sense to separate out variable values from the script. The resources section defines the object storage bucket using the input variables. The outputs section allows us to see information about the compartment that's been created, including the ID of the bucket.

The variables, resources and outputs sections can be split into separate files if you find that organisation easier. It may help for more complex definitions.

The full list of parameters and outputs available can be found here.

oci_bucket_variables.auto.tfvars

There are a number of ways to supply values for input variables (see here). In this example we'll use a ".auto.tfvars" file. We won't check this script into version control as it contains sensitive information.

Create a file called "oci_bucket_variables.auto.tfvars" with the following contents. Adjust the values to match your desired compartment and bucket namespace details.

compartment_id     = "ocid1.compartment.oc1..aaaaaaaa..."
bucket_name        = "ob-bucket2"
bucket_namespace   = "lrxjmaqkl..."

The compartment_id is the OCID of the compartment that will house the VCN. You must adjust it with a valid value from your Oracle Cloud account. You would not normally use the root compartment for this. You can get the ID of a compartment from your Oracle Cloud account as follows.

The bucket_namespace is assigned when you create an Oracle Cloud account. You can find it in your tenanct details.

Build the OCI Object Storage Bucket

Initialize the working directory using the terraform init command.

terraform init

Use the terraform plan command to test the execution plan.

terraform plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # oci_objectstorage_bucket.tf_bucket will be created
  + resource "oci_objectstorage_bucket" "tf_bucket" {
      + access_type                  = "NoPublicAccess"
      + approximate_count            = (known after apply)
      + approximate_size             = (known after apply)
      + bucket_id                    = (known after apply)
      + compartment_id               = "ocid1.compartment.oc1..aaaaaaaa..."
      + created_by                   = (known after apply)
      + defined_tags                 = (known after apply)
      + etag                         = (known after apply)
      + freeform_tags                = (known after apply)
      + id                           = (known after apply)
      + is_read_only                 = (known after apply)
      + kms_key_id                   = (known after apply)
      + name                         = "ob-bucket2"
      + namespace                    = "lrxjmaqkl..."
      + object_events_enabled        = (known after apply)
      + object_lifecycle_policy_etag = (known after apply)
      + replication_enabled          = (known after apply)
      + storage_tier                 = (known after apply)
      + time_created                 = (known after apply)
      + versioning                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + bucket_id   = (known after apply)
  + bucket_name = "ob-bucket2"

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Use the terraform apply command to create the OCI compartment.

terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # oci_objectstorage_bucket.tf_bucket will be created
  + resource "oci_objectstorage_bucket" "tf_bucket" {
      + access_type                  = "NoPublicAccess"
      + approximate_count            = (known after apply)
      + approximate_size             = (known after apply)
      + bucket_id                    = (known after apply)
      + compartment_id               = "ocid1.compartment.oc1..aaaaaaaa..."
      + created_by                   = (known after apply)
      + defined_tags                 = (known after apply)
      + etag                         = (known after apply)
      + freeform_tags                = (known after apply)
      + id                           = (known after apply)
      + is_read_only                 = (known after apply)
      + kms_key_id                   = (known after apply)
      + name                         = "ob-bucket2"
      + namespace                    = "lrxjmaqkl..."
      + object_events_enabled        = (known after apply)
      + object_lifecycle_policy_etag = (known after apply)
      + replication_enabled          = (known after apply)
      + storage_tier                 = (known after apply)
      + time_created                 = (known after apply)
      + versioning                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + bucket_id   = (known after apply)
  + bucket_name = "ob-bucket2"

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

oci_objectstorage_bucket.tf_bucket: Creating...
oci_objectstorage_bucket.tf_bucket: Creation complete after 0s [id=n/lrxjmaqkl.../b/ob-bucket2]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

bucket_id = "ocid1.bucket.oc1.uk-london-1.aaaaaaaa..."
bucket_name = "ob-bucket2"

Check the Oracle Cloud account to see the new compartment.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.