Hello! Welcome to the next lesson in our journey to build a production-grade blog platform.
In our last session, you successfully built a full Continuous Deployment (CD) pipeline. Your GitHub Actions workflow can now automatically trigger a zero-downtime rolling update on your EKS cluster whenever new code is merged. This completes the core automation loop from code to production.
Today's lesson addresses a critical, non-functional requirement that underpins this entire process: security. We'll take a deep dive into how credentials for AWS and Kubernetes are managed within GitHub Actions, exploring why the approach we've taken is the modern industry standard.
The learning outcome for this lesson is to: Manage AWS and Kubernetes credentials securely within GitHub Actions using secrets.
We will contrast the modern, secure approach using OpenID Connect (OIDC) with the older, riskier method of using long-lived access keys.
MERN Stack Analogy
In the MERN world, you're used to managing secrets like database URIs or third-party API keys. A common approach is to use .env files for local development and then add those same key-value pairs to the "environment variables" section of a hosting provider like Vercel or Heroku. While this works, it often involves storing static, long-lived credentials. If one of these secrets leaks, it remains valid until you manually revoke and replace it. Today, we'll explore a more dynamic and secure alternative that's standard practice in cloud-native environments.
Two Approaches to Credential Management
There are two primary ways to grant a CI/CD system like GitHub Actions access to your AWS account.
1. The Traditional (but risky) Way: Long-Lived IAM User Keys
The original method was to:
- Create a dedicated IAM User in AWS.
- Generate a long-lived Access Key ID and Secret Access Key for that user.
- Store these two powerful secrets (
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY) in your GitHub repository's "Secrets" section. - The GitHub Actions workflow would then use these keys to authenticate its API calls to AWS.
Deploying applications to AWS EKS using GitHub actions
To see what this traditional approach looks like in practice, watch this segment from the video 'Deploying applications to AWS EKS using GitHub actions'. It demonstrates storing static AWS keys as repository secrets.
Watch from 38:02 to 40:46 to see how AWS keys are added as secrets in the GitHub UI. Then, skim through 15:51 to 16:58 to see how the workflow YAML references these secrets. This gives you a clear picture of the static key pattern.
This pattern is simple to understand but carries significant security risks:
- Exposure Risk: If these keys are ever accidentally logged or exposed, an attacker has long-term, persistent access to your AWS account with the full permissions of that IAM user.
- Rotation Burden: Security best practices demand regular key rotation, which is a manual and often forgotten process.
- Broad Access: These keys provide access from anywhere, not just from your GitHub Actions runner.
For these reasons, this method is no longer recommended for production systems.
2. The Modern, Secure Way: OpenID Connect (OIDC)
This is the approach we've already implemented, and now we'll formalize why it's superior. Instead of storing a powerful secret, we establish a trust relationship between GitHub and AWS.
The flow works like this:

Let's break down this superior method.
Your GitHub Actions Secrets Are the Weakest Link in Your AWS Security Chain
The article 'Your GitHub Actions Secrets Are the Weakest Link...' provides an excellent summary of why OIDC is the preferred method and walks through the setup.
Read the introduction and the sections 'Then what should you do?' and 'What the heck is OIDC?'. This will reinforce the security benefits and give you a high-level overview of the four main setup steps.
Key Steps in the OIDC Flow:
- Configure Trust in AWS: You tell AWS to trust GitHub as an identity provider. This is a one-time setup where you add GitHub's OIDC provider (
token.actions.githubusercontent.com) to AWS IAM. - Create an IAM Role with a Trust Policy: You create an IAM role that your GitHub Actions will "assume". The critical part is its Trust Policy. This policy specifies exactly which GitHub repositories, branches, or environments are allowed to assume this role.
- Request a Token in GitHub Actions: Your workflow requests a special JWT from GitHub by including
permissions: id-token: write. This token contains claims about the workflow run, such as the repository name (repo) and branch (ref). - Exchange the Token for AWS Credentials: The
aws-actions/configure-aws-credentialsaction takes the JWT and calls the AWS Security Token Service (STS), effectively saying, "Here is a token from GitHub. Please give me temporary credentials for the role I'm allowed to assume." - Use Temporary Credentials: AWS STS validates the token against the IAM role's trust policy. If everything matches, it grants short-lived AWS credentials (valid for about an hour) that the rest of your workflow can use.
The only "secret" you store in GitHub is the ARN of the IAM role (arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME). This is just an identifier, not a credential itself. Leaking the ARN is not a security risk.
Connecting to Kubernetes Credentials
You might be wondering: where are the Kubernetes credentials?
In our EKS setup, Kubernetes authentication is handled by AWS IAM. When your workflow runs the aws eks update-kubeconfig command, it uses the temporary AWS credentials obtained via OIDC to generate a valid kubeconfig file for that specific session. This kubeconfig contains a token that the EKS API server can validate with IAM.
Therefore, by securing our AWS access with OIDC, we automatically secure our Kubernetes access. We don't need to generate and store a static kubeconfig file or a service account token in GitHub secrets, which would be another major security risk.
Your Task: Hardening the IAM Trust Policy
Your current pipeline works because the trust policy on your IAM role likely allows any branch from your repository to assume the role. Let's practice the principle of least privilege by hardening this policy.
Your task is to modify the IAM Role's trust policy to only allow GitHub Actions running from the main branch to assume it.
-
Navigate to AWS IAM: Go to the IAM console, click on "Roles," and find the role your pipeline is using (e.g.,
GitHubActionsEKSRole). -
Find the Trust Policy: Click on the "Trust relationships" tab and then "Edit trust policy".
-
Analyze the
Condition: You will see aConditionblock with aStringLikeclause. It probably looks something like this:"Condition": { "StringLike": { "token.actions.githubusercontent.com:sub": "repo:YourOrg/YourRepo:*" } }The
*wildcard allows any branch, tag, or environment. -
Modify the Policy: You need to make this more specific. Change
StringLiketoStringEqualsand update the value to target only themainbranch.
Configuring OpenID Connect in Amazon Web Services - GitHub Docs
The official GitHub documentation provides the exact syntax for scoping the OIDC trust policy to specific branches or environments.
Read the section 'Configuring the role and trust policy'. Pay close attention to the first JSON example, which shows how to use StringEquals and the sub field to lock the policy down to a specific branch: repo:octo-org/octo-repo:ref:refs/heads/octo-branch.
Using the syntax from the documentation, update your policy to only permit the main branch. It should now look like this (replace with your org and repo name):
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:YourOrg/YourRepo:ref:refs/heads/main"
}
}
Save the updated policy.
- Verify the Fix (and the Failure):
- Create a new branch from
main:git checkout -b test-oidc-policy. - Make a trivial change (e.g., add a comment in a file).
- Push the new branch:
git push --set-upstream origin test-oidc-policy. - Observe the failure: If your workflow is configured to run on pushes to all branches, it will trigger and fail at the
Configure AWS Credentialsstep. Check the logs; you'll see an error indicating it's "not authorized to perform sts:AssumeRoleWithWebIdentity". This is your hardened policy working correctly! - Verify success: Now, merge this branch into
main. The workflow will run again, and this time it will succeed because the condition in your trust policy is met.
- Create a new branch from
This exercise provides direct, hands-on experience with how the IAM trust boundary works and how to enforce security policies in your CI/CD pipeline.
Conclusion
Congratulations on completing this deep dive into CI/CD security! You now understand not just how to manage credentials, but why the modern OIDC-based approach is vastly more secure than storing static keys.
Key Takeaways:
- Storing long-lived credentials like
AWS_ACCESS_KEY_IDin GitHub secrets is a significant security risk and is not a recommended practice. - OpenID Connect (OIDC) is the industry standard for secure, "secretless" authentication between platforms like GitHub Actions and cloud providers like AWS.
- OIDC works by establishing a trust relationship and exchanging a short-lived JWT for temporary cloud credentials.
- The IAM Role Trust Policy is the enforcement point, where you can define granular rules about which repositories, branches, or environments are allowed to access your cloud resources.
- For EKS, securing AWS IAM access effectively secures Kubernetes access, as EKS uses IAM for authentication.
Preview of the Next Lesson:
Our CI/CD pipeline is now secure and fully functional. In the next lesson, we'll refine it further by looking at another production-grade practice. You will learn to implement a basic versioning strategy for Docker images using Git commit hashes, moving away from a simple latest tag and ensuring every deployment is traceable to a specific commit.