Terraform Helm Provider

Abhay Pore
4 min readApr 11, 2023

--

In this blog post, we are going to learn how to use Terraforms Helm Plugin to install any helm chart.

Terraform

Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.

Helm Chart

Helm uses a packaging format called charts. A chart is a collection of files that describe a related set of Kubernetes resources.

Integrating Helm Chart with Terraform

With the help of Terraform Helm provider we can manage our Helm chart Deployments into our Kubernetes cluster.

Defining Provider into provider.tf file:

provider "helm" {
kubernetes {
config_path = "~/.kube/config"
}
}

Here we have to define helm as a provider and we need to provide our kubeconfig file path so that our terraform script can have access to the kubernetes cluster.

Using nginx helm chart to deploy on our kubernetes cluster

resource "helm_release" "nginx" {
name = "my-test"
repository = "https://charts.bitnami.com/bitnami"
chart = "nginx"
namespace = "default"
}

Here we need to use helm_release resource (only one resource is available as part of this provider), and we need to provide nginx repository url & chart name in order to install it. If no namespace is provided, it will be installed on default namespace & if no version is provided the latest available version will be picked.

Lets run terraform init in order to intialiaze:

terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/helm...
- Installing hashicorp/helm v2.9.0...
- Installed hashicorp/helm v2.9.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Now lets try to run terraform plan command:

terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# helm_release.nginx will be created
+ resource "helm_release" "nginx" {
+ atomic = false
+ chart = "nginx"
+ cleanup_on_fail = false
+ create_namespace = false
+ dependency_update = false
+ disable_crd_hooks = false
+ disable_openapi_validation = false
+ disable_webhooks = false
+ force_update = false
+ id = (known after apply)
+ lint = false
+ manifest = (known after apply)
+ max_history = 0
+ metadata = (known after apply)
+ name = "my-test"
+ namespace = "default"
+ pass_credentials = false
+ recreate_pods = false
+ render_subchart_notes = true
+ replace = false
+ repository = "https://charts.bitnami.com/bitnami"
+ reset_values = false
+ reuse_values = false
+ skip_crds = false
+ status = "deployed"
+ timeout = 300
+ verify = false
+ version = "13.2.33"
+ wait = true
+ wait_for_jobs = false
}

Plan: 1 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

We can see one resource is getting added in the plan, now lets try to install it using terraform apply command.

terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# helm_release.nginx will be created
+ resource "helm_release" "nginx" {
+ atomic = false
+ chart = "nginx"
+ cleanup_on_fail = false
+ create_namespace = false
+ dependency_update = false
+ disable_crd_hooks = false
+ disable_openapi_validation = false
+ disable_webhooks = false
+ force_update = false
+ id = (known after apply)
+ lint = false
+ manifest = (known after apply)
+ max_history = 0
+ metadata = (known after apply)
+ name = "my-test"
+ namespace = "default"
+ recreate_pods = false
+ render_subchart_notes = true
+ replace = false
+ repository = "https://charts.bitnami.com/bitnami"
+ reset_values = false
+ reuse_values = false
+ skip_crds = false
+ status = "deployed"
+ timeout = 300
+ verify = false
+ version = "13.2.33"
+ wait = true
+ wait_for_jobs = false
}

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

helm_release.nginx: Creating...
helm_release.nginx: Still creating... [10s elapsed]
helm_release.nginx: Creation complete after 16s [id=my-test]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Now, lets check if pod is running on kubernetes cluster or not?

kubectl get po 
NAME READY STATUS RESTARTS AGE
my-test-nginx-9d4d896fc-jfj4g 1/1 Running 0 44s

Yes the pod is running as expected.

Conclusion

Terraform can be used as a infrastructure as a code and also we can provision any helm chart application using helm provider.

Ref:

https://registry.terraform.io/providers/hashicorp/helm/latest/docs

https://artifacthub.io/packages/helm/bitnami/nginx

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response