|
| 1 | +--- |
| 2 | +layout: "docs" |
| 3 | +page_title: "Terraform Cloud Settings - Terraform CLI" |
| 4 | +--- |
| 5 | + |
| 6 | +# Terraform Cloud Settings |
| 7 | + |
| 8 | +Terraform CLI can integrate with Terraform Cloud, acting as a client for Terraform Cloud's |
| 9 | +[CLI-driven run workflow](https://www.terraform.io/docs/cloud/run/cli.html). |
| 10 | + |
| 11 | +> **Hands On:** Try the [Migrate State to Terraform Cloud](https://learn.hashicorp.com/tutorials/terraform/cloud-migrate) tutorial on HashiCorp Learn. |
| 12 | +
|
| 13 | +You must configure the following settings to use Terraform Cloud for a particular working directory: |
| 14 | + |
| 15 | +- Provide credentials to access Terraform Cloud, preferably by using the |
| 16 | + [`terraform login`](/docs/cli/commands/login.html) command. |
| 17 | +- Add a `cloud` block to the directory's Terraform configuration, to specify |
| 18 | + which organization and workspace(s) to use. |
| 19 | +- Optionally, use a `.terraformignore` file to specify files that shouldn't be |
| 20 | + uploaded with the Terraform configuration when running plans and applies. |
| 21 | + |
| 22 | +After adding or changing a `cloud` block, you must run `terraform init`. |
| 23 | + |
| 24 | +## The `cloud` Block |
| 25 | + |
| 26 | +The `cloud` block is a nested block within the top-level `terraform` settings |
| 27 | +block. It specifies which Terraform Cloud workspaces to use for the current |
| 28 | +working directory. |
| 29 | + |
| 30 | +```hcl |
| 31 | +terraform { |
| 32 | + cloud { |
| 33 | + organization = "my-org" |
| 34 | + hostname = "app.terraform.io" # Optional; defaults to app.terraform.io |
| 35 | +
|
| 36 | + workspaces { |
| 37 | + tags = ["networking", "source:cli"] |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +The `cloud` block also has some special restrictions: |
| 44 | + |
| 45 | +- A configuration can only provide one `cloud` block. |
| 46 | +- A `cloud` block cannot be used with [state backends](/docs/language/settings/backends/index.html). |
| 47 | + A configuration can use one or the other, but not both. |
| 48 | +- A `cloud` block cannot refer to named values (like input variables, locals, or |
| 49 | + data source attributes). |
| 50 | + |
| 51 | +The `cloud` block only affects Terraform CLI's behavior. When Terraform Cloud uses a configuration |
| 52 | +that contains a cloud block - for example, when a workspace is configured to use a VCS provider |
| 53 | +directly - it ignores the block and behaves according to its own workspace settings. |
| 54 | + |
| 55 | +### Arguments |
| 56 | + |
| 57 | +The `cloud` block supports the following configuration arguments: |
| 58 | + |
| 59 | +- `organization` - (Required) The name of the organization containing the |
| 60 | + workspace(s) the current configuration should use. |
| 61 | + |
| 62 | +- `workspaces` - (Required) A nested block that specifies which remote Terraform Cloud workspaces to |
| 63 | +use for the current configuration. The `workspaces` block must contain **exactly one** of the |
| 64 | +following arguments, each denoting a strategy for how workspaces should be mapped: |
| 65 | + |
| 66 | + - `tags` - (Optional) A set of Terraform Cloud workspace tags. You will be able to use |
| 67 | + this working directory with any workspaces that have all of the specified tags, |
| 68 | + and can use [the `terraform workspace` commands](/docs/cli/workspaces/index.html) |
| 69 | + to switch between them or create new workspaces. New workspaces will automatically have |
| 70 | + the specified tags. This option conflicts with `name`. |
| 71 | + |
| 72 | + - `name` - (Optional) The name of a single Terraform Cloud workspace. You will |
| 73 | + only be able to use the workspace specified in the configuration with this working |
| 74 | + directory, and cannot manage workspaces from the CLI (e.g. `terraform workspace select` or |
| 75 | + `terraform workspace new`). This option conflicts with `tags`. |
| 76 | + |
| 77 | +- `hostname` - (Optional) The hostname of a Terraform Enterprise installation, if using Terraform |
| 78 | + Enterprise. Defaults to Terraform Cloud (app.terraform.io). |
| 79 | + |
| 80 | +- `token` - (Optional) The token used to authenticate with Terraform Cloud. |
| 81 | + We recommend omitting the token from the configuration, and instead using |
| 82 | + [`terraform login`](/docs/cli/commands/login.html) or manually configuring |
| 83 | + `credentials` in the |
| 84 | + [CLI config file](/docs/cli/config/config-file.html#credentials). |
| 85 | + |
| 86 | +## Excluding Files from Upload with .terraformignore |
| 87 | + |
| 88 | +When executing a remote `plan` or `apply` in a [CLI-driven run](/docs/cloud/run/cli.html), |
| 89 | +a copy of your configuration directory is uploaded to Terraform Cloud. You can define |
| 90 | +paths to exclude from upload by adding a `.terraformignore` file at the root of your |
| 91 | +configuration directory. If this file is not present, the upload will exclude |
| 92 | +the following by default: |
| 93 | + |
| 94 | +- `.git/` directories |
| 95 | +- `.terraform/` directories (exclusive of `.terraform/modules`) |
| 96 | + |
| 97 | +The rules in `.terraformignore` file resemble the rules allowed in a |
| 98 | +[.gitignore file](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring): |
| 99 | + |
| 100 | +- Comments (starting with `#`) or blank lines are ignored. |
| 101 | +- End a pattern with a forward slash `/` to specify a directory. |
| 102 | +- Negate a pattern by starting it with an exclamation point `!`. |
| 103 | + |
| 104 | +-> **Note:** Unlike `.gitignore`, only the `.terraformignore` at the root of the configuration directory is considered. |
0 commit comments