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:

Requirements

NameVersion
terraform>= 1.3.0
helm>= 2.2
kubernetes>= 2.7.1

Providers

NameVersion
helm>= 2.2
kubernetes>= 2.7.1

Modules

NameSourceVersion
eks_iam_policycloudposse/iam-policy/aws2.0.1
eks_iam_rolecloudposse/eks-iam-role/aws2.1.1
thiscloudposse/label/null0.25.0

Resources

NameType
helm_release.thisresource
kubernetes_namespace.defaultresource

Inputs

NameDescriptionTypeDefaultRequired
additional_tag_mapAdditional 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.
map(string){}no
atomicIf set, installation process purges chart on fail. The wait flag will be set automatically if atomic is used. Defaults to false.boolnullno
attributesID 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.
list(string)[]no
aws_account_numberAWS account number of EKS cluster owner.stringnullno
aws_partitionAWS partition: aws, aws-cn, or aws-us-gov. Applicable when var.iam_role_enabled is true.string"aws"no
chartChart 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.stringn/ayes
chart_versionSpecify the exact chart version to install. If this is not specified, the latest version is installed.stringnullno
cleanup_on_failAllow deletion of new resources created in this upgrade when upgrade fails. Defaults to false.boolnullno
contextSingle 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.
any
{
"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
}
no
create_namespace(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.
boolnullno
create_namespace_with_kubernetesCreate 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.
boolnullno
delimiterDelimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
stringnullno
dependency_updateRuns helm dependency update before installing the chart. Defaults to false.boolnullno
descriptionRelease description attribute (visible in the history).stringnullno
descriptor_formatsDescribe 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).
any{}no
develUse chart development versions, too. Equivalent to version >0.0.0-0. If version is set, this is ignored.boolnullno
disable_openapi_validationIf set, the installation process will not validate rendered templates against the Kubernetes OpenAPI Schema. Defaults to false.boolnullno
disable_webhooksPrevent hooks from running. Defaults to false.boolnullno
eks_cluster_oidc_issuer_urlOIDC issuer URL for the EKS cluster (initial "https://" may be omitted).stringn/ayes
enabledSet to false to prevent the module from creating any resourcesboolnullno
environmentID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'stringnullno
force_updateForce resource update through delete/recreate if needed. Defaults to false.boolnullno
iam_override_policy_documentsList 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.
list(string)nullno
iam_policyIAM 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.
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)
})), [])
}))
}))
nullno
iam_policy_enabledWhether to create and attach an IAM policy to the created IAM rolebooltrueno
iam_policy_statementsDeprecated: 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.
any{}no
iam_role_enabledWhether 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.boolfalseno
iam_source_json_urlURL 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.
stringnullno
iam_source_policy_documentsList 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.
list(string)nullno
id_length_limitLimit 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.
numbernullno
keyringLocation of public keys used for verification. Used only if verify is true. Defaults to /.gnupg/pubring.gpg in the location set by home.stringnullno
kubernetes_namespaceThe namespace to install the release into. Defaults to default.stringnullno
kubernetes_namespace_annotationsAnnotations to be added to the created namespace. Ignored unless create_namespace_with_kubernetes is true.map(string){}no
kubernetes_namespace_labelsLabels to be added to the created namespace. Ignored unless create_namespace_with_kubernetes is true.map(string){}no
label_key_caseControls 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.
stringnullno
label_orderThe 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.
list(string)nullno
label_value_caseControls 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.
stringnullno
labels_as_tagsSet 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.
set(string)
[
"default"
]
no
lintRun the helm chart linter during the plan. Defaults to false.boolnullno
max_historyMaximum number of release versions stored per release. Defaults to 0 (no limit).numbernullno
nameID 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.
stringnullno
namespaceID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally uniquestringnullno
permissions_boundaryARN of the policy that is used to set the permissions boundary for the role.stringnullno
postrender_binary_pathRelative or full path to command binary.stringnullno
recreate_podsPerform pods restart during upgrade/rollback. Defaults to false.boolnullno
regex_replace_charsTerraform 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.
stringnullno
release_nameThe name of the release to be installed. If omitted, use the name input, and if that's omitted, use the chart input.string""no
render_subchart_notesIf set, render subchart notes along with the parent. Defaults to true.boolnullno
replaceRe-use the given name, even if that name is already used. This is unsafe in production. Defaults to false.boolnullno
repositoryRepository URL where to locate the requested chart.stringnullno
repository_ca_fileThe Repositories CA file.stringnullno
repository_cert_fileThe repositories cert file.stringnullno
repository_key_fileThe repositories cert key file.stringnullno
repository_passwordPassword for HTTP basic authentication against the repository.stringnullno
repository_usernameUsername for HTTP basic authentication against the repository.stringnullno
reset_valuesWhen upgrading, reset the values to the ones built into the chart. Defaults to false.boolnullno
reuse_valuesWhen upgrading, reuse the last release's values and merge in any overrides. If reset_values is specified, this is ignored. Defaults to false.boolnullno
service_account_nameName 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.
stringnullno
service_account_namespaceKubernetes 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.
stringnullno
service_account_role_arn_annotation_enabledWhether 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.
booltrueno
service_account_set_key_pathThe 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.
string"serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"no
setValue block with custom values to be merged with the values yaml.
list(object({
name = string
value = string
type = string
}))
[]no
set_sensitiveValue block with custom sensitive values to be merged with the values yaml that won't be exposed in the plan's diff.
list(object({
name = string
value = string
type = string
}))
[]no
skip_crdsIf set, no CRDs will be installed. By default, CRDs are installed if not already present. Defaults to false.boolnullno
stageID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'stringnullno
tagsAdditional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string){}no
tenantID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is forstringnullno
timeoutTime in seconds to wait for any individual kubernetes operation (like Jobs for hooks). Defaults to 300 seconds.numbernullno
valuesList of values in raw yaml to pass to helm. Values will be merged, in order, as Helm does with multiple -f options.anynullno
verifyVerify 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.boolnullno
waitWill 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.boolnullno
wait_for_jobsIf 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.boolnullno

Outputs

NameDescription
metadataBlock status of the deployed release.
service_account_nameKubernetes Service Account name
service_account_namespaceKubernetes Service Account namespace
service_account_policy_arnIAM policy ARN
service_account_policy_idIAM policy ID
service_account_policy_nameIAM policy name
service_account_role_arnIAM role ARN
service_account_role_nameIAM role name
service_account_role_unique_idIAM role unique ID