|
| 1 | +--- |
| 2 | +layout: "oci" |
| 3 | +page_title: "Provider: Oracle Cloud Infrastructure" |
| 4 | +sidebar_current: "docs-oci-guide-object_store_backend" |
| 5 | +description: |- |
| 6 | + The Oracle Cloud Infrastructure provider. Object Store Backend |
| 7 | +--- |
| 8 | + |
| 9 | +## Using the Object Store for Terraform State Files |
| 10 | +You can store [Terraform state files](https://www.terraform.io/docs/state/index.html) in the |
| 11 | +Oracle Cloud Infrastructure Object Storage. Doing so requires that you configure a backend using one of the Terraform backend types. |
| 12 | + |
| 13 | +Terraform supports various backend types to allow flexibility in how state files are loaded into Terraform. (For more |
| 14 | +information, see [Terraform Backend Types](https://www.terraform.io/docs/backends/types/index.html).) For our purposes, we address two of these approaches: |
| 15 | + |
| 16 | +- Using an HTTP remote state backend |
| 17 | +- Using an S3-compatible remote state backend |
| 18 | + |
| 19 | +### Using an HTTP Backend |
| 20 | + |
| 21 | +Using the [HTTP backend type](https://www.terraform.io/docs/backends/types/http.html) allows you to store state using a simple REST client. With the HTTP backend type, you can |
| 22 | +easily fetch, update, and purge state using the HTTP GET, POST, and DELETE methods. |
| 23 | + |
| 24 | +To configure the HTTP backend to store your Oracle Cloud Infrastructure Terraform state files, do the following: |
| 25 | + |
| 26 | + |
| 27 | +#### Create a Pre-Authenticated Request |
| 28 | + |
| 29 | +Creating a pre-authenticated request in Oracle Object Storage enables accessing a bucket or object in the Oracle Cloud |
| 30 | +Infrastructure without needing to provide credentials. To do so, you must create a pre-authenticated request that has |
| 31 | +read/write permissions to the object store where you intend to save the Terraform state file. You can do so in any of |
| 32 | +three ways: by using the Console UI, by using the command line interface (CLI), or by using the REST APIs. |
| 33 | + |
| 34 | +> **Note** |
| 35 | +A state file must exist in the bucket before you create the pre-authenticated request. This file can be an existing state file, or an empty file for the initial state. |
| 36 | + |
| 37 | +For guidance, see [Using Pre-Authenticated Requests](https://docs.cloud.oracle.com/iaas/Content/Object/Tasks/usingpreauthenticatedrequests.htm). |
| 38 | + |
| 39 | + |
| 40 | +#### Upload Existing State |
| 41 | + |
| 42 | +If you have an existing state file, you can upload it using Curl to make an HTTP Put request to the object store URL, as shown here: |
| 43 | + |
| 44 | +```sh |
| 45 | +curl -X PUT -H "Content-Type: text/plain" --data-binary "@path/to/local/tfstate" http://<prefix>/<my-access-uri> |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +#### Configure HTTP as a Terraform Backend |
| 50 | + |
| 51 | +The [HTTP backend type](https://www.terraform.io/docs/backends/types/http.html) stores state using a simple REST client |
| 52 | +and allows you to easily fetch, update, and purge state using the HTTP GET, POST, and DELETE methods. |
| 53 | + |
| 54 | +The access URI for addressing Oracle Cloud Infrastructure Terraform configurations must be of the form: |
| 55 | +https://objectstorage.us-phoenix-1.oraclecloud.com/my-access-uri (where region and access URI are specific to you). |
| 56 | + |
| 57 | +For more example configuration and state files that reference code, and a summary of configuration variables, |
| 58 | +see [Standard Backends: HTTP](https://www.terraform.io/docs/backends/types/http.html). |
| 59 | + |
| 60 | +Following is an example Terraform configuration. The region in the URL can be something other than the Phoenix region. |
| 61 | + |
| 62 | +```hcl-terraform |
| 63 | +terraform { |
| 64 | + backend "http" { |
| 65 | + address = "https://objectstorage.us-phoenix-1.oraclecloud.com/<my-access-uri>" update_method = "PUT" } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +#### Reinitialize Terraform |
| 71 | + |
| 72 | +Finally, you must reinitialize Terraform and then run the apply command, as shown following. |
| 73 | + |
| 74 | +```sh |
| 75 | +terraform init |
| 76 | +terraform apply |
| 77 | +``` |
| 78 | + |
| 79 | +After completing these steps, you are able to use Oracle Cloud Infrastructure as the backend for storing Terraform state files. |
| 80 | + |
| 81 | + |
| 82 | +### Using an S3-Compatible Backend |
| 83 | + |
| 84 | +Configuring the S3-compatible backend requires that the account be enabled with S3 authentication keys, which are set on a per-user basis. |
| 85 | + |
| 86 | +1. In the Console, open the navigation menu, then, under Governance and Administration, navigate to Identity, then Users. |
| 87 | +Under User Details, click Amazon S3 Compatibility API Keys. For more guidance, |
| 88 | +see [Working with Amazon S3 Compatibility API Keys](https://docs.cloud.oracle.com/Content/Identity/Tasks/managingcredentials.htm#s3). |
| 89 | + |
| 90 | +2. Set the location for the credentials file. The default location is `~/.aws/credentials`. You can set an alternate location by using the S3 backend `shared_credentials_file` option. |
| 91 | + |
| 92 | + > **Warning** |
| 93 | + Never set the access_key and the secret_key attributes in the same Terraform backend configuration, since this creates a security risk. |
| 94 | + |
| 95 | +3. Configure the `[default]` entry in the credentials file with the appropriate object storage credentials. |
| 96 | +The file can contain any number of credential profiles. If you provide a different profile name, you must also |
| 97 | +update the backend `profile` option in your Terraform configuration file. |
| 98 | + |
| 99 | + Following is an example of Object Storage credentials: |
| 100 | + |
| 101 | + ``` |
| 102 | + [default] |
| 103 | + aws_access_key_id=ocid1.credential.oc1..aaaaaaaasbmhehdmefolvqwtbdjgwfsxjsgxgipdbph7odn2brgurgsyztca |
| 104 | + aws_secret_access_key=mSTdaWhlbWj3ty4JZXlm0NUZV52xlImWjayJLJ6OH9A= |
| 105 | + ``` |
| 106 | + |
| 107 | + Where `aws_access_key_id and aws_secret_access_key` are user-specific values provided from the Console. |
| 108 | + The key values provided in the example are not valid and provided as examples only. |
| 109 | + |
| 110 | +4. Set the object storage endpoint value in the following format: `https://{tenancy}.compat.objectstorage.{region}.oraclecloud.com` |
| 111 | + |
| 112 | +Following is a full example of an Object Storage backend configuration: |
| 113 | + |
| 114 | +```hcl-terraform |
| 115 | +terraform { |
| 116 | + backend "s3" { |
| 117 | + bucket = "terraform-state" |
| 118 | + key = "terraform.tfstate" |
| 119 | + region = "us-phoenix-1" |
| 120 | + endpoint = "https://acme.compat.objectstorage.us-phoenix-1.oraclecloud.com" |
| 121 | +
|
| 122 | + skip_region_validation = true |
| 123 | + skip_credentials_validation = true |
| 124 | + skip_requesting_account_id = true |
| 125 | + skip_get_ec2_platforms = true |
| 126 | + skip_metadata_api_check = true |
| 127 | + force_path_style = true |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +The S3 backend configuration can also be used for the terraform_remote_state data source to enable sharing state across Terraform projects. |
| 133 | + |
| 134 | +Once you have configured the backend, you must run `terraform init` to finish the setup. |
| 135 | +If you already have an existing `terraform.tfstate` file, then Terraform prompts you to confirm that the current state file is the one to upload to the remote state. |
| 136 | + |
| 137 | + |
| 138 | +### For More Information |
| 139 | + |
| 140 | +- [Using Pre-Authenticated Requests](https://docs.cloud.oracle.com/iaas/Content/Object/Tasks/usingpreauthenticatedrequests.htm) |
| 141 | +- [State Files](https://www.terraform.io/docs/state/index.html) |
| 142 | +- [Terraform Backend Types](https://www.terraform.io/docs/backends/types/index.html) |
0 commit comments