# Terraform CLI Usage

This page describes common CLI commands to consume and publish Terraform assets through Sonatype Nexus Repository.

## Retrieve Terraform Content

After configuring Terraform to use the Nexus proxy repository, run Terraform commands as usual from your workspace. The following commands use Nexus repository to retrieve Terraform content:

- `terraform init` \- Downloads required modules and providers through the Nexus Terraform proxy repository.
- `terraform get` \- Fetches modules referenced in the Terraform configuration.
- `terraform init -upgrade` \- Refreshes module and provider versions when updates are available.

The following OpenTofu commands use Nexus Repository to retrieve content:

- `tofu init` \- Downloads required modules and providers through the Nexus Terraform repository.
- `tofu get` \- Fetches modules referenced in the OpenTofu configuration.
- `tofu init -upgrade` \- Refreshes module and provider versions when updates are available.

## Upload Terraform Modules

Terraform modules are uploaded using the following path structure:

```
/v1/modules/{namespace}/{name}/{provider}/{version}/{filename}
```

CURL command:

```
curl
    -X PUT \
    'https://[NEXUS URL]/repository/<REPO_NAME>/v1/modules/{namespace}/{name}/{provider}/{version}/{filename}' \
    -u 'USERNAME:PASSWORD' \
    -H 'Content-Type: application/zip' \
    --data-binary '@{filename}.zip'
```

Parameters:

- `namespace` \- Organization or team name  
- `name` \- Module name  
- `provider` \- Provider name  
- `version` \- Semantic version such as 1.0.0.  
- `filename` \- Archive file name following the naming convention  
- `[NEXUS URL]` \- Your Nexus URL  
- `<REPO_NAME>` \- Your Nexus Repository Name, for example _terraform-hosted_

**Note**

As Terraform does not support a publish command, we use CURL to perform the publish operation.

Terraform module archives must include `.tf` files to be considered valid. Each module can be up to 100 MB in size and must be packaged as` .tar.gz` , `.tgz,` or `.zip` (recommended).

## Upload Terraform Providers

Provider uploads use the following path structure:

```
/v1/providers/{namespace}/{type}/{version}/download/{os}/{arch}
```

CURL Command:

```
curl
    -X PUT \
    'https://[NEXUS URL]/repository/<REPO_NAME>/v1/providers/{namespace}/{type}/{version}/download/{os}/{arch}' \
    -u 'USERNAME:PASSWORD' \
    -H 'Content-Type: application/zip' \
    --data-binary '@{filename}.zip'
```

Parameters:

- `namespace` \- Provider namespace  
- `type` \- Provider type, for example `aws`, `azure`, `random`  
- `version` \- Provider version, for example `5.100.0`  
- `os` \- Operating System  
- `arch` \- Architecture  
- `[NEXUS URL]` \- Your Nexus URL  
- `<REPO_NAME>` \- Your Nexus Repository Name, for example _terraform-hosted_

**Note**

The filename is not included in the URL path. Instead, it must be provided in the `Content-Disposition` header.

When a provider binary is uploaded, Nexus automatically generates provider metadata. A provider package must be a platform-specific binary archive in `.zip` format. Each provider version can include multiple archives for different operating systems and architectures.
