<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Victor Chugunov on Medium]]></title>
        <description><![CDATA[Stories by Victor Chugunov on Medium]]></description>
        <link>https://medium.com/@vichugunov?source=rss-f2631f6f367d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SaplYD7xxhz__XxH.</url>
            <title>Stories by Victor Chugunov on Medium</title>
            <link>https://medium.com/@vichugunov?source=rss-f2631f6f367d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:02:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vichugunov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mastering Helm Chart Configuration and EKS Pod Identity]]></title>
            <link>https://medium.com/datareply/mastering-helm-chart-configuration-and-eks-pod-identity-38185ac98836?source=rss-f2631f6f367d------2</link>
            <guid isPermaLink="false">https://medium.com/p/38185ac98836</guid>
            <category><![CDATA[helm]]></category>
            <category><![CDATA[docker-in-docker]]></category>
            <category><![CDATA[pod-identity]]></category>
            <category><![CDATA[amazon-eks]]></category>
            <dc:creator><![CDATA[Victor Chugunov]]></dc:creator>
            <pubDate>Mon, 21 Oct 2024 09:25:49 GMT</pubDate>
            <atom:updated>2024-10-21T11:12:41.031Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem statement</h3><p>Let’s imagine you are working on your helm charts and are setting up manually (e.g. via rest API exposed) pods after all resources within these helm charts are deployed. These settings are bound to the custom scenario you want to reflect. Afterward, you realize you want to fine-tune helm chart values since workload (e.g. number of replicas deployed) can also differ from use case to use case. Moreover, since you have the intention to adhere principle of least privilege and grant only necessary permissions you want to grant each helm chart its own AWS permissions. And, last but not least, you think to reuse as much as you can and do not explode on helm charts amount. So, how would you do that?</p><p>For legibility, each section will represent an approach to tackle one problem at a time, and at the end, all pieces of the puzzle will eventually fall into place.</p><blockquote><strong>TL;DR </strong>k8s jobs use docker-in-docker image to run scripts (embedded in docker images) to control other pods. AWS permissions are granted via EKS pod identity, k8s permissions are controlled via k8s roles, bound to the service account. Since EKS pod identity has to be created before pods where it’s applied to, helm charts have to be deployed in the fixed order. The order and chart values are reflected in the yaml file that is read in the pipeline.</blockquote><h3>Granting (AWS and k8s) permissions to pods</h3><p>Historically, kubernetes (k8s) uses service accounts (sa) as its internal identity system. So that if the pod has the desire to talk to k8s control plane to make an action, k8s Role is defined and attached via RoleBinding to the service account that is used in the pod definition. This RBAC authorization is well covered in the k8s docs: <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/">using rbac authorization</a></p><p>Previously, AWS Identity and Access Management (IAM) had only one way to attach an AWS Role to the service account via support for federated identities using OpenID Connect (OIDC). This allowed developers to obtain a valid OIDC JWT token and exchange (manually or via SDK) it for temporary IAM credentials by passing this token to AWS STS <em>AssumeRoleWithWebIdentity</em> API operation. This is known as IAM roles for service accounts (IRSA) and explained in detail in AWS docs: <a href="https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html">IAM Roles for service accounts</a></p><p>However, this approach contains one hidden caveat: since k8s service account is bound to the namespace and AWS Role trust policy is bound to the service account directly, you’d be forced to create a new role each time you want to deploy your beloved helm charts to the new namespace. That also means, you no longer can make changes in one place if want to alter aws permissions for all your applications that live in separate namespaces and you have to alter all existing roles and role “template” for the upcoming deployments. Sounds not so fancy, right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*X4S3J-_qrrmV5QeXsnV0QA.png" /><figcaption>Using IRSA, the AWS Role is bound to the namespaced k8s service account. In the trust policy of each IAM role, the action sts:AssumeRoleWithWebIdentity is granted to the specific service account.</figcaption></figure><p>In 2023 at <a href="https://aws.amazon.com/blogs/aws/top-announcements-of-aws-reinvent-2023/">AWS reInvent</a>, Amazon EKS Pod Identity was <a href="https://aws.amazon.com/blogs/containers/amazon-eks-pod-identity-a-new-way-for-applications-on-eks-to-obtain-iam-credentials/">introduced</a>, making the process of obtaining IAM permissions simpler without the setup of an OIDC identity provider. Instead, Pod Identity Agent is deployed as an EKS Addon to the EKS Cluster. It manages credentials to assume roles for applications in the pods that use known (listed in pod identity associations) service accounts. Each EKS Pod Identity association maps a role to a service account in a namespace. This allows a single IAM role to be used in the same applications (even in multiple clusters) in identical associations without modifying the trust policy of the role. That means you can define and modify the role once and maintain it via the IaC of your choice.</p><p>If a pod uses a service account that has an association, Amazon EKS Pod identity Agent sets environment variables in the containers of the pod. The environment variables configure the AWS SDKs, including the AWS CLI, to use the EKS Pod Identity credentials. That makes the process of obtaining temporary IAM credentials easier and lessens the effort of the developer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VvVaOOK9QnEizXIU_b83SA.png" /><figcaption>EKS pod identity flow differs from IRSA: a single IAM role could be mentioned in several pod identity associations, each containing a mapping between service account, namespace, where it is deployed, and the iam role arn. Trust policy in the role refers to the principal pods.eks.amazonaws.com instead.</figcaption></figure><h3>Control pods inside the pod</h3><p>A need to migrate configuration scripts (that were run only locally) led us to consider using docker-in-docker image for the pod container within our generic configuration job chart. This idea assumes creating a docker image with all scripts inside and all required dependencies and storing this in AWS Elastic Container Registry (ECR). This road was hit to make the least amount of changes to the application itself and migrate the complex application setup inside of the pod, which is almost as-is. However, some hidden traps were awaiting us. Let’s embark on this journey and uncover them.</p><p>Having the service account set with necessary roles for querying k8s API and having the EKS pod identity agent with the necessary pod identity association for this service account and an AWS role to assume, we can do what we need with the k8s cluster and AWS roles in the pod itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*I3ceAlO85PEBn4ZtVFrXxQ.png" /><figcaption>Two types of credentials: credentials to access k8s API from the service account and temporary IAM credentials from EKS Pod Identity Agent.</figcaption></figure><p>However, to carry over these permissions further and propagate them to the running container that is created from the docker image pulled from ECR within the pod (docker-in-docker, remember?) is an error-prone and not easy exercise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*Atzk2JxQ064pqCYEgSD5WQ.png" /><figcaption>Running a script in docker-in-docker within the k8s pod requires a bit of heavy lifting: you need to provide temporary iam credentials and service account token to the spawned docker container so that the script could have access as it is in the pod itself</figcaption></figure><p>Using k8s service account, pods can authenticate with the Kubernetes API server with a token that only Kubernetes API server can validate. This legacy sa token is a jwt token that doesn’t expire (rotation of the signing key is difficult) and has a custom audience. To obtain temporary AWS credentials, it’s necessary to claim them from the EKS Pod identity Agent. We need to provide these both as environment variables to the spawn docker container so that it can proceed with running scripts with the required permissions.</p><p>To make it easier, the same terminal session can be used during the setup and run script process, and the scripts itself can be saved in the ConfigMap</p><p>job.yaml</p><pre>apiVersion: batch/v1<br>kind: Job<br>metadata:<br>  name: startupJob<br>spec:<br>  template:<br>    spec:<br>      serviceAccountName: {{ .Values.serviceAccountName }}<br>      containers:<br>      - name: main<br>        image: docker:27.2.0-dind-alpine3.20<br>        command:<br>          -           sh<br>          -           /scripts/post-install.sh<br>        securityContext:<br>          privileged: true<br>        volumeMounts:<br>        - name: scripts<br>          mountPath: /scripts<br>        - name: dind-storage<br>          mountPath: /var/lib/docker<br>        env:<br>          - name: DOCKER_TLS_CERTDIR<br>            value: &quot;&quot;<br>      restartPolicy: OnFailure<br>      volumes:<br>        - name: scripts<br>          configMap:<br>            name: scriptsConfigMap<br>            items:<br>            - key: post-install-script<br>              path: post-install.sh<br>            - key: run-script<br>              path: run-script.sh<br>        - name: dind-storage<br>          emptyDir: {}</pre><pre>{{- define &quot;post-install&quot; }}<br>#!/bin/sh<br>apk update<br>apk add --no-cache bash curl unzip less aws-cli &lt;other depedencies your need to install&gt;<br><br># install kubectl<br>curl -LO https://dl.k8s.io/release/v1.28.12/bin/linux/amd64/kubectl<br>install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl<br><br>export AWS_REGION=&lt;your-aws-region&gt;<br>ACCOUNT_ID=&lt;your account id&gt;<br><br># login to the ecr<br>docker login -u AWS -p $(aws ecr get-login-password --region $AWS_REGION) $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com<br><br># obtain temporary IAM credentials<br>TOKEN=$(cat $AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE)<br><br>curl $AWS_CONTAINER_CREDENTIALS_FULL_URI -H &quot;Authorization: $TOKEN&quot; &gt; creds.json<br>export AWS_ACCESS_KEY_ID=$(jq -r &#39;.AccessKeyId&#39; creds.json)<br>export AWS_SECRET_ACCESS_KEY=$(jq -r &#39;.SecretAccessKey&#39; creds.json)<br>export AWS_SESSION_TOKEN=$(jq -r &#39;.Token&#39; creds.json)<br><br># obtain k8s tokens<br>SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount<br>CACERT_FILE=${SERVICEACCOUNT}/ca.crt<br>export SA_TOKEN=$(cat ${SERVICEACCOUNT}/token)<br>export CERT_BASE64=$(cat $(echo $CACERT_FILE)|base64 -w0 -)<br><br>sh /scripts/run-script.sh<br>exit_code=$?<br><br>echo &quot;Exit code from run-script: $exit_code&quot;<br>exit $exit_code<br><br>{{- end }}</pre><pre>{{- define &quot;run-script&quot; }}<br><br>{{ .Values.job.runScript }}<br><br>{{- end }}</pre><pre>apiVersion: v1<br>kind: ConfigMap<br>metadata:<br>  name: scriptsConfigMap<br>data:<br>  post-install-script: |<br>    {{- include &quot;post-install&quot; . | indent 4 }}<br>  run-script: |<br>    {{- include &quot;run-script&quot; . | indent 4 }<br></pre><p>And then when you want to run your script, you can just use all the defined environment variables and keep your script runtime-agnostic.</p><p>values.yaml</p><pre>job:<br>  runScript: |-<br>     docker run \<br>            -e AWS_ACCESS_KEY_ID \<br>            -e AWS_SECRET_ACCESS_KEY \<br>            -e AWS_SESSION_TOKEN \<br>            -e NAMESPACE \<br>            -e SA_TOKEN \<br>            -e AWS_REGION \<br>            -v /var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount \<br>            &lt;your ecr image&gt; &lt;commandline params<br></pre><h3>AWS ACK: create pod identity association</h3><p>However, the EKS pod identity association has to be set up before the pods it attaches to. As of Oct 2024, the issue is still not resolved: <a href="https://github.com/aws/containers-roadmap/issues/2400">github issue</a></p><p>To overcome this, we create EKS pod identity association and linked service account in a separate helm chart.</p><p><strong>Note:</strong> As AWS docs <a href="https://docs.aws.amazon.com/eks/latest/userguide/pod-id-association.html#pod-id-association-create">report</a>, you can create EKS pod identity association and specify a service account name that doesn’t (yet) exist in the cluster. However, this behavior is still flaky and we recommend creating the service account in the same chart. As for us, we enrich the service account with necessary k8s roles in another job chart, referencing it by name. That helps to separate responsibilities and pertain to the required deployment order.</p><p>To focus only on helm charts and create only required resources, we incorporate AWS Controllers for Kubernetes (ACK), separate k8s controllers deployed to the kubernetes control plane that watch for the custom resource definitions in the cluster and create/remove AWS resources accordingly. To read more about it, you can head to the nice overview from the AWS about this here: <a href="https://aws.amazon.com/blogs/containers/aws-controllers-for-kubernetes-ack/">AWS Controllers for Kubernetes announcement</a></p><p>There are many ways to deploy these ACK controllers: <a href="https://aws-controllers-k8s.github.io/community/docs/user-docs/install/">ACK deployment options</a>. The easiest way is to supply helm charts to the control plane. We assume that you have this installed and proceed.</p><p>We need to have a controller for EKS definitions installed and use it to create EKS pod identity association: <a href="https://aws-controllers-k8s.github.io/community/reference/eks/v1alpha1/podidentityassociation/">EKS pod identity association resource definition</a></p><pre>apiVersion: eks.services.k8s.aws/v1alpha1<br>kind: PodIdentityAssociation<br>metadata:<br>  name: pod-association<br>spec:<br>  roleARN: {{ .Values.roleArn }}<br>  serviceAccount: {{ .Values.serviceAccountName }}<br>  clusterName: &lt;your cluster name&gt;<br>  namespace: {{ $.Release.Namespace }}</pre><pre>apiVersion: v1<br>kind: ServiceAccount<br>metadata:<br>  name: {{ .Values.serviceAccountName }}<br>automountServiceAccountToken: true</pre><p><strong>Note</strong> Since we use a service account to query the k8s control plane, we have the option <em>automountServiceAccountToken</em> enabled. To read more on this, you may head to the official k8s docs: <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#opt-out-of-api-credential-automounting">opt-out for service account credentials automapping</a></p><h3>Deployment</h3><p>Since the order of the helm chart matters and since we want to observe all available scenarios and their settings, we decided to use yaml file with the following shape:</p><pre>scenario1:<br>  - permissions:<br>      version: x.x.x<br>      values:<br>        serviceAccountName: serviceAccountName<br>        roleArn: &lt;role arn&gt;<br>  - job:<br>      version: x.x.x<br>      values:<br>         serviceAccountName: serviceAccountName<br>         runScript: |-<br>           docker run \<br>                  -e AWS_ACCESS_KEY_ID \<br>                  -e AWS_SECRET_ACCESS_KEY \<br>                  -e AWS_SESSION_TOKEN \<br>                  -e NAMESPACE \<br>                  -e SA_TOKEN \<br>                  -e AWS_REGION \<br>                  -v /var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount \<br>                  &lt;your ecr image&gt; &lt;commandline params</pre><p>The target user selects the dedicated scenario to deploy by its name, and charts are deployed one by one in the order they appear.</p><p><strong>Note: </strong>The chart entries are mentioned within an array so that the same chart name can be deployed with different settings.</p><h3>Conclusion</h3><p>If you have a complex setup and you are short on time to achieve it when migrating it to the cloud, you may use this approach to configure your pods after you deploy the required resources within your helm charts.</p><p>If you find these challenges exciting and are keen to learn, Data Reply has a wide range of opportunities available! <a href="https://www.reply.com/de/about/careers/de/job-search?country=de&amp;company=data_reply">Join us</a>, and let’s create something extraordinary together.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38185ac98836" width="1" height="1" alt=""><hr><p><a href="https://medium.com/datareply/mastering-helm-chart-configuration-and-eks-pod-identity-38185ac98836">Mastering Helm Chart Configuration and EKS Pod Identity</a> was originally published in <a href="https://medium.com/datareply">DataReply</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>