Terraform Provider for env0
Terraform Provider for env0
Quick Start
terraform {
required_providers {
env0 = {
source = "env0/env0"
}
}
}
provider "env0" {}
data "env0project" "defaultproject" { name = "My First Project" }
resource "env0_template" "example" { name = "example" description = "Example template" repository = "https://github.com/env0/templates" path = "aws/hello-world" }
resource "env0configurationvariable" "inatemplate" { name = "VARIABLE_NAME" value = "some value" templateid = env0template.tested1.id }
Authentication
- Generate an
apikeyandapisecretfrom the Organization Settings page.
- These can be provided by one of two methods:
ENV0APIKEY and ENV0APISECRET environment variables, and just declaring the provider with no parameters:
provider "env0" {}
2. Specify these fields as parameters to the provider:
variable "env0apikey" {}
variable "env0apisecret" {}
provider "env0" { apikey = var.env0api_key apisecret = var.env0api_secret }
How to get VCS credentials for Creating a template or a VCS environment
To create an env0template or a VCS env0environment resources a user must provision the corresponding credentials:
githubinstallationidfor Githubbitbucketclientkeyfor Bitbucketgitlabprojectid+token_idfor Gitlabtoken_idfor Azure DevOpsvcsconnectionidfor an already existing VCS connection (created in env0)
env0 app, then just fetch the corresponding env0_environment data source and use the relevant credentials:
data "env0environment" "masterenvironment" {
id = "exampleId"
}
resource "env0_template" "example" { name = "example AzureDevOps" description = "Example AzureDevOps template" repository = "https://dev.azure.com/example-org/AWS/_git/example" path = "hello-world" tokenid = data.env0environment.masterenvironment.tokenid }
Development Setup
Required tools: Go 1.26, golangci-lint v2+
Build
- Use the
./build.shscript. - The output binary is called
terraform-provider-env0
Run local version of the provider
- Build -
./build.sh - Create the plugins folder -
mkdir -p ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_arm64(for Intel processor, replacearm64withamd64) - Copy the built binary -
cp ./terraform-provider-env0 ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_arm64(Replacedarwinwithlinuxon Linux) - Require the local provider in your
main.tf-
terraform {
required_providers {
env0 = {
version = "6.6.6"
source = "terraform.env0.com/local/env0"
}
}
}
Installing pre-commit hooks
For consistent coding style, install pre-commit hooks.
pre-commit install
pre-commit install --hook-type pre-push
Testing
Integration tests
- The integration tests run against the real env0 API
- Have
ENV0APIKEYandENV0APISECRETenvironment variables defined. - Also set
ENV0APIENDPOINTif you want to run against a non-prod environment. - Run
go run tests/harness.go(from the project root folder) to run all the tests. - Use
go run tests/harness.go 003configurationvariableto run a specific test.
terraform initterraform apply -auto-approve -var second_run=0terraform apply -auto-approve -var second_run=1terraform outputs -json- and verifies expected outputs fromexpected_outputs.jsonterraform destroy
DESTROYMODE exists and it's value is NODESTROY, the harness will avoid calling terraform destroy, allowing the developer to inspect the resources created, through the dashboard, for example.
Afterwards, when cleanup is required, just set DESTROYMODE to DESTROYONLY and only terraform destroy will run.
Integration Test Prerequisites
- An env0 organization
- An API Key
- A Github.com integrated template name
Github Integrated Templatefor https://github.com/env0/templates - A Gitlab.com integrated template name
Gitlab Integrated Templatefor https://gitlab.com/env0/gitlab-vcs-integration-tests
Unit Testing
How to run tests
Run from root directory:
go test -v ./...
Running a single provider test
export TEST_PATTERN="TestUnitConfigurationVariableResource/Create" && go test -v ./env0
How to use mocks
- Make sure
GOPATHis in yourPATH
go env GOPATH
echo $PATH
export PATH=$PATH:$(go env GOPATH) # if not
- Install mockgen
go install go.uber.org/mock/mockgen@v0.5.0
- Make sure to add this line in files that include the interface you'd wish to mock:
//go:generate mockgen -destination=<file>_mock.go -package=<package> . <interface>
- Run from root directory:
go generate ./...
Documentation
- Docs are generated using github.com/hashicorp/terraform-plugin-docs
- The
DocsGitHub Action regenerates docs on every PR and commits any changes back to the PR branch, so generated docs stay in sync automatically — no manual step is needed. - You can run
./generate-docs.shlocally to preview changes if you want. - Please add an example to
examples/<resources or data-sources>/env0_<name>dir and make sure it is added to the docs.
Release
To release a version to the Terraform Public Registry:
- Validate that all status checks are ✅ on
mainbranch (generated docs are kept in sync per-PR, somainshould already be current) - Go to Actions → release → Run workflow, select the branch (
main) and the version bump (patch/minor/major), then Run workflow. It computes the next tag from the latest one, creates & pushes it, and publishes the release with binaries — no approval needed. - The Registry will automatically pick up on the new version.
v..* tag manually (git tag v0.0.9 && git push origin --tags) also triggers the same release.