dev, stage, prod
This document explains in more detail which environments we have, what the difference between AWS accounts and environments is, and how we distinguish between environments in the source code.
AWS Accounts vs. Environments
AWS Accounts
For the Central CDN, two AWS accounts exist: statistaplatform-stage and
statistaplatform-prod, or stage and prod for short.
In the source code, these are identified via the enum type AccountType. Thus,
an object of type AccountType can only have one of the two values
AccountType.STAGE or AccountType.PROD.
Environments
The main environments we deploy are of course stage and prod.
However, in order to be able to do some development without interfering with the
main environments, it is also possible to have additional development
environments. These are called e.g. dev1 or dev2 and are deployed into the
stage AWS account.
In the source code, these environment names are usually stored in a string
variable called envName. Following this logic, the environments stage and
prod have the envNames stage and prod again, but dev environments have
different envNames like dev1.
Due to the fact that dev environments can only be deployed into the stage AWS
account, their AccountType is stage again.
Sometimes, we need to distinguish in the code whether or not we are dealing with
a dev environment. This is done using the flag isSpecialDev. It is set to
true for dev environments and false for the environments stage and prod.
Define From OutSide Which Environment to Deploy
So how does the code distinguish between the environments? How does it "know", which environment to deploy?
This happens mostly in the main CDK file called
infra-main.ts. In there, we read the value of env-name
which is handed over to the cdk deploy command from "outside" as a command
line argument.
Depending on the value of this command line argument, the code decides which
values the variables accountType, envName, and isSpecialDev get. These
variables are then used throughout the rest of the code.
stage or prod: Use Factory Instead of if-then-else
We want to avoid code that has the form
if (stage === true) {
// do things for stage
} else {
// do things for prod
}
as it can make it hard to understand where the differences between the stage
and the prod environment are.
Instead, there is a factory called EnvConstantsFactory which provides those
values that need to be differentiated between stage and prod. It can be used
like this:
const accountId = EnvConstantsFactory.getConstsForAccountType(accountType).backupAccountId
where accountType is of type AccountType again. The above line e.g. provides
the different AWS account IDs for the backups, depending on whether we deploy to
stage or prod.
This way, most of the differences between stage and prod are stored in one
config file per account type, i.e., in the files
env-constants-stage.ts and
env-constants-prod.ts.