In part 1, I created a full-stack web app, added Atlas Mongodb, integrated Firebase for authentication, containerized the app using Docker, set up GitHub actions, and pushed it to Elastic Container Registry. Now, we will deploy the Docker images to Kubernetes (EKS) and expose them securely to the public using in Docker, ECR, Amazon Kubernetes (EKS) and an ingress LoadBalancer.


Prerequisites

  • Docker images pushed to ECR ( which we did in the first part)
  • An AWS EKS (Elastic Kubernetes Service) cluster - we need to create this one
  • kubectl installed locally in our laptop
  • aws CLI installed locally ( if you dont have one)
  • eksctl installed (helpful in setting up the EKS but its optional)

AWS CLI Installation

I am using Macbook so I will initialy install awscli

  1. Use brew install awscli
    • marygracezabala@Mac ~ % brew install awscli
      ==> Auto-updating Homebrew...
  2. Execute AWS Configure and add the AWS credentials you created with IAM role for development. This saves to ~/.aws/credentials locally.

  3. Restart your IDE (Visual Studion Code) - I need to restart my Vscode so it can see the credential files (part of my troubleshooting)

Additional IAM roles to be added for EKS

 

Permission PolicyWhy it's NeededAttach To
AmazonEKSClusterPolicyManage and interact with EKS clustersIAM User
AmazonEKSWorkerNodePolicyAllow nodes (EC2) to register to EKSIAM Role for EC2 (for worker nodes)
AmazonEKS_CNI_PolicyManage networking inside EKS (VPC CNI plugin)IAM Role for EC2 (for worker nodes)
AmazonEC2FullAccess (optional)Create/manage EC2 instances for EKSIAM User (for eksctl to create nodes)
   
   

Create EKS Cluster

  1. Create EKS Cluster Role.

    This is needed tp:

    • Create EKS cluster
    • Used by the Kubernetes Control Plane
    • Allows EKS to:
      • Create/manage networking
      • Attach load balancers
      • Call APIs like Auto Scaling, CloudWatch, etc.
    • You typically assign:
      • AmazonEKSClusterPolicy
      • VPC-related permissions if needed

    Edit Trust Policy of EKS Cluster Role

    Add additional permission to the role

    • AmazonEKSBlockStoragePolicy
    • AmazonEKSComputePolicy
    • AmazonEKSLoadBalancingPolicy
    • AmazonEKSNetworkingPolicy

 

2. Create EKS Node IAM role. 

For our app in the recent blog, React + Node.js + MongoDB app running in EKS. 

  • This is required when creating a Node Group (EC2 worker nodes)
  • The policy is Attached to each EC2 node
  • It Allows EC2 nodes to:
    • Register themselves with the EKS cluster
    • Pull container images from ECR
    • Use VPC CNI plugin (networking)
    • Log to CloudWatch (optional)
  • You must assign:
    • AmazonEKSWorkerNodePolicy - Allows the node to join EKS cluster
    • AmazonEC2ContainerRegistryReadOnly - Allows pulling container images from ECR
    • AmazonEKS_CNI_Policy - Enables VPC networking
    • CloudWatchAgentServerPolicy - to enable logging

Steps to Crear Node IAM Role

  1. Go to IAM > Roles > Create role
  2. Choose "AWS service" ➝ EC2

  3. Attach these policies:
    • AmazonEKSWorkerNodePolicy
    • AmazonEKS_CNI_Policy
    • AmazonEC2ContainerRegistryReadOnly
    • CloudWatchAgentServerPolicy
  4. I named it  eks-node-role

  5. Select this role when creating the node group

Go to EKS in AWS. Then click create cluster. Create Kubernetes CLuster in AWS

  1. I  named it eks-cluster-react-fullstack.Assign the Cluster IAM Role and the Node IAM Role

 

Configure the kubernetes Cluster

  1. Configure kubectl to Use EKS Cluster

    marygracezabala@Mac back-end % aws eks update-kubeconfig --region us-east-1 --name eks-
    cluster-react-fullstack
    
    Cluster status is CREATING
    marygracezabala@Mac back-end % 
  2. Wait for the cluster to finish

  3. Execute these commands again

    marygracezabala@Mac back-end % aws eks update-kubeconfig --region us-east-1 --name eks-cluster-react-fullstack
    Added new context arn:aws:eks:us-east-1:443370678519:cluster/eks-cluster-react-fullstack to /Users/marygracezabala/.kube/config
    marygracezabala@Mac back-end % kubectl get nodes
    No resources found
    marygracezabala@Mac back-end % 

Secure App with AWS Secrets Manager + IRSA

Now we’ll fetch secrets from AWS Secrets Manager using IAM Roles for Service Accounts (IRSA).

  1. Create secrets in AWS Secrets Manager (e.g., Firebase credentials, MongoDB username/password).
    • Please see the link on how to create secrets in AWS secrets manager
  2. Update Node IAM Role (eks-node-role-full-stack) to allow secrets access:
  3. {
     "Effect": "Allow",
     "Action": ["secretsmanager:GetSecretValue"],
     "Resource": "arn:aws:secretsmanager:REGION:ACCOUNT_ID:secret:react-blog-secrets-*"
    }
  4. Install the aws-sdk module:

    marygracezabala@Mac React Full Stack % npm install @aws-sdk/client-secrets-manager
    
    added 77 packages, and audited 90 packages in 4s
    
    3 packages are looking for funding
      run `npm fund` for details
    
    found 0 vulnerabilities
  5. To allow the application pods to securely access AWS Secrets Manager (instead of using .env files or hardcoded credentials), I must create a Kubernetes service account mapped to an IAM role via IAM Roles for Service Accounts (IRSA).
    • Check the OIDC Provider is Associated with the EKS cluster(eks-cluster-react-fullstack) we created previously

    • Create an IAM Policy for AWS Secrets Manager Access

    • Create an IAM Role for the Kubernetes Service Account
      • In the IAM Console, click Create role.
      • Select Web identity as the trusted entity type.

        As you noticed above, I chose the OIDC Identity and addedsts.amazonaws.com as audience. To created oidc identity provider, follow these steps:

        • Choose your EKS cluster's OIDC provider from the dropdown.

          This is the OIDC Information obtained from EKS cluster console

           

        • For Audience, enter sts.amazonaws.com.

        • Click Next. Now you already how OIDC as Identity provider

      • After choosing web identity and added oid and audience,we can now continue creating the role. Click next and attach the SecretsManagerReadAccess policy you created earlier.

        Please see the image below

      • Next create the role, I named the role as EKSSecretsAccessRole. 

      • Take note of the arn of the EKSSecretsAccessRole as we will associate it with the Kubernetes service account that we will creat

  6. Create a Kubernetes Service Account and Associate the IAM Role
    • Since the AWS Console doesn't support creating Kubernetes service accounts directly, you'll need to use kubectl:
    • Ensure your kubectl is configured to interact with your EKS cluster.
    • Create a file named service-account.yaml with the following content (replace placeholders accordingly):

      First, create a yaml configuration in the directory

      react-fullstack-app/
      ├── back-end/
      │   └── ...
      ├── front-end/
      │   └── ...
      ├── k8s/
      │   └── service-account.yaml   ← create this one to create service-account for our kubernetes cluster
      ├── Dockerfile
      ├── .dockerignore
      ├── .gitignore
      └── README.md

      Now in my local macbook, I will create k8s directory, and from the k8s directory, I will create service-account.yaml 

      marygracezabala@Mac React Full Stack % mkdir k8s
      marygracezabala@Mac React Full Stack % cd k8s 
      marygracezabala@Mac k8s % vi service-account.yaml

      service-account.yaml

      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: secrets-access-sa
        namespace: default
        annotations:
          eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/EKSSecretsAccessRole
    • Apply the configuration:

      marygracezabala@Mac k8s % kubectl apply -f service-account.yaml
      serviceaccount/secrets-access-sa created

      This service account is now associated with the IAM role, allowing pods that use it to access AWS Secrets Manager securely.

  7. Modify /src/server.ts to fetch secrets at runtime using IRSA:

    import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
    
    const secretsManager = new SecretsManagerClient({ region: process.env.AWS_REGION });
    
    const secret = await secretsManager.send(
      new GetSecretValueCommand({ SecretId: 'react-blog-secrets' })
    );
    
    const credentials = JSON.parse(secret.SecretString || '{}');
    
  8. Update Dockerfile and Build Again

Make sure that you set AWS Region in the .env variable

Reconfigure and rebuild my docker image after modifying the secret-fetching login in the server.ts

This blog is still ongoing and I will update other details soon.