Skip to main content

Module: helm-release

This terraform-aws-helm-release module deploys a Helm chart with an option to create an EKS IAM Role for a Service Account (IRSA).

Usage

This module deploys a Helm chart with an option to create an EKS IAM Role for a Service Account (IRSA). It has many of the same features and limitations of Helm, and uses the Terraform Helm provider, specifically the helm_release resource.

NOTE: This module is just a convenient wrapper, packaging up 3 concepts:

  1. Deploying a Helm Chart to an EKS cluster
  2. Creating a Kubernetes namespace in the EKS cluster
  3. Creating an IAM role for a Kubernetes Service Account (which, in turn, is presumably created by deploying the Helm Chart)

Many issues may arise that are due to limitations of Helm, Kubernetes, EKS, Terraform, or the Terraform providers. Please address issues and complaints to the project that can potentially fix them, which will usually not be this module.

Provider requirements.

This module is unusual in that it requires you to configure 3 separate Terraform providers:

  1. AWS
  2. Helm
  3. Kubernetes

Cloud Posse maintains a provider-helm.tf file "mixin" for use in Cloud Posse components which you can also use as an example of how to configure the Helm and Kubernetes providers in your own component.

Creating a namespace

This module provides 2 options for creating the namespace the chart will be deployed to, for the case where you are deploying the chart into its own namespace that does not already exist.

  1. create_namespace_with_kubernetes will manage the namespace using a Terraform kubernetes_namespace resource. This is the recommended way to create the namespace, because it allows you to annotate (kubernetes_namespace_annotations) and label (kubernetes_namespace_labels) the namespace, and it provides proper sequencing of creation and destruction of deployments, resources, and IAM roles. When the deployment is destroyed with terraform destroy, the namespace will be deleted, too. This will delete everything else in the namespace (but never the Custom Resource Definitions, which themselves are non-namespaced), so if this is not the desired behavior, you should create the namespace in a separate Terraform component.
  2. create_namespace is the obsolete way to create a namespace, by delegating the responsibility to Helm. This is not recommended because it provides no control over the annotations or labels of the namespace, and when the deployment is destroyed with terraform destroy, the namespace will be left behind. This can cause problems with future deployments.

Note: You may have trouble deleting a release from within Terraform if the Kubernetes cluster has been modified outside of this module, for example if the namespace or the cluster itself has been deleted. You can delete the Terraform state if the resources are gone, using terraform state rm or even terraform workspace delete, or you can try using terraform destroy. In some cases, it may be helpful to set var.enabled to false while destroying:

terraform destroy -var enabled=false

For a complete example, see examples/complete.

module "helm_release" {
source = "cloudposse/helm-release/aws"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"

name = "echo"

repository = "https://charts.helm.sh/incubator"
chart = "raw"
chart_version = "0.2.5"

create_namespace = true
kubernetes_namespace = "echo"

atomic = true
cleanup_on_fail = true
timeout = 300
wait = true

# These values will be deep merged
# values = [
# ]

# Enable the IAM role
iam_role_enabled = true

# Add the IAM role using set()
service_account_role_arn_annotation_enabled = true

# Dictates which ServiceAccounts are allowed to assume the IAM Role.
# In this case, only the "echo" ServiceAccount in the "echo" namespace
# will be able to assume the IAM Role created by this module.
service_account_name = "echo"
service_account_namespace = "echo"

# IAM policy statements to add to the IAM role
iam_policy = [{
statements = [{
sid = "ListMyBucket"
effect = "Allow"
actions = ["s3:ListBucket"]
resources = ["arn:aws:s3:::test"]
conditions = []
},
{
sid = "WriteMyBucket"
effect = "Allow"
actions = ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"]
resources = ["arn:aws:s3:::test/*"]
conditions = []
}]
}]
}

Typically, the prefix for the full name of the created IAM role for the service account ends with the name value, supplied either via the name or the context input. If service_account_name is set to something other than *, the service account name is then appended to this prefix. In the case where name and service_account_name are the same, this leads to a repetition, for a name like eg-echo-echo. For this reason, we recommend setting name to "" when it would otherwise be the same as service_account_name:

module "helm_release" {
source = "cloudposse/helm-release/aws"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"

name = ""

create_namespace = true
kubernetes_namespace = "echo"

service_account_name = "echo"
service_account_namespace = "echo"

iam_role_enabled = true

service_account_role_arn_annotation_enabled = true

# ...
}

This will result in an IAM role with a name such as: eg-uw2-dev-echo@echo instead of eg-uw2-dev-echo-echo@echo. Additionally, if var.name is empty, the name used for the Helm Release will be that of var.chart.

Examples

Here is an example of using this module:

Variables

Required Variables

chart (string) required

Chart name to be installed. The chart name can be local path, a URL to a chart, or the name of the chart if repository is specified. It is also possible to use the <repository>/<chart> format here if you are running Terraform on a system that the repository has been added to with helm repo add but this is not recommended.

eks_cluster_oidc_issuer_url (string) required

OIDC issuer URL for the EKS cluster (initial "https://" may be omitted).

Optional Variables

atomic (bool) optional

If set, installation process purges chart on fail. The wait flag will be set automatically if atomic is used. Defaults to false.


Default value: null

aws_account_number (string) optional

AWS account number of EKS cluster owner.


Default value: null

aws_partition (string) optional

AWS partition: aws, aws-cn, or aws-us-gov. Applicable when var.iam_role_enabled is true.


Default value: "aws"

chart_version (string) optional

Specify the exact chart version to install. If this is not specified, the latest version is installed.


Default value: null

cleanup_on_fail (bool) optional

Allow deletion of new resources created in this upgrade when upgrade fails. Defaults to false.


Default value: null

create_namespace (bool) optional

(Not recommended, use create_namespace_with_kubernetes instead)
Create the namespace via Helm if it does not yet exist. Defaults to false.
Does not support annotations or labels. May have problems when destroying.
Ignored when create_namespace_with_kubernetes is set.



Default value: null

create_namespace_with_kubernetes (bool) optional

Create the namespace via Kubernetes if it does not yet exist. Defaults to false.
Must set true if you want to use namespace annotations or labels.



Default value: null

dependency_update (bool) optional

Runs helm dependency update before installing the chart. Defaults to false.


Default value: null

description (string) optional

Release description attribute (visible in the history).


Default value: null

devel (bool) optional

Use chart development versions, too. Equivalent to version >0.0.0-0. If version is set, this is ignored.


Default value: null

disable_openapi_validation (bool) optional

If set, the installation process will not validate rendered templates against the Kubernetes OpenAPI Schema. Defaults to false.


Default value: null

disable_webhooks (bool) optional

Prevent hooks from running. Defaults to false.


Default value: null

force_update (bool) optional

Force resource update through delete/recreate if needed. Defaults to false.


Default value: null

iam_override_policy_documents (list(string)) optional

List of IAM policy documents (as JSON strings) that are merged together into the exported document with higher precedence.
In merging, statements with non-blank SIDs will override statements with the same SID
from earlier documents in the list and from other "source" documents.



Default value: null

iam_policy optional

IAM policy as list of Terraform objects, compatible with Terraform aws_iam_policy_document data source
except that source_policy_documents and override_policy_documents are not included.
Use inputs iam_source_policy_documents and iam_override_policy_documents for that.



Type:

list(object({
policy_id = optional(string, null)
version = optional(string, null)
statements = list(object({
sid = optional(string, null)
effect = optional(string, null)
actions = optional(list(string), null)
not_actions = optional(list(string), null)
resources = optional(list(string), null)
not_resources = optional(list(string), null)
conditions = optional(list(object({
test = string
variable = string
values = list(string)
})), [])
principals = optional(list(object({
type = string
identifiers = list(string)
})), [])
not_principals = optional(list(object({
type = string
identifiers = list(string)
})), [])
}))
}))

Default value: null

iam_policy_enabled (bool) optional

Whether to create and attach an IAM policy to the created IAM role


Default value: true

iam_policy_statements (any) optional

Deprecated: Use iam_policy instead.
List or Map of IAM policy statements to use in the policy.
This can be used with iam_source_policy_documents and iam_override_policy_documents
and with or instead of iam_source_json_url.



Default value: { }

iam_role_enabled (bool) optional

Whether to create an IAM role. Setting this to true will also replace any occurrences of {service_account_role_arn} in var.values_template_path with the ARN of the IAM role created by this module.


Default value: false

iam_source_json_url (string) optional

URL of the IAM policy (in JSON format) to download and use as source_json argument.
This is useful when using a 3rd party service that provides their own policy.
Statements in this policy will be overridden by statements with the same SID in iam_override_policy_documents.



Default value: null

iam_source_policy_documents (list(string)) optional

List of IAM policy documents (as JSON strings) that are merged together into the exported document.
Statements defined in iam_source_policy_documents must have unique SIDs and be distinct from SIDs
in iam_policy and deprecated iam_policy_statements.
Statements in these documents will be overridden by statements with the same SID in iam_override_policy_documents.



Default value: null

keyring (string) optional

Location of public keys used for verification. Used only if verify is true. Defaults to /.gnupg/pubring.gpg in the location set by home.


Default value: null

kubernetes_namespace (string) optional

The namespace to install the release into. Defaults to default.


Default value: null

kubernetes_namespace_annotations (map(string)) optional

Annotations to be added to the created namespace. Ignored unless create_namespace_with_kubernetes is true.


Default value: { }

kubernetes_namespace_labels (map(string)) optional

Labels to be added to the created namespace. Ignored unless create_namespace_with_kubernetes is true.


Default value: { }

lint (bool) optional

Run the helm chart linter during the plan. Defaults to false.


Default value: null

max_history (number) optional

Maximum number of release versions stored per release. Defaults to 0 (no limit).


Default value: null

permissions_boundary (string) optional

ARN of the policy that is used to set the permissions boundary for the role.


Default value: null

postrender_binary_path (string) optional

Relative or full path to command binary.


Default value: null

recreate_pods (bool) optional

Perform pods restart during upgrade/rollback. Defaults to false.


Default value: null

release_name (string) optional

The name of the release to be installed. If omitted, use the name input, and if that's omitted, use the chart input.


Default value: ""

render_subchart_notes (bool) optional

If set, render subchart notes along with the parent. Defaults to true.


Default value: null

replace (bool) optional

Re-use the given name, even if that name is already used. This is unsafe in production. Defaults to false.


Default value: null

repository (string) optional

Repository URL where to locate the requested chart.


Default value: null

repository_ca_file (string) optional

The Repositories CA file.


Default value: null

repository_cert_file (string) optional

The repositories cert file.


Default value: null

repository_key_file (string) optional

The repositories cert key file.


Default value: null

repository_password (string) optional

Password for HTTP basic authentication against the repository.


Default value: null

repository_username (string) optional

Username for HTTP basic authentication against the repository.


Default value: null

reset_values (bool) optional

When upgrading, reset the values to the ones built into the chart. Defaults to false.


Default value: null

reuse_values (bool) optional

When upgrading, reuse the last release's values and merge in any overrides. If reset_values is specified, this is ignored. Defaults to false.


Default value: null

service_account_name (string) optional

Name of the Kubernetes ServiceAccount allowed to assume the IAM role created when var.iam_role_enabled is set to true.


In combination with var.service_account_namespace, this variable is used to determine which ServiceAccounts are allowed
to assume the IAM role in question.


It is not recommended to leave this variable as null or "", as this would mean ServiceAccounts of any name in the
namespace specified by var.service_account_namespace are allowed to assume the IAM role in question. If both variables
are omitted, then a ServiceAccount of any name in any namespace will be able to assume the IAM role in question, which
is the least secure scenario.


The best practice is to set this variable to the name of the ServiceAccount created by the Helm Chart.



Default value: null

service_account_namespace (string) optional

Kubernetes Namespace of the Kubernetes ServiceAccount allowed to assume the IAM role created when var.iam_role_enabled
is set to true.


In combination with var.service_account_name, this variable is used to determine which ServiceAccounts are allowed
to assume the IAM role in question.


It is not recommended to leave this variable as null or "", as this would mean any ServiceAccounts matching the
name specified by var.service_account_name in any namespace are allowed to assume the IAM role in question. If both
variables are omitted, then a ServiceAccount of any name in any namespace will be able to assume the IAM role in question,
which is the least secure scenario.


The best practice is to set this variable to the namespace of the ServiceAccount created by the Helm Chart.



Default value: null

service_account_role_arn_annotation_enabled (bool) optional

Whether or not to dynamically insert an eks.amazonaws.com/role-arn annotation into $var.service_account_set_key_path.annotations
(by default, serviceAccount.annotations), with the value being the ARN of the IAM role created when var.iam_role_enabled.


Assuming the Helm Chart follows the standard convention of rendering ServiceAccount annotations in serviceAccount.annotations
(or a similar convention, which can be overriden by var.service_account_set_key_path as needed),
this allows the ServiceAccount created by the Helm Chart to assume the IAM Role in question via the EKS OIDC IdP, without
the consumer of this module having to set this annotation via var.values or var.set, which would involve manually
rendering the IAM Role ARN beforehand.


Ignored if var.iam_role_enabled is false.



Default value: true

service_account_set_key_path (string) optional

The key path used by Helm Chart values for ServiceAccount-related settings (e.g. serviceAccount... or rbac.serviceAccount...).


Ignored if either var.service_account_role_arn_annotation_enabled or var.iam_role_enabled are set to false.



Default value: "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"

set optional

Value block with custom values to be merged with the values yaml.


Type:

list(object({
name = string
value = string
type = string
}))

Default value: [ ]

set_sensitive optional

Value block with custom sensitive values to be merged with the values yaml that won't be exposed in the plan's diff.


Type:

list(object({
name = string
value = string
type = string
}))

Default value: [ ]

skip_crds (bool) optional

If set, no CRDs will be installed. By default, CRDs are installed if not already present. Defaults to false.


Default value: null

timeout (number) optional

Time in seconds to wait for any individual kubernetes operation (like Jobs for hooks). Defaults to 300 seconds.


Default value: null

values (any) optional

List of values in raw yaml to pass to helm. Values will be merged, in order, as Helm does with multiple -f options.


Default value: null

verify (bool) optional

Verify the package before installing it. Helm uses a provenance file to verify the integrity of the chart; this must be hosted alongside the chart. For more information see the Helm Documentation. Defaults to false.


Default value: null

wait (bool) optional

Will wait until all resources are in a ready state before marking the release as successful. It will wait for as long as timeout. Defaults to true.


Default value: null

wait_for_jobs (bool) optional

If wait is enabled, will wait until all Jobs have been completed before marking the release as successful. It will wait for as long as timeout. Defaults to false.


Default value: null

Context Variables

The following variables are defined in the context.tf file of this module and part of the terraform-null-label pattern.

additional_tag_map (map(string)) optional

Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.


Required: No

Default value: { }

attributes (list(string)) optional

ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.


Required: No

Default value: [ ]

context (any) optional

Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.


Required: No

Default value:

{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
delimiter (string) optional

Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.


Required: No

Default value: null

descriptor_formats (any) optional

Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
\{<br/> format = string<br/> labels = list(string)<br/> \}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).


Required: No

Default value: { }

enabled (bool) optional

Set to false to prevent the module from creating any resources
Required: No

Default value: null

environment (string) optional

ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: No

Default value: null

id_length_limit (number) optional

Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.


Required: No

Default value: null

label_key_case (string) optional

Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.


Required: No

Default value: null

label_order (list(string)) optional

The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.


Required: No

Default value: null

label_value_case (string) optional

Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.


Required: No

Default value: null

labels_as_tags (set(string)) optional

Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.


Required: No

Default value:

[
"default"
]
name (string) optional

ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.


Required: No

Default value: null

namespace (string) optional

ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: No

Default value: null

regex_replace_chars (string) optional

Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.


Required: No

Default value: null

stage (string) optional

ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: No

Default value: null

tags (map(string)) optional

Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.


Required: No

Default value: { }

tenant (string) optional

ID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: No

Default value: null

Outputs

metadata

Block status of the deployed release.

service_account_name

Kubernetes Service Account name

service_account_namespace

Kubernetes Service Account namespace

service_account_policy_arn

IAM policy ARN

service_account_policy_id

IAM policy ID

service_account_policy_name

IAM policy name

service_account_role_arn

IAM role ARN

service_account_role_name

IAM role name

service_account_role_unique_id

IAM role unique ID

Dependencies

Requirements

  • terraform, version: >= 1.3.0
  • helm, version: >= 2.2
  • kubernetes, version: >= 2.7.1

Providers

  • helm, version: >= 2.2
  • kubernetes, version: >= 2.7.1

Modules

NameVersionSourceDescription
eks_iam_policy2.0.1cloudposse/iam-policy/awsn/a
eks_iam_role2.1.1cloudposse/eks-iam-role/awsn/a
this0.25.0cloudposse/label/nulln/a

Resources

The following resources are used by this module:

Data Sources

The following data sources are used by this module: