Kubernetes · Freelance

CloudFormation EKS Platform with Per-Service CI/CD

CloudFormation builds a VPC, EKS node group and one CodePipeline per .NET microservice that builds, pushes and deploys with kubectl.

  • EKS
  • CodePipeline
  • CodeBuild
  • ECR
  • CloudFormation
  • IAM
  • ALB Ingress
  • Docker
  • .NET

// Why this matters

Setting up EKS CI/CD with CodePipeline and CodeBuild for .NET microservices is a recurring need: a handful of services, one cluster, and a team that wants push-to-deploy without adopting a whole GitOps stack. The pattern here is deliberately small. It uses native AWS services, needs no in-cluster agent, and each service gets its own pipeline from one reusable template.

The part that deserves care is the last mile. The build must run kubectl against the cluster, which means credentials. The wrong answer is a long-lived access key in a build variable; the right answer is a role assumption with a short session and a narrowly mapped Kubernetes identity, so the pipeline can deploy and nothing else.

Per-service pipelines also keep blast radius small. A broken build for one service does not block releases of the others, and permissions can be tuned per repository.

// The problem

A multi-service .NET application had to move onto Kubernetes on AWS with automated container build and deployment. Developers were building images on laptops and applying manifests by hand, which produced untraceable images (the tag latest everywhere), drift between what was in git and what was running, and credentials with far more reach than a deploy needs.

There are a few specific traps. EKS authenticates through IAM, but authorises through Kubernetes RBAC, so an IAM role that can call the cluster API still gets Unauthorized until it is mapped in the cluster. Node groups that cannot join because the node role is missing from the auth mapping fail silently. And if every service copy-pastes a pipeline, every fix has to be made N times.

The goal was a push to a branch producing a rolling deployment of exactly that commit, with one pipeline definition parameterised per service.

// The solution

We separated infrastructure into stacks with clear ownership. The VPC stack provides subnets and a security group. The node group stack provides an auto scaling group, launch configuration (use launch templates today) and the node instance role. The pipeline stack is the reusable part: parameters name the repository and service, and the template creates an ECR repository, a CodeBuild project, a pipeline role and a pipeline with source, build and deploy behaviour.

The build does everything in one CodeBuild job: compute the tag, render it into the deployment manifest, build and push the image, then assume the kubectl role, produce a kubeconfig and run kubectl apply. A single job keeps the credentials inside one short-lived process and avoids an extra approval hop for a small team. If you need a manual gate, split deploy into its own action and add an approval stage before it.

Access to the cluster is by role mapping. A dedicated deployment role is trusted by the CodeBuild role, has only eks:DescribeCluster and nothing else on AWS, and is mapped in the cluster to a group that is bound to a namespace-scoped Role, not cluster-admin. Newer clusters should use EKS access entries rather than editing the aws-auth ConfigMap, but the idea is identical.

The ingress controller is installed with an IAM role for its service account (IRSA): an OIDC provider on the cluster, a trust policy limited to one service account subject, and the controller policy attached. This keeps cloud permissions off the node role, which every pod on the node could otherwise inherit.

How it flows flow1. VPC stack → 2. EKS node group → 3. ECR → 4. Source → 5. Build → 6. Push → 7. Deploy → 8. Ingress01VPC stack02EKS node group03ECR04Source05Build06Push07 · GATEDeploy08Ingress
How it flows. End-to-end flow. Dashed pink nodes are gates: points where a human or a check must pass before the next stage runs.
  1. VPC stacksubnets, routes, security group
  2. EKS node groupASG, launch configuration, node role
  3. ECRone repository per service
  4. SourceGitHub stage in each pipeline
  5. BuildCodeBuild builds a uniquely tagged image
  6. Pushimage pushed to ECR
  7. Deploy gateassume a short-lived kubectl role, update kubeconfig, apply manifests
  8. IngressALB controller with cluster role and auth mapping
DeveloperPipelineCodeBuildECRSTSEKSpush to branchstart build with sourcepush image tagged with commitassume kubectl role, 900 secondstemporary credentialskubectl apply, rolling update
One deployment, end to end. Credentials exist only inside the build and expire in minutes.
Network stackVPC and subnetsRoute tablesCluster security groupCompute stackNode group ASGLaunch configurationNode instance roleAccessKubectl deploy roleaws-auth mappingNamespace RBACPer-service pipeline stackECR repositoryCodeBuild projectCodePipelineCluster add-onsALB ingress controllerIRSA role
Stack layout. Each stack can be updated without touching the others.

// Before you start

  • AWS CLI v2, kubectl and eksctl installed; an account with permission to create VPCs, EKS clusters, IAM roles and CodePipeline resources.
  • A GitHub connection (CodeStar connection) or CodeCommit repositories for each service; a Dockerfile and buildspec at the repository root.
  • An EKS cluster (managed by your own template or eksctl) whose name you can pass as a parameter.
  • Basic Kubernetes knowledge: deployments, services, RBAC roles and role bindings.
  • A .NET service with a working multi-stage Dockerfile.

// Step-by-step implementation

  1. 01 Create the IAM role that kubectl will assume

    This role is the only door into the cluster for pipelines. Its trust policy lets the CodeBuild role (and nothing else) assume it. Its permissions are just eks:DescribeCluster, which is what aws eks update-kubeconfig needs. All real authority comes from the Kubernetes side, which is exactly where you want it to be auditable.

  2. 02 Map the role into the cluster with least-privilege RBAC

    Map the role to a Kubernetes group, then bind that group to a Role in the application namespace with just the verbs a deploy needs. cluster-admin is the lazy answer and the one that bites later. If you are on a recent cluster, create an access entry with an access policy scoped to a namespace instead of editing the ConfigMap.

    Be careful with aws-auth: it also holds the node role mapping, and a typo in the whole ConfigMap can stop nodes joining. Edit with eksctl or apply a complete, reviewed file.

  3. 03 Write the buildspec: tag, build, push, assume, deploy

    The tag combines repository, branch, timestamp and the first eight characters of the commit, so the image tells you where it came from without consulting the pipeline. CodeBuild substitutes the tag into the manifest with sed before apply. Credentials from assume-role are exported only for the final commands.

    Note the build needs privileged mode to run Docker; set it on the project. Use the aws ecr get-login-password form (the older get-login command was removed in CLI v2).

  4. 04 Template the per-service pipeline stack

    The pipeline stack takes the repository and service name as parameters and creates an ECR repository with a lifecycle rule, a CodeBuild project with the environment variables the buildspec expects, and the pipeline itself. Because it is one template, onboarding service number five is a single stack creation with different parameters.

    Use a CodeStar connection for GitHub rather than a personal access token; it removes a long-lived credential from the stack parameters entirely.

  5. 05 Write the Kubernetes manifests the build applies

    The deployment carries a placeholder image that the build replaces. Set resource requests and limits (a .NET service without a memory limit will happily grow until the node evicts something), a readiness probe so the rolling update only shifts traffic to healthy pods, and a rolling strategy that never drops below full capacity.

  6. 06 Install the ALB ingress controller with an IAM role for its service account

    Associate an OIDC provider with the cluster, create a role whose trust policy accepts only the controller service account as the subject, attach the published controller policy, and annotate the service account. With that in place the controller can manage load balancers and the nodes carry no extra permissions.

    Then one Ingress per service (or one shared with path rules) creates and configures the ALB.

  7. 07 Deploy the stacks in order and release a service

    Order matters: network, then node group, then the pipeline-shared roles, then the aws-auth mapping (which needs the node role and the kubectl role), then one pipeline stack per service. After that, a release is simply a push.

// How to verify it works

  • kubectl get nodes shows every node Ready, proving the node role mapping is correct.
  • Push a trivial change; the pipeline goes green and kubectl -n apps get deploy web-api -o jsonpath="{.spec.template.spec.containers[0].image}" shows a tag containing the new commit prefix.
  • kubectl -n apps rollout history deployment/web-api records a new revision for the push.
  • From the assumed kubectl role, kubectl auth can-i delete nodes returns no and kubectl auth can-i create deployments -n apps returns yes.
  • Break the Dockerfile on purpose: the build fails before any apply, leaving the running version untouched.

// Design decisions and trade-offs

  • A dedicated short-lived (900s) role assumption instead of embedded cluster credentials.
  • Image tag encodes repo, branch, time and commit for traceability.
  • One pipeline per microservice from the same template.
  • Rolling updates and resource limits set in manifests.

// Pitfalls and lessons learned

  • IAM access to the cluster API is not authorisation: without the aws-auth (or access entry) mapping you get Unauthorized and no useful message.
  • Overwriting aws-auth without the node role mapping drops nodes from the cluster. Always apply the full file.
  • Forgetting PrivilegedMode on the CodeBuild project makes docker build fail with a daemon connection error.
  • The long-removed ecr get-login command breaks builds on current CLI versions; use get-login-password.
  • Using the latest tag defeats rollbacks and traceability. Tag by commit.
  • Without a readiness probe, a rolling update shifts traffic to pods that are still starting.
  • A GitHub personal token in a stack parameter is a leak waiting to happen; use a connection.

// Frequently asked questions

How do I deploy to EKS from CodeBuild without storing cluster credentials?

Give CodeBuild permission only to assume a dedicated role, map that role in the cluster to a namespace-scoped RBAC group, and assume it for a few minutes inside the build.

Should I use aws-auth or EKS access entries?

Use access entries on current clusters; they are managed through the API and avoid hand-edited ConfigMaps. The role-to-group mapping idea is the same.

Why one pipeline per microservice?

Independent release cadence, independent failures and per-repository permissions, all from one reusable template.

How do I roll back a bad deployment?

Run kubectl rollout undo, or re-run the pipeline on the previous commit. Commit-based image tags make both safe.

Is this GitOps?

No, it is push-based CD. For pull-based reconciliation see Kubernetes-Native Self-Service Infrastructure with Crossplane for Argo CD patterns and Who Deploys the Deployer? Terraform-Bootstrapped FluxCD for Flux.

// The outcome

Push to branch, get a rolling deployment, per service.

// Where to take it next

// Keep reading