// Why this matters
A Terraform platform for EKS, Aurora and OpenSearch is what most data-driven applications end up needing: a container runtime, a relational store, a search index, a key-value table and some glue functions. Standing that up by clicking, once per environment, produces three subtly different environments and a deployment process nobody trusts.
The goal here was one repeatable pattern that produces dev, test and production from the same modules, with differences expressed as a small file of inputs. That is the whole value proposition of infrastructure as code, but it only holds if the module boundaries and state layout are chosen well.
This write-up reproduces the architecture I delivered for a freelance client, rewritten with generic names. It uses current module versions where the original used older ones, and flags where the original shortcuts should not be copied.
// The problem
When everything lives in one root with copy-pasted settings per environment, drift is guaranteed. Someone raises an instance size in production and forgets staging; someone adds a security group rule by hand. Terraform plans then show surprises, and applying them is risky.
The service mix adds its own traps. EKS pods that need S3 or DynamoDB access are often given the node role, so every pod on a node has every permission. OpenSearch in a VPC needs a service-linked role, a security group that admits the right sources, and an access policy; forgetting any one gives a domain that creates fine and cannot be queried. A Lambda inside a VPC cannot reach the internet or AWS APIs without endpoints or NAT.
Finally, globally unique names (buckets, domains) and remote state configuration have to be generated or injected. Hard-coding them blocks the second environment from ever being created.
// The solution
The layout is a modules/ directory with one module per service and a root that composes them. Each module owns its security group and exposes only what callers need (an endpoint, a security group ID, an ARN). Callers allow traffic into a data store by referencing the consumer security group rather than a CIDR, which keeps rules meaningful when subnets change.
For EKS I use the community module with IRSA enabled. IRSA works by publishing the cluster OIDC issuer as an IAM identity provider; a role trusts that provider with a condition on the service account subject. A pod using that account receives short-lived web-identity credentials for exactly that role. This replaces the broad node policy and is the single most valuable security change in the stack. The original build also granted wildcard actions to the pod role; scope those to named resources.
The AWS Load Balancer Controller is installed with a Helm release from Terraform, with its own IRSA role. That makes ingress resources work immediately and keeps the controller version pinned in code. Aurora, OpenSearch and DynamoDB live in private subnets, reachable only from the EKS node and Lambda security groups.
Terragrunt sits above all of it. A root terragrunt.hcl configures the S3 backend with a key derived from the path, and each environment folder holds only an inputs block. I considered Terraform workspaces and rejected them: they share one backend and one code path, which makes accidents across environments easy. If you want to see the same idea applied to a multi-cloud foundation, read Adding a Region Without Destroying Anything and One Reusable Plan/Apply Workflow for Cost-Optimised Autoscaling.
- Terragruntremote state, per-environment variables
- VPC modulenetwork foundation
- EKS modulecluster, workers, worker policy
- IRSA gatepod role bound through an annotated service account
- Load balancer controllerinstalled via Terraform Helm release
- Data storesAurora, OpenSearch with access policy, DynamoDB
- ServerlessVPC Lambda with its own security group and role
- Workloadsdeployments, services and ingress
// Before you start
- Terraform 1.6+, Terragrunt 0.55+, kubectl, Helm 3 and the AWS CLI v2.
- An AWS account and role able to create VPC, EKS, RDS, OpenSearch, DynamoDB, Lambda and IAM resources.
- An S3 bucket and DynamoDB table for remote state (created once, outside this stack).
- Familiarity with IAM trust policies and Kubernetes service accounts.
- A plan for CIDR ranges per environment so peering stays possible later.
// Step-by-step implementation
01 Repository layout and Terragrunt remote state
Keep reusable code and environment configuration apart. The root
terragrunt.hcldefines the backend once, with a state key derived from the folder path so each environment and component gets its own state automatically.Each environment folder contains only an
inputsblock. If an environment needs something the module cannot express, that is a signal to extend the module, not to fork it.02 VPC module with private subnets and tagged subnets for EKS
Use the community VPC module. The subnet tags are not decoration: the load balancer controller discovers where to place internal and public load balancers through the
kubernetes.io/role/elbandinternal-elbtags.One NAT gateway per environment is enough for non-production; production usually wants one per availability zone.
03 EKS with IRSA and a scoped pod role
Enable IRSA on the cluster module, then create a role whose trust policy names a specific namespace and service account. The sample original used a wildcard subject, which lets any service account in the cluster assume the role. Pin it.
Give the role only what the workload uses. Replace broad
s3:*anddynamodb:*statements with the actions and ARNs of the buckets and tables this stack creates.04 Install the AWS Load Balancer Controller with Helm from Terraform
The controller needs its own IRSA role with the published controller policy. Installing it from Terraform means a fresh environment has working ingress straight after apply, and the chart version is pinned in code instead of in someone memory.
Configure the Helm provider from the cluster outputs using an exec-based token, not a token that expires mid-apply.
05 Aurora in private subnets, reachable only from the cluster
Create a security group that allows the database port only from the node security group (and from the Lambda security group, added below). Put the cluster in the database subnet group and keep a parameter group in code so tuning is reviewed.
The master credentials are managed by RDS in Secrets Manager. That avoids a password in Terraform state and in variables, and rotation is a setting rather than a project.
06 OpenSearch inside the VPC with an access policy
A VPC domain has no public endpoint, so the security group is the front door. Allow HTTPS from the node security group only. The domain also needs the service-linked role to exist in the account before the first VPC domain is created; if it is missing the apply fails with a message that does not obviously mention it.
Add a resource-based access policy that permits the pod role and nothing else. Network reachability alone is not authorisation.
07 DynamoDB, S3 and a VPC-attached Lambda with its own role
DynamoDB and S3 need no network plumbing, but a Lambda in the VPC does. Give it a dedicated security group with egress only, and allow that group into Aurora. If the function must call AWS APIs, add VPC endpoints for them or a NAT route; otherwise invocations hang until timeout.
Keep the Lambda role least-privilege and separate from the pod role. Attach the managed VPC access policy for network interfaces and add only the table and bucket permissions it uses.
08 Plan and apply per environment
Run Terragrunt from the environment folder. The first apply for a fresh account takes a while: EKS and OpenSearch each take around fifteen to twenty minutes. Apply in a single run so the Helm release sees a working cluster; if you split it, apply the cluster first.
// How to verify it works
kubectl -n kube-system get deploy aws-load-balancer-controllerreports 2/2 available; creating an Ingress produces an ALB within a couple of minutes.- A pod using
app-sarunsaws sts get-caller-identityand shows the pod role; a pod using another service account cannot assume it. - From a pod,
curlto the OpenSearch endpoint succeeds with the pod role credentials and is refused from outside the VPC. terragrunt planin dev, test and prod shows no unexpected diffs, and each environment has its own state key in the bucket.- The Lambda reads a DynamoDB item and connects to Aurora from inside the VPC; removing the Lambda security group rule breaks the database connection as expected.
// Design decisions and trade-offs
- Module per service; per-service security groups.
- IRSA instead of node-wide credentials.
- Random suffixes for globally unique names; DRY environments via Terragrunt.
// Pitfalls and lessons learned
- A wildcard subject in the IRSA trust policy lets every service account assume the role. Always pin namespace and name.
- The OpenSearch service-linked role must exist before the first VPC domain. Missing it fails the apply with a confusing error.
- A Lambda in private subnets with no NAT or endpoints times out on any AWS API call. Plan the network path when you attach it.
- Modules with a hard-coded name collide in the second environment. Prefix every name with the environment and use random suffixes for globally unique ones.
- Pod roles granted wildcard actions on services are the node-role problem again. Scope actions and resources to this stack.
- Database passwords in variable files land in state. Let RDS manage the master secret.
// Frequently asked questions
How do I give EKS pods AWS permissions with Terraform?
Enable IRSA on the cluster, create an IAM role whose trust policy references the cluster OIDC provider and one service account subject, and annotate that service account with the role ARN.
Why Terragrunt instead of Terraform workspaces?
Terragrunt gives each environment its own backend key and inputs file with shared code. Workspaces share a backend and code path, which makes cross-environment mistakes easier.
How do I install the AWS Load Balancer Controller with Terraform?
Create an IRSA role with the controller policy and install the chart with a helm_release, setting the cluster name, VPC ID and service account annotation.
Why cannot I reach my OpenSearch domain in a VPC?
Check the security group allows HTTPS from your source, the access policy permits your principal, and you are calling from inside the VPC. A VPC domain has no public endpoint.
Should each service be its own state?
Split state when lifecycles differ or blast radius matters. This stack keeps one state per environment for simplicity; large teams often split network, data and compute.
// The outcome
One repeatable pattern across dev, test and production.
// Where to take it next
- Split the platform into network, data and compute stacks with Terragrunt dependencies once teams grow.
- Add Karpenter or cluster autoscaler tuned to your workload, and pod disruption budgets.
- Add alarms for Aurora, OpenSearch and Lambda errors and route them to your on-call tool.
- Compare the Kubernetes-native alternative in Kubernetes-Native Self-Service Infrastructure with Crossplane.