Image for Terraform CI/CD tasks, based on Alpine Linux for efficient cloud management.
1.2K
This Docker image is tailor-made for CI/CD pipelines, specifically designed to automate Terraform tasks such as validation, planning, application, and destruction of infrastructure. Based on Alpine Linux, it's a lightweight yet robust solution for managing your cloud infrastructure efficiently.
This guide outlines the steps for setting up a Terraform project within GitLab, configuring the necessary backend, and managing CI/CD variables. It also details how to write the .gitlab-ci.yml to automate Terraform operations such as initialization, validation, application, and destruction of infrastructure.
To integrate Terraform with GitLab's managed state services, follow these steps:
Navigate to your GitLab project settings:
You will receive a command similar to the following:
export GITLAB_ACCESS_TOKEN=<YOUR-ACCESS-TOKEN>
export TF_STATE_NAME=$TF_STATE_NAME
terraform init \
-backend-config="address=https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/terraform/state/$TF_STATE_NAME" \
-backend-config="lock_address=https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/terraform/state/$TF_STATE_NAME/lock" \
-backend-config="unlock_address=https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/terraform/state/$TF_STATE_NAME/lock" \
-backend-config="username=YOUR_USER_NAME" \
-backend-config="password=$GITLAB_ACCESS_TOKEN" \
-backend-config="lock_method=POST" \
-backend-config="unlock_method=DELETE" \
-backend-config="retry_wait_min=5"
Explanation:
address: URL to the Terraform state file stored on GitLab.lock_address: URL to acquire a state lock, preventing others from writing to the state.unlock_address: URL to release a state lock.retry_wait_min: Minimum time to wait before retrying after a lock error.Add these configurations to your main.tf file:
terraform {
backend "http" {
address = "https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/terraform/state/$TF_STATE_NAME"
lock_address = "https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/terraform/state/$TF_STATE_NAME/lock"
unlock_address = "https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/terraform/state/$TF_STATE_NAME/lock"
}
}
To configure your project's CI/CD pipeline with the necessary Terraform variables, follow these steps:
Go to your project settings in GitLab:
Add the following variables:
default).Ensure each variable is set as:
.gitlab-ci.yml Configuration for TerraformThis YAML file defines the stages and jobs for the Terraform operations in your GitLab CI/CD pipeline.
stages:
- init
- fmt
- validate
- plan
- apply
- destroy
image: xraydev/terraform-ci:latest
cache:
paths:
- .terraform/
terraform_init:
stage: init
script:
- echo "Initializing Terraform"
- >
terraform init
-backend-config="username=$GITLAB_USER_LOGIN"
-backend-config="password=$GITLAB_ACCESS_TOKEN"
-backend-config="lock_method=POST"
-backend-config="unlock_method=DELETE"
-backend-config="retry_wait_min=5"
terraform_fmt:
stage: fmt
script:
- echo "Checking Terraform file formatting"
- terraform fmt -check
terraform_validate:
stage: validate
script:
- echo "Validating Terraform configuration"
- terraform validate
terraform_plan:
stage: plan
script:
- echo "Planning Terraform configuration"
- gitlab-terraform plan
- gitlab-terraform plan-json
artifacts:
paths:
- .terraform/plan.cache
reports:
terraform: .terraform/plan.json
terraform_apply:
stage: apply
script:
- echo "Applying Terraform configuration"
- terraform apply -auto-approve
only:
- main
when: manual
terraform_destroy:
stage: destroy
script:
- echo "Destroying Terraform infrastructure"
- terraform destroy -auto-approve
when: manual
Note: The terraform_apply and terraform_destroy stages require manual activation for safety reasons. You can automate it by commenting out when: manual if necessary.
Content type
Image
Digest
sha256:55b1dd7b5…
Size
31.3 MB
Last updated
over 2 years ago
docker pull xraydev/terraform-ci