# AWS CDK lets FDEs rebuild a customer's stack without retyping it

> The usual trap is a hard-coded bucket name or a renamed construct, and either can break a deployment that should repeat cleanly for the next customer.

Original: https://fdetimes.net/en/tools/aws-cdk-repeatable-customer-infrastructure/

A single hard-coded bucket name in your code is enough to stop you building the same infrastructure a second time in the same AWS account. The AWS CDK best-practices guide warns about exactly this. For a Forward Deployed Engineer, it is the line between a repeatable deployment and a week spent clicking through the console again for a new customer.

FDEs rarely work for just one customer. The pattern you build for the first will sooner or later be carried over to the second and third, sometimes with a requirement that the data live in a different Region. If the first customer's infrastructure exists only in the console and in your head, the next customer starts from zero.

AWS CDK turns infrastructure into a reusable code library. To use it well, you need to know what it does, where to draw the boundaries and where things tend to go wrong.

## Infrastructure as ordinary code, deployed through CloudFormation

CDK is an open-source framework developed by AWS. You describe infrastructure in code and CDK provisions it through CloudFormation. The framework supports TypeScript, JavaScript, Python, Java, C#/.NET and Go. The source lives in the public aws/aws-cdk repository under the Apache-2.0 licence.

Version 2 became generally available on 2 December 2021 and gathered every stable construct library into a single package, aws-cdk-lib. A new project therefore needs one main dependency rather than a string of separate packages.

The biggest benefit is that infrastructure is now ordinary code. You review it through pull requests, write unit tests for it and manage it in source control like any other software. At handover, the customer receives a readable repository rather than a list of console steps.

## Each customer is an environment

In CDK, an environment is the AWS account and Region pair a stack is deployed to. The official documentation illustrates this by instantiating the same stack class twice, once with a US env and once with an EU env. That is also the basic pattern for per-customer deployment.

AWS recommends separating two jobs: **model with constructs, deploy with stacks**. A construct is a reusable logical unit, such as a document-ingestion pipeline. A stack only assembles constructs for a specific deployment scenario. Imagine customer A runs in the US and customer B requires its data to stay in the EU. The minimal code looks like this:

```ts
import { App, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';

// Construct reused for every customer
class DocIngest extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    new s3.Bucket(this, 'Raw'); // no bucketName set, let CDK generate the name
  }
}

class CustomerStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);
    new DocIngest(this, 'DocIngest');
  }
}

const app = new App();
new CustomerStack(app, 'customer-a-us', { env: { account: '111111111111', region: 'us-east-1' } });
new CustomerStack(app, 'customer-b-eu', { env: { account: '222222222222', region: 'eu-west-1' } });
```

Each customer's configuration sits in the source code itself rather than in a parameterised template. This is what AWS recommends, so that every commit produces a deterministic CloudFormation template for each environment. When a customer asks which version of the infrastructure is running, the answer is a commit hash.

Before the first deployment, each environment must be bootstrapped once with `cdk bootstrap`, because environments are entirely independent of one another. Take 2 customers, each with staging and production in two different Regions: that is 4 environments, and 4 bootstraps. Put this step on the checklist for the day you are first given access to a customer's account.

## Three ways to break repeatability

The first is resource names. If you hard-code a table or bucket name, the same piece of infrastructure cannot be deployed twice in one account. When the customer wants a test environment alongside production, everything grinds to a halt. Let CDK generate the names and pass them to the application.

The second is more dangerous. Changing a resource's logical ID, for instance by renaming a construct during a refactor, causes that resource to be replaced with a new one on the next deployment. For a customer's database, that is a real incident. AWS advises putting stateful resources such as databases in a separate stack and turning on termination protection.

The third is lookups, such as finding a VPC or an AMI. The results are cached in `cdk.context.json`, and that file needs to be committed with the code so later runs use the same values. Customer environments often live for years, so a single lookup that returns a different result is enough to throw a stable deployment off course.

**Key point:** Infrastructure is repeatable only when nothing in it is named or looked up by hand.

## Limits to know before you promise a customer anything

CDK provisions through CloudFormation, so it is a tool for infrastructure on AWS. If the customer runs on another cloud, you need a different approach. The bootstrap step also requires permissions in each of the customer's accounts. At large enterprises, obtaining them can take longer than writing the code, so raise the question in the discovery session.

As the number of customers grows, deploying from a laptop stops being viable. The CDK Pipelines module automates building, testing and deploying a CDK app, and it updates itself: add a new stage or stack and the pipeline reconfigures itself to deploy it. AWS recommends putting stages in separate environments, which fits the one-account-per-customer model.

## The order to learn it in

Start with the boundary between construct and stack, because it is the design decision hardest to change later. Next come environments and bootstrapping, then unit tests for templates. Leave CDK Pipelines for last, once you genuinely have two or more customers to manage.

When reading FDE job ads, look for phrases such as "infrastructure as code" or "deploy into customer environments". On a CV, a specific line such as "one construct, deployed to N environments, with unit tests" carries far more weight than "proficient in AWS". The best proof is a public repository that does exactly that.

With the first customer, CDK can look like overhead. By the second, you will see why it pays.

**Try this week:**

- Build a TypeScript CDK app with one construct and two stacks pointing at two different Regions, then run cdk bootstrap for each environment
- Go through your current infrastructure repo, find every hard-coded bucket or table name and remove it
- Write a unit test that checks the template your stack generates, then put the repo on GitHub as part of your portfolio

## Sources

- [What is the AWS CDK? - AWS CDK v2 Developer Guide](https://docs.aws.amazon.com/cdk/v2/guide/home.html)

- [AWS Cloud Development Kit (AWS CDK) v2 is now generally available](https://aws.amazon.com/about-aws/whats-new/2021/12/aws-cloud-development-kit-cdk-generally-available)

- [aws/aws-cdk: The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code](https://github.com/aws/aws-cdk)

- [Environments for the AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/environments.html)

- [Best practices for developing and deploying cloud infrastructure with the AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html)

- [AWS CDK bootstrapping](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html)

- [Continuous integration and delivery (CI/CD) using CDK Pipelines](https://docs.aws.amazon.com/cdk/v2/guide/cdk-pipeline.html)
