# OS_USERNAME & OS_PASSWORD are overridden as secret GitLab CI variables
```
### Multiple environments support
The terraform template allows deploying multiple environments in parallel.
Use cases of this include:
- monorepo, where a single Git repository might host several separate deployable components or apps,
- multi-instances deployment of the same application.
This feature can be enabled using the [parallel matrix jobs](https://docs.gitlab.com/ee/ci/yaml/#parallelmatrix) pattern
at the `.tf-base` or `.tf-workspace` job level.
The job on which put the matrix depends on your needs (whether to use multiple terraform modules in the same repository or only one).
Environments namespacing is ensured by the `TF_ENVIRONMENT_NAMESPACE` variable (must start with a `/`).
Here is an example of the `.gitlab-ci.yml` file for a project deploying both a frontend and a backend application:
```yaml
variables:
# Terraform deployment scripts are located in the ./scripts/ directory
TF_SCRIPTS_DIR:$CI_PROJECT_DIR/scripts
# Overriding .tf-base to get all security and deployments jobs for every terraform module
.tf-base:
parallel:
matrix:
-TF_ENVIRONMENT_NAMESPACE:/front
TF_PROJECT_DIR:front
-TF_ENVIRONMENT_NAMESPACE:/back
TF_PROJECT_DIR:back
```
The above configuration will deploy 2 environments on each pipeline:
- on feature branches: `review/front/$CI_COMMIT_REF_NAME` and `review/back/$CI_COMMIT_REF_NAME`
- on the integration branch: `integration/front` and `integration/back`
- on the production branch: `staging/front` and `staging/back` (and finally `production/front` and `production/back`)
Here's another example of the `.gitlab-ci.yml` file for a project deploying the same application on multiple environments:
```yaml
variables:
# Terraform deployment scripts are located in the ./scripts/ directory
TF_SCRIPTS_DIR:$CI_PROJECT_DIR/scripts
# Overriding .tf-workspace to get only deployments jobs for each terraform module
.tf-workspace:
parallel:
matrix:
-TF_ENVIRONMENT_NAMESPACE:/some-project-id
GCP_PROJECT_ID:some-project-id
-TF_ENVIRONMENT_NAMESPACE:/some-other-project-id
GCP_PROJECT_ID:some-other-project-id
```
The above configuration will deploy 2 enviroments on deployments jobs (`tf-plan-*`, `tf-review`, `tf-destroy-*`, etc.).
:info: When using both this feature and [GitLab backend](#gitlab-managed-terraform-state-default), the state name will be `<namespace>_<environment_slug>`
where `namespace` is built from `TF_ENVIRONMENT_NAMESPACE` (stripped of punctuation characters and converted to lowercase).
### Supported output artifacts
The Terraform template supports [job artifacts](https://docs.gitlab.com/ci/jobs/job_artifacts/) that your Terraform
@@ -231,6 +290,16 @@ Examples:
* When used in conjuction with Ansible template, your Terraform script may [generate the Ansible inventory file](https://www.percona.com/blog/how-to-generate-an-ansible-inventory-with-terraform/) into the `$TF_OUTPUT_DIR` directory.
* When dynamically obtaining a floating IP address, your Terraform script may generate the `terraform.env` file to propated it as an environment variables.
> [!important]
> If [multiple environments](#multiple-environments-support) are configured, the output variables are prefixed with a
> sluggified value of the `TF_ENVIRONMENT_NAMESPACE` variable (stripped of punctuation characters and converted to lowercase):
>
> * `<namespace_slug>_environment_type`: set to the type of environment (`review`, `integration`, `staging` or `production`),
> * `<namespace_slug>_environment_name`: the application name (see below),
> * `<namespace_slug>_environment_url`: set to the environment URL (whether determined statically or dynamically).
>
> The output dotenv file will be `terraform.env.<namespace_slug>` instead, and the dynamic variable `${environment_namespace}` can be used in your scripts and manifests to access the contextual value of `<namespace_slug>`.
### Terraform integration in Merge Requests
This template enables [Terraform integration in Merge Requests](https://docs.gitlab.com/user/infrastructure/iac/mr_integration/).
@@ -376,7 +445,7 @@ Here are some advices about your **secrets** (variables marked with a :lock:):
The Terraform template uses some global configuration used throughout all jobs.
| Input / Variable | Description | Default value |
| `image` / `TF_IMAGE` | the Docker image used to run Terraform CLI commands <br/>:warning: **set the version required by your project** | `docker.io/hashicorp/terraform:latest`<br/>[](https://to-be-continuous.gitlab.io/doc/secu/trivy-TF_IMAGE) |
| `gitlab-backend-disabled` / `TF_GITLAB_BACKEND_DISABLED` | Set to `true` to disable [GitLab managed Terraform State](https://docs.gitlab.com/user/infrastructure/iac/terraform_state/) | _none_ (enabled) |
| `apk-extra-opts` / `TF_APK_EXTRA_OPTS` | Extra [`apk add` options](https://www.mankier.com/8/apk)(`apk` is used to install `jq` and/or `curl` if necessary) | _none_ |
| `environment-namespace` / `TF_ENVIRONMENT_NAMESPACE` | Extra [GitLab environments](https://docs.gitlab.com/ci/environments/) namespace _(only required when deploying [multiple environments](#multiple-environments-support))_<br/>:warning: must start with a slash `/` | _none_ |
"description":"Extra [`apk add` options](https://www.mankier.com/8/apk) (`apk` is used to install `jq` and/or `curl` if necessary)",
"advanced":true
},
{
"name":"TF_ENVIRONMENT_NAMESPACE",
"description":"Extra [GitLab environments](https://docs.gitlab.com/ci/environments/) namespace _(only required when deploying multiple environments)_\n\n:warning: must start with a slash `/`",
# define environment_type, environment_name and environment_slug as TF variables through env rather than on CLI (fails if not declared)
# define environment_type, environment_name, environment_slug and environment_namespace as TF variables through env rather than on CLI (fails if not declared)