Skip to content

Commit 06c31fc

Browse files
committed
Update posts with new Global stack
1 parent 21a64d2 commit 06c31fc

2 files changed

Lines changed: 260 additions & 81 deletions

File tree

posts/2023-10-07-the-stack-part-2.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ Instead of setting this up from scratch, start from the template for this step i
103103

104104
Our overall aim is to structure our CDK into three main groupings:
105105

106-
- `Cloud`: Hosted Zones, VPC, Certificates, infrequently changing things
106+
- `Global`: "Global" (often `us-east-1`) specific things such as ACM Certificates for CloudFront, and we'll also put Hosted Zones here
107+
- `Cloud`: Region specific infrequently changing things such as VPC, Region-specific Certificates, etc
107108
- `Platform`: DynamoDB, Cache, SQS
108109
- `Services`: Lambdas, API Gateway, etc
109110

110-
This split is based on the frequency something changes, and allows us to deploy freqeuently changing stacks without having to also look at things that very rarely change. In this part of the series we will set up the `Cloud` stack.
111+
This split is based on the frequency something changes, and allows us to deploy freqeuently changing stacks without having to also look at things that very rarely change. In this part of the series we will set up the `Global` stack.
111112

112113
As you can see in the [GitHub repository](https://github.com/codetalkio/the-stack/tree/part-2-automatic-deployments), we structure our CDK stack as follows:
113114

@@ -116,45 +117,47 @@ As you can see in the [GitHub repository](https://github.com/codetalkio/the-stac
116117
- `deployment.ts`: Our "executable" that CDK run run (defined in `cdk.json`)
117118
- `helpers.ts`: Helper functions to make our CDK code more readable and safe to run
118119
- `lib/`: The actual logic of our CDK stacks
119-
- `cloud/`: Our 'Cloud' layer containing all the resources that are shared across all stacks
120-
- `stack.ts`: Gathers all of our 'Cloud' stacks into one stack
120+
- `global/`: Our 'Global' layer containing all the resources that are shared across all stacks
121+
- `stack.ts`: Gathers all of our 'Global' stacks into one stack
121122
- `domain.ts`: Sets up our Hosted Zone and ACM certificates
122123

123124
This might seem like overkill right now, but will benefit us quite quickly as we start adding more stacks to our project.
124125

125-
In `deployment.ts` you'll see the root of our CDK stack. This is where we will define the three layers we mentioned earlier, `Cloud`, `Platform`, and `Services`. In CDK terminilogy these are called `Stack`s.
126+
In `deployment.ts` you'll see the root of our CDK stack. This is where we will define the three layers we mentioned earlier, `Global`, `Cloud`, `Platform`, and `Services`. In CDK terminilogy these are called `Stack`s.
126127

127-
For now, we will only define the `Cloud` layer:
128+
For now, we will only define the `Global` layer:
128129

129130
```typescript
130131
// ...imports
131132
const app = new cdk.App();
132133

133134
/**
134-
* Define our 'Cloud' stack that provisions the infrastructure for our application, such
135-
* as domain names, certificates, and other resources that are shared across all.
135+
* Define our 'Global' stack that provisions the infrastructure for our application, such
136+
* as domain names, certificates, and other resources that are shared across all regions.
136137
*
137138
* ```bash
138-
* bun run cdk deploy --concurrency 4 'Cloud' 'Cloud/**'
139+
* bun run cdk deploy --concurrency 6 'Global/**'
139140
* ```
140141
*/
141-
const cloudStackName = "Cloud";
142-
if (matchesStack(app, cloudStackName)) {
143-
new CloudStack(app, cloudStackName, {
142+
const globalStackName = "Global";
143+
if (matchesStack(app, globalStackName)) {
144+
// Some of our global resources need to live in us-east-1 (e.g. CloudFront certificates),
145+
// so we set that as the region for all global resources.
146+
new GlobalStack(app, globalStackName, {
144147
env: {
145-
account: process.env.CDK_DEFAULT_ACCOUNT || process.env.AWS_ACCOUNT_ID,
146-
region: process.env.CDK_DEFAULT_REGION || process.env.AWS_REGION,
148+
account: process.env.AWS_ACCOUNT_ID || process.env.CDK_DEFAULT_ACCOUNT,
149+
region: "us-east-1",
147150
},
148-
domain: validateEnv("DOMAIN", cloudStackName),
151+
domain: validateEnv("DOMAIN", globalStackName)
149152
});
150153
}
151154
```
152155

153156
We've set up some conveniences to easily run a single stack, via `matchesStack`, and to validate our environment variables, via `validateEnv`.
154157

155-
Our `CloudStack` is then defined in `lib/cloud/stack.ts`, and more or less just pieces together the types and the sub-stacks in the `cloud/` directory.
158+
Our `GlobalStack` is then defined in `lib/global/stack.ts`, and more or less just pieces together the types and the sub-stacks in the `global/` directory.
156159

157-
The interesting bit here is the call to `new domain.Stack` which is what actually kicks off the provisioning of resources, which are defined inside the `lib/cloud/domain.ts` file on layer deeper:
160+
The interesting bit here is the call to `new domain.Stack` which is what actually kicks off the provisioning of resources, which are defined inside the `lib/global/domain.ts` file on layer deeper:
158161

159162
```typescript
160163
// ...imports
@@ -170,7 +173,7 @@ export class Stack extends cdk.Stack {
170173
}
171174
```
172175

173-
And finally we get to the interesting part of it all in `lib/cloud/domain.ts`. This is the first place we are actually defining resources that will be deployed to AWS, by calling the CDK `Construct`s that are available to us. `Construct` is the CDK terminology for the actual resources we create, i.e. our building blocks.
176+
And finally we get to the interesting part of it all in `lib/global/domain.ts`. This is the first place we are actually defining resources that will be deployed to AWS, by calling the CDK `Construct`s that are available to us. `Construct` is the CDK terminology for the actual resources we create, i.e. our building blocks.
174177

175178
We create our Hosted Zone via `new route53.HostedZone` and our ACM certificate via `new acm.Certificate`. You can find out more about each of these in the CDK docs:
176179

@@ -646,7 +649,7 @@ _setup-deployment:
646649
cd deployment
647650
bun install
648651

649-
# Deploy the specified <stack>, e.g. `just deploy Cloud`, defaulting to --all.
652+
# Deploy the specified <stack>, e.g. `just deploy 'Global/**'`, defaulting to --all.
650653
deploy stack='--all':
651654
#!/usr/bin/env bash
652655
set -euxo pipefail
@@ -680,7 +683,7 @@ $ just test deployment
680683
# Synthesize our CDK stack:
681684
$ just test synth
682685
# Deploy our CDK stack:
683-
$ just deploy # or just deploy Cloud
686+
$ just deploy # or just deploy Global
684687
```
685688

686689

0 commit comments

Comments
 (0)