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:
- Deploying a Helm Chart to an EKS cluster
- Creating a Kubernetes namespace in the EKS cluster
- 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:
- AWS
- Helm
- 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.
create_namespace_with_kuberneteswill manage the namespace using a Terraformkubernetes_namespaceresource. 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 withterraform 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.create_namespaceis 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 withterraform 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:
examples/complete- complete example of using this module
Variables
Required Variables
chart(string) requiredChart name to be installed. The chart name can be local path, a URL to a chart, or the name of the chart if
repositoryis 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 withhelm repo addbut this is not recommended.eks_cluster_oidc_issuer_url(string) requiredOIDC issuer URL for the EKS cluster (initial "https://" may be omitted).
Optional Variables
atomic(bool) optionalIf set, installation process purges chart on fail. The wait flag will be set automatically if atomic is used. Defaults to
false.Default value:
nullaws_account_number(string) optionalAWS account number of EKS cluster owner.
Default value:
nullaws_partition(string) optionalAWS partition:
aws,aws-cn, oraws-us-gov. Applicable whenvar.iam_role_enabledistrue.Default value:
"aws"chart_version(string) optionalSpecify the exact chart version to install. If this is not specified, the latest version is installed.
Default value:
nullcleanup_on_fail(bool) optionalAllow deletion of new resources created in this upgrade when upgrade fails. Defaults to
false.Default value:
nullcreate_namespace(bool) optional(Not recommended, use
create_namespace_with_kubernetesinstead)
Create the namespace via Helm if it does not yet exist. Defaults tofalse.
Does not support annotations or labels. May have problems when destroying.
Ignored whencreate_namespace_with_kubernetesis set.Default value:
nullcreate_namespace_with_kubernetes(bool) optionalCreate the namespace via Kubernetes if it does not yet exist. Defaults to
false.
Must settrueif you want to use namespace annotations or labels.Default value:
nulldependency_update(bool) optionalRuns helm dependency update before installing the chart. Defaults to
false.Default value:
nulldescription(string) optionalRelease description attribute (visible in the history).
Default value:
nulldevel(bool) optionalUse chart development versions, too. Equivalent to version
>0.0.0-0. If version is set, this is ignored.Default value:
nulldisable_openapi_validation(bool) optionalIf set, the installation process will not validate rendered templates against the Kubernetes OpenAPI Schema. Defaults to
false.Default value:
nulldisable_webhooks(bool) optionalPrevent hooks from running. Defaults to
false.Default value:
nullforce_update(bool) optionalForce resource update through delete/recreate if needed. Defaults to
false.Default value:
nulliam_override_policy_documents(list(string)) optionalList 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:
nulliam_policyoptionalIAM policy as list of Terraform objects, compatible with Terraform
aws_iam_policy_documentdata source
except thatsource_policy_documentsandoverride_policy_documentsare not included.
Use inputsiam_source_policy_documentsandiam_override_policy_documentsfor 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:
nulliam_policy_enabled(bool) optionalWhether to create and attach an IAM policy to the created IAM role
Default value:
trueiam_policy_statements(any) optionalDeprecated: Use
iam_policyinstead.
List or Map of IAM policy statements to use in the policy.
This can be used withiam_source_policy_documentsandiam_override_policy_documents
and with or instead ofiam_source_json_url.Default value:
{ }iam_role_enabled(bool) optionalWhether to create an IAM role. Setting this to
truewill also replace any occurrences of{service_account_role_arn}invar.values_template_pathwith the ARN of the IAM role created by this module.Default value:
falseiam_source_json_url(string) optionalURL of the IAM policy (in JSON format) to download and use as
source_jsonargument.
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 iniam_override_policy_documents.Default value:
nulliam_source_policy_documents(list(string)) optionalList of IAM policy documents (as JSON strings) that are merged together into the exported document.
Statements defined iniam_source_policy_documentsmust have unique SIDs and be distinct from SIDs
iniam_policyand deprecatediam_policy_statements.
Statements in these documents will be overridden by statements with the same SID iniam_override_policy_documents.Default value:
nullkeyring(string) optionalLocation of public keys used for verification. Used only if
verifyis true. Defaults to/.gnupg/pubring.gpgin the location set byhome.Default value:
nullkubernetes_namespace(string) optionalThe namespace to install the release into. Defaults to
default.Default value:
nullkubernetes_namespace_annotations(map(string)) optionalAnnotations to be added to the created namespace. Ignored unless
create_namespace_with_kubernetesistrue.Default value:
{ }kubernetes_namespace_labels(map(string)) optionalLabels to be added to the created namespace. Ignored unless
create_namespace_with_kubernetesistrue.Default value:
{ }lint(bool) optionalRun the helm chart linter during the plan. Defaults to
false.Default value:
nullmax_history(number) optionalMaximum number of release versions stored per release. Defaults to
0(no limit).Default value:
nullpermissions_boundary(string) optionalARN of the policy that is used to set the permissions boundary for the role.
Default value:
nullpostrender_binary_path(string) optionalRelative or full path to command binary.
Default value:
nullrecreate_pods(bool) optionalPerform pods restart during upgrade/rollback. Defaults to
false.Default value:
nullrelease_name(string) optionalThe 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) optionalIf set, render subchart notes along with the parent. Defaults to
true.Default value:
nullreplace(bool) optionalRe-use the given name, even if that name is already used. This is unsafe in production. Defaults to
false.Default value:
nullrepository(string) optionalRepository URL where to locate the requested chart.
Default value:
nullrepository_ca_file(string) optionalThe Repositories CA file.
Default value:
nullrepository_cert_file(string) optionalThe repositories cert file.
Default value:
nullrepository_key_file(string) optionalThe repositories cert key file.
Default value:
nullrepository_password(string) optionalPassword for HTTP basic authentication against the repository.
Default value:
nullrepository_username(string) optionalUsername for HTTP basic authentication against the repository.
Default value:
nullreset_values(bool) optionalWhen upgrading, reset the values to the ones built into the chart. Defaults to
false.Default value:
nullreuse_values(bool) optionalWhen upgrading, reuse the last release's values and merge in any overrides. If
reset_valuesis specified, this is ignored. Defaults tofalse.Default value:
nullservice_account_name(string) optionalName of the Kubernetes ServiceAccount allowed to assume the IAM role created when
var.iam_role_enabledis set totrue.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
nullor"", as this would mean ServiceAccounts of any name in the
namespace specified byvar.service_account_namespaceare 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:
nullservice_account_namespace(string) optionalKubernetes Namespace of the Kubernetes ServiceAccount allowed to assume the IAM role created when
var.iam_role_enabled
is set totrue.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
nullor"", as this would mean any ServiceAccounts matching the
name specified byvar.service_account_namein 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:
nullservice_account_role_arn_annotation_enabled(bool) optionalWhether or not to dynamically insert an
eks.amazonaws.com/role-arnannotation into$var.service_account_set_key_path.annotations
(by default,serviceAccount.annotations), with the value being the ARN of the IAM role created whenvar.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 byvar.service_account_set_key_pathas 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 viavar.valuesorvar.set, which would involve manually
rendering the IAM Role ARN beforehand.Ignored if
var.iam_role_enabledisfalse.Default value:
trueservice_account_set_key_path(string) optionalThe key path used by Helm Chart values for ServiceAccount-related settings (e.g.
serviceAccount...orrbac.serviceAccount...).Ignored if either
var.service_account_role_arn_annotation_enabledorvar.iam_role_enabledare set tofalse.Default value:
"serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"setoptionalValue block with custom values to be merged with the values yaml.
Type:
list(object({
name = string
value = string
type = string
}))Default value:
[ ]set_sensitiveoptionalValue 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) optionalIf set, no CRDs will be installed. By default, CRDs are installed if not already present. Defaults to
false.Default value:
nulltimeout(number) optionalTime in seconds to wait for any individual kubernetes operation (like Jobs for hooks). Defaults to
300seconds.Default value:
nullvalues(any) optionalList of values in raw yaml to pass to helm. Values will be merged, in order, as Helm does with multiple
-foptions.Default value:
nullverify(bool) optionalVerify 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:
nullwait(bool) optionalWill wait until all resources are in a ready state before marking the release as successful. It will wait for as long as
timeout. Defaults totrue.Default value:
nullwait_for_jobs(bool) optionalIf 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 tofalse.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.
context.tf file of this module and part of the terraform-null-label pattern.additional_tag_map(map(string)) optionalAdditional key-value pairs to add to each map in
tags_as_list_of_maps. Not added totagsorid.
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)) optionalID element. Additional attributes (e.g.
workersorcluster) to add toid,
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 thedelimiter
and treated as a single ID element.Required: No
Default value:
[ ]context(any) optionalSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables asnullto 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) optionalDelimiter to be used between ID elements.
Defaults to-(hyphen). Set to""to use no delimiter at all.Required: No
Default value:
nulldescriptor_formats(any) optionalDescribe additional descriptors to be output in the
descriptorsoutput map.
Map of maps. Keys are names of descriptors. Values are maps of the form
\{<br/> format = string<br/> labels = list(string)<br/> \}
(Type isanyso the map values can later be enhanced to provide additional options.)
formatis a Terraform format string to be passed to theformat()function.
labelsis a list of labels, in order, to pass toformat()function.
Label values will be normalized before being passed toformat()so they will be
identical to how they appear inid.
Default is{}(descriptorsoutput will be empty).Required: No
Default value:
{ }enabled(bool) optionalSet to false to prevent the module from creating any resources
Required: NoDefault value:
nullenvironment(string) optionalID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: NoDefault value:
nullid_length_limit(number) optionalLimit
idto this many characters (minimum 6).
Set to0for unlimited length.
Set tonullfor keep the existing setting, which defaults to0.
Does not affectid_full.Required: No
Default value:
nulllabel_key_case(string) optionalControls the letter case of the
tagskeys (label names) for tags generated by this module.
Does not affect keys of tags passed in via thetagsinput.
Possible values:lower,title,upper.
Default value:title.Required: No
Default value:
nulllabel_order(list(string)) optionalThe 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:
nulllabel_value_case(string) optionalControls 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 thetagsinput.
Possible values:lower,title,upperandnone(no transformation).
Set this totitleand setdelimiterto""to yield Pascal Case IDs.
Default value:lower.Required: No
Default value:
nulllabels_as_tags(set(string)) optionalSet of labels (ID elements) to include as tags in the
tagsoutput.
Default is to include all labels.
Tags with empty values will not be included in thetagsoutput.
Set to[]to suppress all generated tags.
Notes:
The value of thenametag, if included, will be theid, not thename.
Unlike othernull-labelinputs, the initial setting oflabels_as_tagscannot be
changed in later chained modules. Attempts to change it will be silently ignored.Required: No
Default value:
[
"default"
]name(string) optionalID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as atag.
The "name" tag is set to the fullidstring. There is no tag with the value of thenameinput.Required: No
Default value:
nullnamespace(string) optionalID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: NoDefault value:
nullregex_replace_chars(string) optionalTerraform 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:
nullstage(string) optionalID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: NoDefault value:
nulltags(map(string)) optionalAdditional 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) optionalID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: NoDefault value:
null
Outputs
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
Dependencies
Requirements
terraform, version:>= 1.3.0helm, version:>= 2.2kubernetes, version:>= 2.7.1
Providers
helm, version:>= 2.2kubernetes, version:>= 2.7.1
Modules
| Name | Version | Source | Description |
|---|---|---|---|
eks_iam_policy | 2.0.1 | cloudposse/iam-policy/aws | n/a |
eks_iam_role | 2.1.1 | cloudposse/eks-iam-role/aws | n/a |
this | 0.25.0 | cloudposse/label/null | n/a |
Resources
The following resources are used by this module:
helm_release.this(resource)kubernetes_namespace.default(resource)
Data Sources
The following data sources are used by this module: