================ CODE SNIPPETS ================ TITLE: Sample Request: List Terraform Provider Installation Packages DESCRIPTION: Example `curl` command to query download packages for version `2.0.0` of the `hashicorp/random` provider. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/provider-network-mirror-protocol.mdx#_snippet_6 LANGUAGE: shell CODE: ``` curl 'https://terraform.example.com/providers/registry.terraform.io/hashicorp/random/2.0.0.json' ``` -------------------------------- TITLE: Example JSON for Provision Start Message DESCRIPTION: This JSON snippet demonstrates the structure of a `provision_start` message from Terraform UI. It signifies the beginning of a resource provisioning operation, including the resource details and the type of provisioner being used. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/machine-readable-ui.mdx#_snippet_18 LANGUAGE: json CODE: ``` { "@level": "info", "@message": "null_resource.none[0]: Provisioning with 'local-exec'...", "@module": "terraform.ui", "@timestamp": "2021-03-26T16:38:43.997431-04:00", "hook": { "resource": { "addr": "null_resource.none[0]", "module": "", "resource": "null_resource.none[0]", "implied_provider": "null", "resource_type": "null_resource", "resource_name": "none", "resource_key": 0 }, "provisioner": "local-exec" }, "type": "provision_start" } ``` -------------------------------- TITLE: Terraform UI Message: Ephemeral Operation Start Example DESCRIPTION: This JSON example shows the `ephemeral_op_start` message, indicating the beginning of an ephemeral resource operation. It includes the resource details and the specific action being performed (e.g., "open"). SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/machine-readable-ui.mdx#_snippet_26 LANGUAGE: json CODE: ``` { "@level": "info", "@message": "ephemeral.random_password.example: Opening...", "@module": "terraform.ui", "@timestamp": "2024-10-30T10:34:26.222465-00:00", "hook": { "resource": { "addr": "ephemeral.random_password.example", "module": "", "resource": "ephemeral.random_password.example", "implied_provider": "random", "resource_type": "random_password", "resource_name": "example", "resource_key": null }, "action": "open" }, "type": "ephemeral_op_start" } ``` -------------------------------- TITLE: Execute Credentials Helper for 'get' Operation DESCRIPTION: Command-line execution example for the 'credstore' helper to retrieve credentials for a specific host using the 'get' verb. The helper prints a JSON object to stdout on success or an empty JSON object if no credentials are found. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/credentials-helpers.mdx#_snippet_2 LANGUAGE: Shell CODE: ``` terraform-credentials-credstore --host=credstore.example.com get app.terraform.io ``` -------------------------------- TITLE: Example input for network and subnet variables DESCRIPTION: Provides example HCL input values for the `networks` and `subnets` variables, demonstrating the expected structure and data for the `setproduct` example. This input will generate specific combinations for resource creation. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/functions/setproduct.mdx#_snippet_8 LANGUAGE: HCL CODE: ``` networks = { a = { base_cidr_block = "10.1.0.0/16" } b = { base_cidr_block = "10.2.0.0/16" } } subnets = { a = { number = 1 } b = { number = 2 } c = { number = 3 } } ``` -------------------------------- TITLE: Terraform Init: Provider Plugin Installation Options DESCRIPTION: Describes how `terraform init` automatically finds, downloads, and installs necessary provider plugins from registries. It also explains how the dependency lock file is used to ensure consistent provider versions and options to override this. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/commands/init.mdx#_snippet_4 LANGUAGE: APIDOC CODE: ``` -upgrade: Upgrades all previously-selected plugins to the newest version that complies with the configuration's version constraints, ignoring the dependency lock file. -get-plugins=false: Skips plugin installation. (Superseded by `provider_installation` and `plugin_cache_dir` settings in Terraform 0.13+, removed in 0.15). -plugin-dir=PATH: Forces plugin installation to read plugins only from the specified directory, acting as a one-time override for testing local builds. -lockfile=MODE: Sets a dependency lockfile mode. Valid modes include: readonly: Suppresses lockfile changes but verifies checksums against recorded information. Conflicts with -upgrade. ``` -------------------------------- TITLE: HCL Deployment Block Example with Inputs DESCRIPTION: An example of a deployment block named "production" that accepts 'aws_region' and 'instance_count' as input variables, demonstrating practical usage. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/stacks/reference/tfdeploy.mdx#_snippet_2 LANGUAGE: HCL CODE: ``` deployment "production" { inputs = { aws_region = "us-west-1" instance_count = 2 } } ``` -------------------------------- TITLE: Example of Generated Terraform HCL Configuration File DESCRIPTION: A simplified example of the HCL configuration content that Terraform generates for an imported resource. This output serves as a starting template for further refinement and integration into the main configuration. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/import/generating-configuration.mdx#_snippet_3 LANGUAGE: hcl CODE: ``` resource "aws_iot_thing" "bar" { name = "foo" } ``` -------------------------------- TITLE: Terraform Provider Installation Method: direct DESCRIPTION: Documentation for the `direct` provider installation method. This method instructs Terraform to request provider information directly from its origin registry and download it over the network. It does not require any additional arguments. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/config/config-file.mdx#_snippet_4 LANGUAGE: APIDOC CODE: ``` direct: description: Requests information about the provider directly from its origin registry and downloads over the network from the location that registry indicates. arguments: None ``` -------------------------------- TITLE: HCL `range` Function Output Examples DESCRIPTION: Provides several examples demonstrating the output of the `range` function with different combinations of `max`, `start`, `limit`, and `step` values, including positive, negative, and fractional steps. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/functions/range.mdx#_snippet_2 LANGUAGE: HCL CODE: ``` > range(3) [ 0, 1, 2, ] > range(1, 4) [ 1, 2, 3, ] > range(1, 8, 2) [ 1, 3, 5, 7, ] > range(1, 4, 0.5) [ 1, 1.5, 2, 2.5, 3, 3.5, ] > range(4, 1) [ 4, 3, 2, ] > range(10, 5, -2) [ 10, 8, 6, ] ``` -------------------------------- TITLE: Initialize Terraform Working Directory DESCRIPTION: Explains how `terraform init` installs required providers, noting default network access and options to limit it for air-gapped environments. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/plugins/index.mdx#_snippet_0 LANGUAGE: APIDOC CODE: ``` terraform init Purpose: Initializes a working directory by installing any required providers. Details: - Requires network access by default to download providers. - Can be configured to limit or skip network access, or use local/HTTPS mirrors. ``` -------------------------------- TITLE: Install Terraform Stacks CLI on Debian or Ubuntu DESCRIPTION: Commands to add the HashiCorp GPG key and repository, then install the terraform-stacks-cli package using apt on Debian or Ubuntu systems. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/stacks/reference/tfstacks-cli.mdx#_snippet_1 LANGUAGE: shell-session CODE: ``` $ wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg $ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list $ sudo apt update && sudo apt install terraform-stacks-cli ``` -------------------------------- TITLE: Install Terraform Stacks CLI with Homebrew DESCRIPTION: Commands to add the HashiCorp tap and install the tfstacks CLI using Homebrew on macOS or Linux. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/stacks/reference/tfstacks-cli.mdx#_snippet_0 LANGUAGE: shell-session CODE: ``` $ brew tap hashicorp/tap $ brew install hashicorp/tap/tfstacks ``` -------------------------------- TITLE: Example Terraform Configuration for Console Testing DESCRIPTION: A sample `main.tf` configuration defining a map variable `apps` and a `random_pet` resource using `for_each`. This configuration serves as the context for subsequent `terraform console` examples, allowing users to test interpolations against defined variables and resources. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/commands/console.mdx#_snippet_1 LANGUAGE: hcl CODE: ``` variable "apps" { type = map(any) default = { "foo" = { "region" = "us-east-1" }, "bar" = { "region" = "eu-west-1" }, "baz" = { "region" = "ap-south-1" } } } resource "random_pet" "example" { for_each = var.apps } ``` -------------------------------- TITLE: Terraform Test File Progress and Status Example DESCRIPTION: This snippet shows a `test_file` message, providing progress updates for each test file during `terraform test` execution. It includes `path`, `progress` (starting, teardown, complete), and `status` (pass, error, fail, skip) upon completion. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/machine-readable-ui.mdx#_snippet_35 LANGUAGE: json CODE: ``` { "@level": "info", "@message": "main.tftest.hcl... pass", "@module": "terraform.ui", "@testfile": "validation.tftest.hcl", "@timestamp": "2023-08-09T16:12:30.724368+02:00", "test_file": { "path": "validation.tftest.hcl", "progress": "complete", "status": "pass" }, "type": "test_file" } ``` -------------------------------- TITLE: Example JSON for Refresh Start Message DESCRIPTION: This JSON snippet shows the structure of a `refresh_start` message from Terraform UI. It indicates the initiation of a state refresh operation for a resource, providing the resource details and its identifier. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/machine-readable-ui.mdx#_snippet_22 LANGUAGE: json CODE: ``` { "@level": "info", "@message": "null_resource.none[0]: Refreshing state... [id=1971614370559474622]", "@module": "terraform.ui", "@timestamp": "2021-03-26T14:18:06.508915-04:00", "hook": { "resource": { "addr": "null_resource.none[0]", "module": "", "resource": "null_resource.none[0]", "implied_provider": "null", "resource_type": "null_resource", "resource_name": "none", "resource_key": 0 }, "id_key": "id", "id_value": "1971614370559474622" }, "type": "refresh_start" } ``` -------------------------------- TITLE: Install Terraform Stacks CLI on Fedora DESCRIPTION: Commands to enable dnf-plugins-core, add the HashiCorp repository, and install the terraform-stacks-cli package using dnf on Fedora systems. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/stacks/reference/tfstacks-cli.mdx#_snippet_3 LANGUAGE: shell-session CODE: ``` $ sudo dnf install -y dnf-plugins-core $ sudo dnf config-manager addrepo --from-repofile=https://rpm.releases.hashicorp.com/fedora/hashicorp.repo $ sudo dnf -y install terraform-stacks-cli ``` -------------------------------- TITLE: Initialize a new Terraform project and Git repository DESCRIPTION: This snippet demonstrates the initial setup for a Terraform project, including creating a Git repository, an empty main.tf configuration file, and running 'terraform init' to download necessary provider plugins. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/intro/core-workflow.mdx#_snippet_0 LANGUAGE: sh CODE: ``` # Create repository $ git init my-infra && cd my-infra Initialized empty Git repository in /.../my-infra/.git/ # Write initial config $ vim main.tf # Initialize Terraform $ terraform init Initializing provider plugins... # ... Terraform has been successfully initialized! ``` -------------------------------- TITLE: Terraform Provider Installation Method: filesystem_mirror DESCRIPTION: Documentation for the `filesystem_mirror` provider installation method. This method allows Terraform to consult a local directory for provider copies. It requires the `path` argument and supports both packed and unpacked directory layouts for provider distribution files. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/config/config-file.mdx#_snippet_5 LANGUAGE: APIDOC CODE: ``` filesystem_mirror: description: Consults a directory on the local disk for copies of providers. arguments: path: string (required) description: The directory to look in for provider copies. supported_layouts: Packed layout: HOSTNAME/NAMESPACE/TYPE/terraform-provider-TYPE_VERSION_TARGET.zip Unpacked layout: HOSTNAME/NAMESPACE/TYPE/VERSION/TARGET Note: Unpacked layout attempts symbolic linking. ``` -------------------------------- TITLE: Example Usage of base64sha256 Function DESCRIPTION: This snippet demonstrates the direct application of the `base64sha256` function within a console-like environment, showing the input string 'hello world' and its corresponding Base64-encoded SHA256 hash output. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/functions/base64sha256.mdx#_snippet_0 LANGUAGE: Console CODE: ``` > base64sha256("hello world") uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek= ``` -------------------------------- TITLE: Sample Request: List Terraform Provider Versions DESCRIPTION: Example `curl` command to query available versions for the `hashicorp/random` provider. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/provider-network-mirror-protocol.mdx#_snippet_3 LANGUAGE: shell CODE: ``` curl 'https://terraform.example.com/providers/registry.terraform.io/hashicorp/random/index.json' ``` -------------------------------- TITLE: Terraform Init with Source Module Copy DESCRIPTION: Demonstrates how to use `terraform init` to copy a module from a source into the current working directory before any other initialization steps are run. This is useful for checking out configurations or using example configurations. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/commands/init.mdx#_snippet_1 LANGUAGE: Shell CODE: ``` terraform init -from-module=MODULE-SOURCE ``` -------------------------------- TITLE: Configure Terraform Provider Installation Block DESCRIPTION: This HCL snippet demonstrates the basic structure of a `provider_installation` block in Terraform's CLI configuration. It shows how to define a `filesystem_mirror` for specific providers (e.g., `example.com/*/*`) and a `direct` method for all others, using `include` and `exclude` patterns to control which installation method applies to which providers. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/cli/config/config-file.mdx#_snippet_3 LANGUAGE: HCL CODE: ``` provider_installation { filesystem_mirror { path = "/usr/share/terraform/providers" include = ["example.com/*/*"] } direct { exclude = ["example.com/*/*"] } } ``` -------------------------------- TITLE: Illustrate chunklist function usage with HCL examples DESCRIPTION: These examples demonstrate the practical application of the `chunklist` function in HCL. They show how a single list is divided into fixed-size sub-lists based on the `chunk_size` parameter, and how the function handles remaining elements. The output clearly displays the resulting list of lists. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/functions/chunklist.mdx#_snippet_1 LANGUAGE: HCL CODE: ``` > chunklist(["a", "b", "c", "d", "e"], 2) [ [ "a", "b", ], [ "c", "d", ], [ "e", ], ] > chunklist(["a", "b", "c", "d", "e"], 1) [ [ "a", ], [ "b", ], [ "c", ], [ "d", ], [ "e", ], ] ``` -------------------------------- TITLE: Terraform Provider Service Discovery Document Example DESCRIPTION: An example of a service discovery document for a host that only implements the provider registry protocol, showing the base URL for the `providers.v1` service. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/internals/provider-registry-protocol.mdx#_snippet_0 LANGUAGE: json CODE: ``` { "providers.v1": "/terraform/providers/v1/" } ``` -------------------------------- TITLE: HCL Example: Configuring Provisioner Connection Blocks DESCRIPTION: This HCL code demonstrates how to define `connection` blocks within Terraform provisioners. It includes two examples: one for an SSH connection to copy a file as the root user, and another for a WinRM connection to copy a file as the Administrator user. Both examples specify connection type, user, password, and host. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/resources/provisioners/connection.mdx#_snippet_0 LANGUAGE: hcl CODE: ``` # Copies the file as the root user using SSH provisioner "file" { source = "conf/myapp.conf" destination = "/etc/myapp.conf" connection { type = "ssh" user = "root" password = var.root_password host = var.host } } # Copies the file as the Administrator user using WinRM provisioner "file" { source = "conf/myapp.conf" destination = "C:/App/myapp.conf" connection { type = "winrm" user = "Administrator" password = var.admin_password host = var.host } } ``` -------------------------------- TITLE: Install Terraform Stacks CLI on CentOS or RHEL DESCRIPTION: Commands to enable yum-utils, add the HashiCorp repository, and install the terraform-stacks-cli package using yum on CentOS or RHEL systems. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/stacks/reference/tfstacks-cli.mdx#_snippet_2 LANGUAGE: shell-session CODE: ``` $ sudo yum install -y yum-utils $ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo $ sudo yum -y install terraform-stacks-cli ``` -------------------------------- TITLE: Terraform `terraform` block complete configuration example DESCRIPTION: Illustrates all supported built-in arguments for the `terraform` block, including `required_version`, `required_providers`, `provider_meta`, `backend`, `cloud`, and `experiments`. SOURCE: https://github.com/hashicorp/terraform/blob/v1.12.2/website/docs/language/terraform.mdx#_snippet_1 LANGUAGE: hcl CODE: ``` terraform { required_version = "" required_providers { { version = "" source = "" } } provider_meta "