You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/multi-tenant-system-with-aws-cdk.md
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
-
title: "Building Scalable Multi-Tenant Systems with AWS CDK: An IaC Approach"
2
+
title: "Building Scalable Multi-Tenant Systems with AWS CDK: An IAC Approach"
3
3
authorId: "mufaddal"
4
4
date: 2024-11-22
5
5
draft: false
6
6
featured: true
7
7
weight: 1
8
8
---
9
-
In this blog I will be taking you on a journey of building the scalable and efficient IaC solution that we build for our multi-tenant system. Here we are not going to debate why we chose the CDK; that will be another discussion that can be highlighted in another blog. Instead, how we approached solving using AWS CDK is going to be discussed in this blog. Even if you are not very familiar with CDK, this blog can help to build a mental model of how we can think while writing the code for the infrastructure of such a complex system.
9
+
In this blog, I will be taking you on a journey of building the scalable and efficient IAC solution that we build for our multi-tenant system. Here we are not going to debate why we chose the CDK; that will be another discussion that can be highlighted in another blog. Instead, how we approached solving using AWS CDK is going to be discussed in this blog. Even if you are not very familiar with CDK, this blog can help to build a mental model of how we can think while writing the code for the infrastructure of such a complex system.
10
10
11
11
## What are Multi-tenant Systems?
12
12
@@ -28,7 +28,7 @@ AWS CDK, which is built on TypeScript, is tightly integrated with AWS CloudForma
28
28
29
29
## Understanding Our Requirements
30
30
31
-
Our use case involves the linear growth of services alongside the exponential growth of tenants.A critical requirement is that each tenant must have database isolation to ensure robust tenant data integrity and confidentiality.This leads us to choose an architecture where services and databases for each tenant are deployed in isolation.
31
+
Our use case involves the linear growth of services alongside the exponential growth of tenants.A critical requirement is that each tenant must have database isolation to ensure robust tenant data integrity and confidentiality.This leads us to choose an architecture where services and databases for each tenant are deployed in isolation.
32
32
33
33
Key requirements include:
34
34
@@ -47,17 +47,17 @@ To visualize our architecture, consider the following components:
47
47
48
48
## What do we know?
49
49
50
-
Now let’s briefly see what all things we have in our bucket and what is expected from IaC.
50
+
Now let’s briefly see what all things we have in our bucket and what is expected from IAC.
51
51
52
52
As we were using AWS as our cloud provider, we started looking into finalizing the architecture that we were going to use for our system. After all the R&D, we decided to go with the Multi-VPC architecture that is one of the recommendations from AWS, and yes, this was written in AWS CDK. And hence, taking recommendations from this CDK solution, we were able to achieve a single VPC for a single tenant, which then solved our complete isolation problem along with the platform VPC connectivity with the tenant VPCs. We will be looking at this in detail in this blog too.
53
53
54
54
Considering we have what we wanted for our networking infrastructure, then for applications we are going to use Fargate ECS services, RDS for databases, SSM for application environment variables, Secret Manager for application secrets, and Route 53 for maintaining the DNS records.
55
55
56
56
And for continuous integration and continuous deployment, we are going to use GitHub Actions. From all this decision, you might realize that we are avoiding anything self-hosted for now.
57
57
58
-
Before we start looking into CDK code, let me tell you I will only be going through the configuration file with you, not the actual code, because CDK only differs from other IaC tools in that it is written in imperative form, which means we make the configuration file public-facing and the actual code an abstraction, which then helps each member of the org to just learn how to manipulate the configuration file and not the actual code, which helps the infrastructure manipulation be very easy, quick, and scalable.
58
+
Before we start looking into CDK code, let me tell you I will only be going through the configuration file with you, not the actual code, because CDK only differs from other IAC tools in that it is written in imperative form, which means we make the configuration file public-facing and the actual code an abstraction, which then helps each member of the org to just learn how to manipulate the configuration file and not the actual code, which helps the infrastructure manipulation be very easy, quick, and scalable.
59
59
60
-
## IaC of Networking
60
+
## IAC of Networking
61
61
62
62
Let’s first start looking into how we break down the [recommended](https://github.com/aws-samples/aws-vpc-builder-cdk/tree/main) networking architecture to fit our solution.
63
63
@@ -117,7 +117,7 @@ providers:
117
117
firewallName: InspectionEgress
118
118
```
119
119
120
-
For incoming traffic from the internet Central ingress is considered as ingress VPC, and similarly, central egress is the VPC from where all traffic will go out to the internet.
120
+
For incoming traffic from the internet, Central ingress is considered the ingress VPC, and similarly, central egress is the VPC from where all traffic will go out to the internet.
121
121
122
122
```yaml
123
123
providers:
@@ -150,7 +150,7 @@ providers:
150
150
151
151
Platform VPC has connectivity with tenants VPCs, and tenants are not having cross-connectivity, as we can verify this with dynamicRoutes.
152
152
153
-
This setup was the first milestone as a part of the infrastructure, as now to onboard any new tenants we just need to add a small block of code and the routes like below.
153
+
This setup was the first milestone as a part of the infrastructure, as now to onboard any new tenants we just need to add a small block of code and the routes, like below.
154
154
155
155
```yaml
156
156
vpcs:
@@ -184,18 +184,18 @@ transitGateways:
184
184
inspectedBy: inspectionVpc
185
185
```
186
186
187
-
Moving forward from networking to application was going to be a little tricky because considering this networking setup using CDK, we have to be sure that we maintain the consistency across networking and application code for infrastructure.
187
+
Moving forward from networking to application was going to be a little tricky because, considering this networking setup using CDK, we have to be sure that we maintain the consistency across networking and application code for infrastructure.
188
188
189
189
So we had two options: either edit the same code to add another support for the application or create a new CDK project that will only care about the application, considering the networking part is already set up.
190
190
191
-
We chose to go with the 2nd approach because
191
+
We chose to go with the second approach because
192
192
193
-
1. Change in application-related configuration will be more aggressive than networking.
193
+
1. Changes in application-related configuration will be more aggressive than networking.
194
194
2. To make application configuration manipulated by developers, we have to keep the unusual data, according to developers, as little as possible in the same place.
195
195
3. Changes in networking configuration can impact the entire ecosystem, and hence maintenance of that should only come under specific teams like SRE/DevOps and should not be available to manipulate so easily by any member of the organization.
196
-
4. By keeping application IaC separate, it also helps in automating the CI/CD, which is also another topic we can discuss in a further blog.
196
+
4. By keeping application IAC separate, it also helps in automating the CI/CD, which is also another topic we can discuss in a further blog.
197
197
198
-
## IaC of Application
198
+
## IAC of Application
199
199
200
200
The basic idea of writing AWS CDK code is to bundle the unit of deployment into the same stack. CDK Stack represents a single CloudFormation stack, which is a collection of resources that are deployed together. So here, I have created the stack with a collection of resources that are going to be deployed together and are linked.
201
201
@@ -250,29 +250,29 @@ This is the first stack that we have written to create common IAM roles that are
250
250
251
251
### ECS Service Stack (with EFS)
252
252
253
-
The stack creates the ECS service for the tenants cluster by identifying via configuration file alongside the ALB, NLB, EFS, and security groups.
253
+
The stack creates the ECS service for the tenant cluster by identifying via the configuration file alongside the ALB, NLB, EFS, and security groups.
254
254
255
255
### RDS Stack
256
256
257
257
Keeping the stateful resources separate is one of the best practices that we followed, and hence the creation of the RDS stack is kept in a different stack along with the KMS key, security groups, and updating the secret manager with RDS credentials.
258
258
259
259
### Public ALB
260
260
261
-
This is one of the common stacks we identified to create a public-facing application load balancer separately by following practices of attaching ACM and proper security groups.
261
+
This is one of the common stacks we identified to create a public-facing application load balancer separately by following the practices of attaching ACM and proper security groups.
262
262
263
263
### Internal ALB
264
264
265
265
CDK Stack that is used to create a separate internal ALB, such as in the platform VPC, to communicate with the tenants VPC services
266
266
267
267
## Conclusion
268
268
269
-
In conclusion, building a scalable and efficient multi-tenant system on AWS requires careful planning and design. By using AWS CDK, we were able to define and provision our cloud infrastructure resources in a flexible and scalable way. Our approach to separating IaC code for networking and applications allowed us to maintain consistency and make changes more easily. We hope that this blog post has provided a useful example of how to use AWS CDK to build a multi-tenant system.
269
+
In conclusion, building a scalable and efficient multi-tenant system on AWS requires careful planning and design. By using AWS CDK, we were able to define and provision our cloud infrastructure resources in a flexible and scalable way. Our approach to separating IAC code for networking and applications allowed us to maintain consistency and make changes more easily. We hope that this blog post has provided a useful example of how to use AWS CDK to build a multi-tenant system.
270
270
271
-
We look forward to sharing more of our experiences in future blog posts with follow-up questions like below.
271
+
We look forward to sharing more of our experiences in future blog posts, with follow-up questions like below.
272
272
273
273
1. How to automate CDK infrastructure provisioning with GitHub Actions?
274
274
2. When to choose CDK over Terraform?
275
275
3. What are the downsides of choosing a multi-VPC AWS architecture?
276
-
4. How to design a scalable deployment pipeline for multiple services and tenants?
277
-
5. How to plan for disaster recovery?
276
+
4. How do you design a scalable deployment pipeline for multiple services and tenants?
277
+
5. How do you plan for disaster recovery?
278
278
6. How to optimize multi-VPC architecture to reduce costs?
0 commit comments