docs / articles / Helm Template Range Example Displayed with Ease

Helm Template Range Example Displayed with Ease

Noah Aug 16, 2026 2026-08-16 04:58:21

In the vast landscape of infrastructure as code (IaC), Helm templates offer a powerful tool for dynamic and repeatable deployments in Kubernetes. One of Helm's standout features is its capability to handle ranges, enabling you to create highly flexible and scalable templates. Today, we explore Helm template ranges through an illustrative example.

Templates — SKS PROPS
Templates — SKS PROPS

To begin, it's crucial to understand that Helm ranges allow you to generate multiple instances of a chart template based on a given set of data. This functionality is achieved using the `.Files` function in combination with `range` and `index` functions. Let's delve into an example that illustrates this beautifully.

Helmet Template – Large
Helmet Template – Large

Defining a Range in Helm Templates

In this section, we'll see how to define a range in a Helm template. We'll create a simple example that generates multiple instances of a deployment based on a list of strings.

the heinet base build template is shown in three different sizes, including one for each
the heinet base build template is shown in three different sizes, including one for each

First, we define our values file with a list of strings we want to use as chart instances. This list could represent different environments, applications, or any other set of distinct data. Here's a simple `values.yaml` file:

```yaml envs: - dev - staging - production ```

Using `.Files` and `range` to Generate Instances

Arai CK6 all views - (VECTOR DESIGN TEMPLATE)
Arai CK6 all views - (VECTOR DESIGN TEMPLATE)

Now, we'll use the `.Files` function along with `range` to generate multiple instances of our chart for each environment. In our `templates/deployment.yaml`, we might write something like this:

```yaml {{- range $index, $env := .Values.envs }} apiVersion: apps/v1 kind: Deployment metadata: name: my-app-{{ $env }} spec: replicas: 1 selector: matchLabels: app: my-app-{{ $env }} template: metadata: labels: app: my-app-{{ $env }} spec: containers: - name: my-app image: my-image:{{ $env }} {{- end }} ```

In this code, the `range` function iterates over the `$envs` list from our values file. For each environment, it creates a new deployment with a unique name and label based on the environment name.

Indexing to Control Instance Behavior

the helmet and visor are shown in three different positions
the helmet and visor are shown in three different positions

In Helm, the `index` function provides a zero-based index for each item in the range, which you can use to control the instance's behavior based on its position in the list. For instance, you could use the index to set a replica count based on the environment:

```yaml {{- range $index, $env := .Values.envs }} apiVersion: apps/v1 kind: Deployment metadata: name: my-app-{{ $env }} spec: replicas: {{ add1 $index }} # adjust replicas based on index selector: matchLabels: app: my-app-{{ $env }} template: metadata: labels: app: my-app-{{ $env }} spec: containers: - name: my-app image: my-image:{{ $env }} {{- end }} ```

Here, the `add1` function is used to increment the replica count for each subsequent environment.

Advanced Ranges:Conditional Instance Generation

Firefighter Helmet Shield Silhouette Images – Browse 920 Stock Photos, Vectors, and Video
Firefighter Helmet Shield Silhouette Images – Browse 920 Stock Photos, Vectors, and Video

Now that we've shown how to generate instances based on a range, let's explore a more advanced use case – conditional instance generation. Perhaps you want to create deployments for some, but not all, environments.

To achieve this, you can use the `if` and `else` functions along with `range` to control instance generation conditionally. For example, you might want to skip the `dev` environment to ensure only production-like environments are deployed:

the free helmet template clip art is available for all ages to print and use on t - shirts
the free helmet template clip art is available for all ages to print and use on t - shirts
Drawing of a knight's helmet
Drawing of a knight's helmet
Helm of Solaire (Dark Souls) digital template for prop making
Helm of Solaire (Dark Souls) digital template for prop making
three different views of a motorcycle helmet
three different views of a motorcycle helmet
F1 Helmet Outline
F1 Helmet Outline
Download Template helmet half face, line Art helmet Vector Illustration, Line art vector, helmet Vector for free
Download Template helmet half face, line Art helmet Vector Illustration, Line art vector, helmet Vector for free
three views of the front, back and side of a motorcycle helmet on a white background
three views of the front, back and side of a motorcycle helmet on a white background
three different views of the white motorcycle helmet
three different views of the white motorcycle helmet
an image of a helmet with measurements and instructions for the parts to be used in this project
an image of a helmet with measurements and instructions for the parts to be used in this project

```yaml {{- range $index, $env := .Values.envs }} {{- if ne $env "dev" -}} apiVersion: apps/v1 kind: Deployment metadata: name: my-app-{{ $env }} spec: replicas: {{ add1 $index }} selector: matchLabels: app: my-app-{{ $env }} template: metadata: labels: app: my-app-{{ $env }} spec: containers: - name: my-app image: my-image:{{ $env }} {{- else -}} {{- end }} {{- end }} ```

In this code, the `if` statement checks if the current environment is not equal to `dev` (`ne $env "dev"`). If the condition is true, it generates the deployment. Otherwise, it skips to the end of the conditional block, skipping the deployment creation for the `dev` environment.

With Helm's powerful TPL language, you can create incredibly flexible and dynamic templates, bringing your IaC to the next level. Our example has barely scratched the surface of what's possible with Helm template ranges.

So, why wait? Dive into the world of Helm template ranges today and start unlocking the full potential of your Kubernetes deployments. Goodbye, static charts! Hello, dynamic Kubernetes deployments!