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/infrastructure-as-code-with-aws-cdk.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ AWS CDK, which is built on TypeScript, is tightly integrated with AWS CloudForma
21
21
22
22
To get the most out of AWS CDK, it's essential to understand three fundamental concepts: Apps, Stacks, and Constructs. These concepts are the foundation of CDK, and understanding them will help us decide when to use the tool and how to structure our infrastructure as code.
23
23
24
-
### Apps (CDK Apps)
24
+
### Apps (CDK Apps)
25
25
26
26
A CDK App represents the root of our infrastructure as code, an entry point of our program. It's a composite of all the required resources for our application. Think of it as the top-level container that holds everything together. An App can contain multiple Stacks, which we'll discuss next.
27
27
@@ -34,6 +34,7 @@ In CDK, a Stack is the unit of deployment. It represents a single cloudformation
34
34
A Construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create or update a resource or a combination of resources. A Construct can have multiple resources that are coupled together, and a Stack can have multiple Constructs. Think of a Construct as a self-contained module that defines a specific piece of our infrastructure.
35
35
36
36
Here's a simple analogy to help illustrate these concepts:
37
+
37
38
* An App is like a city, which contains multiple neighborhoods (Stacks).
38
39
* A Stack is like a neighborhood, which contains multiple houses (Constructs) and other infrastructure (resources).
39
40
* A Construct is like a house, which is composed of multiple rooms (resources) and has its own architecture and configuration.
@@ -49,15 +50,17 @@ As we discussed about CDK, there is much more information available in the offic
49
50
50
51
Our use case involves deploying multiple applications on AWS ECS, with each application using RDS Postgres as its database. The twist was that we had to deploy these applications multiple times for different clients, with each client requiring database isolation. This meant that we needed a solution that could efficiently manage multiple deployments with minimal effort.
51
52
52
-
### The Requirements:
53
+
### The Requirements
54
+
53
55
* Deploy applications on AWS ECS
54
56
* Use RDS Postgres as the database
55
57
* Ensure database isolation for each client
56
58
* Deploy the same application multiple times for different clients
57
59
* Minimize onboarding effort for each new client
58
60
* Manage multiple environments (lower and higher) with similar infrastructure components, but with configuration changes
59
61
60
-
### The Challenge:
62
+
### The Challenge
63
+
61
64
To illustrate the challenge, let's consider an example. Suppose we have a bill generator service that needs to be deployed for multiple clients. The application code remains the same, but each client requires its own isolated database. We could either deploy the application on our own AWS infrastructure or on the client's AWS environment. However, this would require manual effort and configuration for each new client, which wasn't scalable.
62
65
63
66
### The Solution: AWS CDK to the Rescue
@@ -86,6 +89,7 @@ To illustrate the challenge, let's consider an example. Suppose we have a bill g
86
89
7.**Stateful Resource Updates**: Updating stateful resources can be challenging, as it often requires recreating the resource, which involves destroying the existing one first.
87
90
88
91
## When to Choose CDK?
92
+
89
93
1.**Reusable infrastructure**: Bundle infrastructure and reuse across multiple environments with minimal configuration changes.
90
94
2.**Infrastructure and app code together**: Keep infrastructure and application code in one place and written in the same programming language.
91
95
3.**Multi-tenant deployments**: Easily create deployments for multiple clients with minimal effort.
@@ -94,19 +98,23 @@ To illustrate the challenge, let's consider an example. Suppose we have a bill g
94
98
## Recommendations
95
99
96
100
### Modeling with Constructs and Deploying with Stacks
101
+
97
102
A Stack is the unit of deployment, meaning that everything within it is deployed together.
98
103
99
104
**Example: Machine Learning Model**
100
105
A machine learning model can also be modeled as a Construct and deployed as a Stack. The Constructs that make up this model include an Amazon S3 bucket for storing training data, an Amazon SageMaker notebook instance for model development, and an Amazon SageMaker endpoint for model deployment. These Constructs can be composed into a single high-level Construct, such as MachineLearningModel. This Construct can then be instantiated in one or more tacks for deployment, depending on the environment (e.g., dev, prod, staging).
101
106
102
107
### Avoid Changing the Logical ID
108
+
103
109
The logical ID is a unique identifier generated by AWS CDK for each resource in our Stack. Changing the logical ID of a stateful resource can lead to unintended consequences, such as
104
110
105
111
***Resource recreation**: If the logical ID changes, AWS CDK may recreate the resource, resulting in data loss or inconsistencies.
106
112
***Resource conflicts**: Changing the logical ID can cause conflicts with existing resources, leading to errors or unexpected behavior.
107
113
108
114
### Use Generated Resource Names
115
+
109
116
It's recommended to use generated resource names instead of physical names defined while resource creation which can lead to several issues, including: Resource recreation, Resource conflicts, Tight coupling
110
117
111
118
## Conclusion
112
-
Our experience with AWS CDK, we were able to achieve true "infrastructure as code." Our CDK codebase became the single source of truth for our infrastructure, allowing us to manage and version-control our environments with ease. This approach also enabled us to automate our deployments, reduce manual errors, and improve our overall efficiency.
119
+
120
+
Our experience with AWS CDK, we were able to achieve true "infrastructure as code." Our CDK codebase became the single source of truth for our infrastructure, allowing us to manage and version-control our environments with ease. This approach also enabled us to automate our deployments, reduce manual errors, and improve our overall efficiency.
0 commit comments