This terraform-aws-helm-release
module deploys a Helm chart with
an option to create an EKS IAM Role for a Service Account (IRSA).
Tip
Cloud Posse uses atmos
to easily orchestrate multiple environments using Terraform.
Works with Github Actions, Atlantis, or Spacelift.
Watch demo of using Atmos with Terraform
Example of running
atmos
to manage infrastructure from our Quick Start tutorial.
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.
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.
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_kubernetes
will manage the namespace using a Terraformkubernetes_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 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_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 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
.
Important
In Cloud Posse's examples, we avoid pinning modules to specific versions to prevent discrepancies between the documentation and the latest released versions. However, for your own projects, we strongly advise pinning each module to the exact version you're using. This practice ensures the stability of your infrastructure. Additionally, we recommend implementing a systematic approach for updating versions to avoid unexpected changes.
Here is an example of using this module:
examples/complete
- complete example of using this module
Available targets:
help Help screen
help/all Display help for all targets
help/short This help short screen
lint Lint terraform code
Name | Version |
---|---|
terraform | >= 1.3.0 |
helm | >= 2.2 |
kubernetes | >= 2.7.1 |
Name | Version |
---|---|
helm | >= 2.2 |
kubernetes | >= 2.7.1 |
Name | Source | Version |
---|---|---|
eks_iam_policy | cloudposse/iam-policy/aws | 2.0.1 |
eks_iam_role | cloudposse/eks-iam-role/aws | 2.1.1 |
this | cloudposse/label/null | 0.25.0 |
Name | Type |
---|---|
helm_release.this | resource |
kubernetes_namespace.default | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
additional_tag_map | 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. |
map(string) |
{} |
no |
atomic | If set, installation process purges chart on fail. The wait flag will be set automatically if atomic is used. Defaults to false . |
bool |
null |
no |
attributes | 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. |
list(string) |
[] |
no |
aws_account_number | AWS account number of EKS cluster owner. | string |
null |
no |
aws_partition | AWS partition: aws , aws-cn , or aws-us-gov . Applicable when var.iam_role_enabled is true . |
string |
"aws" |
no |
chart | 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. |
string |
n/a | yes |
chart_version | Specify the exact chart version to install. If this is not specified, the latest version is installed. | string |
null |
no |
cleanup_on_fail | Allow deletion of new resources created in this upgrade when upgrade fails. Defaults to false . |
bool |
null |
no |
context | 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. |
any |
{ |
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. |
bool |
null |
no |
create_namespace_with_kubernetes | 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. |
bool |
null |
no |
delimiter | Delimiter to be used between ID elements. Defaults to - (hyphen). Set to "" to use no delimiter at all. |
string |
null |
no |
dependency_update | Runs helm dependency update before installing the chart. Defaults to false . |
bool |
null |
no |
description | Release description attribute (visible in the history). | string |
null |
no |
descriptor_formats | 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 beidentical to how they appear in id .Default is {} (descriptors output will be empty). |
any |
{} |
no |
devel | Use chart development versions, too. Equivalent to version >0.0.0-0 . If version is set, this is ignored. |
bool |
null |
no |
disable_openapi_validation | If set, the installation process will not validate rendered templates against the Kubernetes OpenAPI Schema. Defaults to false . |
bool |
null |
no |
disable_webhooks | Prevent hooks from running. Defaults to false . |
bool |
null |
no |
eks_cluster_oidc_issuer_url | OIDC issuer URL for the EKS cluster (initial "https://" may be omitted). | string |
n/a | yes |
enabled | Set to false to prevent the module from creating any resources | bool |
null |
no |
environment | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | string |
null |
no |
force_update | Force resource update through delete/recreate if needed. Defaults to false . |
bool |
null |
no |
iam_override_policy_documents | 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. |
list(string) |
null |
no |
iam_policy | IAM policy as list of Terraform objects, compatible with Terraform aws_iam_policy_document data sourceexcept 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({ |
null |
no |
iam_policy_enabled | Whether to create and attach an IAM policy to the created IAM role | bool |
true |
no |
iam_policy_statements | 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 . |
any |
{} |
no |
iam_role_enabled | 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. |
bool |
false |
no |
iam_source_json_url | 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 . |
string |
null |
no |
iam_source_policy_documents | 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 SIDsin 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) |
null |
no |
id_length_limit | 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 . |
number |
null |
no |
keyring | Location of public keys used for verification. Used only if verify is true. Defaults to /.gnupg/pubring.gpg in the location set by home . |
string |
null |
no |
kubernetes_namespace | The namespace to install the release into. Defaults to default . |
string |
null |
no |
kubernetes_namespace_annotations | Annotations to be added to the created namespace. Ignored unless create_namespace_with_kubernetes is true . |
map(string) |
{} |
no |
kubernetes_namespace_labels | Labels to be added to the created namespace. Ignored unless create_namespace_with_kubernetes is true . |
map(string) |
{} |
no |
label_key_case | 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 . |
string |
null |
no |
label_order | 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. |
list(string) |
null |
no |
label_value_case | 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 . |
string |
null |
no |
labels_as_tags | 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 bechanged in later chained modules. Attempts to change it will be silently ignored. |
set(string) |
[ |
no |
lint | Run the helm chart linter during the plan. Defaults to false . |
bool |
null |
no |
max_history | Maximum number of release versions stored per release. Defaults to 0 (no limit). |
number |
null |
no |
name | 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. |
string |
null |
no |
namespace | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | string |
null |
no |
permissions_boundary | ARN of the policy that is used to set the permissions boundary for the role. | string |
null |
no |
postrender_binary_path | Relative or full path to command binary. | string |
null |
no |
recreate_pods | Perform pods restart during upgrade/rollback. Defaults to false . |
bool |
null |
no |
regex_replace_chars | 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. |
string |
null |
no |
release_name | The 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_notes | If set, render subchart notes along with the parent. Defaults to true . |
bool |
null |
no |
replace | Re-use the given name, even if that name is already used. This is unsafe in production. Defaults to false . |
bool |
null |
no |
repository | Repository URL where to locate the requested chart. | string |
null |
no |
repository_ca_file | The Repositories CA file. | string |
null |
no |
repository_cert_file | The repositories cert file. | string |
null |
no |
repository_key_file | The repositories cert key file. | string |
null |
no |
repository_password | Password for HTTP basic authentication against the repository. | string |
null |
no |
repository_username | Username for HTTP basic authentication against the repository. | string |
null |
no |
reset_values | When upgrading, reset the values to the ones built into the chart. Defaults to false . |
bool |
null |
no |
reuse_values | When upgrading, reuse the last release's values and merge in any overrides. If reset_values is specified, this is ignored. Defaults to false . |
bool |
null |
no |
service_account_name | 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 allowedto 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 thenamespace specified by var.service_account_namespace are allowed to assume the IAM role in question. If both variablesare 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. |
string |
null |
no |
service_account_namespace | 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 allowedto assume the IAM role in question. It is not recommended to leave this variable as null or "" , as this would mean any ServiceAccounts matching thename specified by var.service_account_name in any namespace are allowed to assume the IAM role in question. If bothvariables 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. |
string |
null |
no |
service_account_role_arn_annotation_enabled | 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 manuallyrendering the IAM Role ARN beforehand. Ignored if var.iam_role_enabled is false . |
bool |
true |
no |
service_account_set_key_path | 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 . |
string |
"serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn" |
no |
set | Value block with custom values to be merged with the values yaml. | list(object({ |
[] |
no |
set_sensitive | Value block with custom sensitive values to be merged with the values yaml that won't be exposed in the plan's diff. | list(object({ |
[] |
no |
skip_crds | If set, no CRDs will be installed. By default, CRDs are installed if not already present. Defaults to false . |
bool |
null |
no |
stage | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | string |
null |
no |
tags | Additional tags (e.g. {'BusinessUnit': 'XYZ'} ).Neither the tag keys nor the tag values will be modified by this module. |
map(string) |
{} |
no |
tenant | ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for | string |
null |
no |
timeout | Time in seconds to wait for any individual kubernetes operation (like Jobs for hooks). Defaults to 300 seconds. |
number |
null |
no |
values | List of values in raw yaml to pass to helm. Values will be merged, in order, as Helm does with multiple -f options. |
any |
null |
no |
verify | 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 . |
bool |
null |
no |
wait | 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 . |
bool |
null |
no |
wait_for_jobs | 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 . |
bool |
null |
no |
Name | Description |
---|---|
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 |
Check out these related projects.
- terraform-aws-iam-policy - Terraform module to create an IAM Policy document from Terraform inputs.
- terraform-aws-eks-iam-role - Terraform module to provision an EKS IAM Role for Service Account.
- terraform-null-label - Terraform module designed to generate consistent names and tags for resources. Use terraform-null-label to implement a strict naming convention.
For additional context, refer to some of these links.
- Helm - Helm: The package manager for Kubernetes.
- IAM Roles for Service Accounts - HashiCorp's guidance on all the requirements for publishing a module. Meeting the requirements for publishing a module is extremely easy.
- Terraform Version Pinning - The required_version setting can be used to constrain which versions of the Terraform CLI can be used with your configuration
Tip
Use Cloud Posse's ready-to-go terraform architecture blueprints for AWS to get up and running quickly.
✅ We build it together with your team.
✅ Your team owns everything.
✅ 100% Open Source and backed by fanatical support.
📚 Learn More
Cloud Posse is the leading DevOps Accelerator for funded startups and enterprises.
Your team can operate like a pro today.
Ensure that your team succeeds by using Cloud Posse's proven process and turnkey blueprints. Plus, we stick around until you succeed.
- Reference Architecture. You'll get everything you need from the ground up built using 100% infrastructure as code.
- Deployment Strategy. Adopt a proven deployment strategy with GitHub Actions, enabling automated, repeatable, and reliable software releases.
- Site Reliability Engineering. Gain total visibility into your applications and services with Datadog, ensuring high availability and performance.
- Security Baseline. Establish a secure environment from the start, with built-in governance, accountability, and comprehensive audit logs, safeguarding your operations.
- GitOps. Empower your team to manage infrastructure changes confidently and efficiently through Pull Requests, leveraging the full power of GitHub Actions.
- Training. Equip your team with the knowledge and skills to confidently manage the infrastructure, ensuring long-term success and self-sufficiency.
- Support. Benefit from a seamless communication over Slack with our experts, ensuring you have the support you need, whenever you need it.
- Troubleshooting. Access expert assistance to quickly resolve any operational challenges, minimizing downtime and maintaining business continuity.
- Code Reviews. Enhance your team’s code quality with our expert feedback, fostering continuous improvement and collaboration.
- Bug Fixes. Rely on our team to troubleshoot and resolve any issues, ensuring your systems run smoothly.
- Migration Assistance. Accelerate your migration process with our dedicated support, minimizing disruption and speeding up time-to-value.
- Customer Workshops. Engage with our team in weekly workshops, gaining insights and strategies to continuously improve and innovate.
This project is under active development, and we encourage contributions from our community.
Many thanks to our outstanding contributors:
For 🐛 bug reports & feature requests, please use the issue tracker.
In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
- Review our Code of Conduct and Contributor Guidelines.
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull Request so that we can review your changes
NOTE: Be sure to merge the latest changes from "upstream" before making a pull request!
Join our Open Source Community on Slack. It's FREE for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally sweet infrastructure.
Sign up for our newsletter and join 3,000+ DevOps engineers, CTOs, and founders who get insider access to the latest DevOps trends, so you can always stay in the know. Dropped straight into your Inbox every week — and usually a 5-minute read.
Join us every Wednesday via Zoom for your weekly dose of insider DevOps trends, AWS news and Terraform insights, all sourced from our SweetOps community, plus a live Q&A that you can’t find anywhere else. It's FREE for everyone!
Preamble to the Apache License, Version 2.0
Complete license is available in the LICENSE
file.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
All other trademarks referenced herein are the property of their respective owners.
Copyright © 2021-2022-2024 Cloud Posse, LLC