<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Ftouh on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Ftouh on Medium]]></description>
        <link>https://medium.com/@aftouh?source=rss-51494ab4a15b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*O-MDTMgWQKcUzsf-YKMYEw.jpeg</url>
            <title>Stories by Ahmed Ftouh on Medium</title>
            <link>https://medium.com/@aftouh?source=rss-51494ab4a15b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 03:18:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aftouh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Organize Your AWS CDK Project]]></title>
            <link>https://medium.com/better-programming/how-to-organize-your-aws-cdk-project-f1c463aa966e?source=rss-51494ab4a15b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1c463aa966e</guid>
            <category><![CDATA[cdk]]></category>
            <category><![CDATA[infra-as-code]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ahmed Ftouh]]></dc:creator>
            <pubDate>Thu, 14 Jan 2021 16:08:16 GMT</pubDate>
            <atom:updated>2021-01-14T16:08:16.451Z</atom:updated>
            <content:encoded><![CDATA[<h4>In the past, I would build my infrastructure as code using Terraform. Lately, I discovered AWS CDK and decided to give it a try</h4><figure><img alt="Bridge structure" src="https://cdn-images-1.medium.com/max/1024/0*C6tskh2rGIR_5s8I" /><figcaption>Photo by <a href="https://unsplash.com/@alain_pham?utm_source=medium&amp;utm_medium=referral">Alain Pham</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>.</figcaption></figure><p>In this article, I will share with you how to organize and tweak your <a href="https://aws.amazon.com/cdk/">CDK</a> repository to provision a multi-stage AWS infrastructure.</p><p>We will build a simplified version of the infrastructure composed of a VPC and its subresources, a Kubernetes cluster, and an <a href="https://www.elastic.co/elasticsearch/">Elasticsearch</a> cluster.<br>This project is developed in Python and the source code is available on <a href="https://github.com/aftouh/cdk-template">GitHub</a>.</p><h3>Bootstrapping the CDK App</h3><p>The first step consists of initializing the Python project using this CDK command line:</p><pre>cdk init sample-app — language python</pre><p>That generates a couple of files and directories. These are the retained elements:</p><ul><li>setup.py: Defines the Python package (name, files, package metadata, runtime dependencies, etc.).</li><li>cdk.json: Tells the CDK toolkit how to execute the app.</li><li>app.py: The entry point of the project.</li><li>tests/unit: Finally, it’s possible to have unit tests for the infra like with any other application.</li></ul><p>This CDK sample-app is a good starting point but clearly not enough to build a production-ready multi-stage infrastructure.</p><h3>Organizing Stacks and Constructs</h3><p>The core components of the CDK framework are stacks and constructs.<br>A <em>stack</em> is the unit of deployment. All resources (or constructs) defined within a stack are provisioned or destroyed as a single entity. A <em>construct</em> is the basic building block that defines one or more AWS resources.</p><p>It is important to take time and think about how to organize your stacks and constructs within your CDK app at the beginning of the project. Trying to reorganize your app later may become confusing and introduce breaking changes.</p><p>In our context, we end up having three stacks:</p><ul><li>Network stack: Defines network resources like VPC, subnets, and security groups.</li><li>Compute stack: Defines stateless resources like Kubernetes cluster, bastion.</li><li>Data stack: Defines stateful resources. In our context, that is an Elasticsearch cluster.</li></ul><p>The main reasons for this splitting are:</p><ul><li>Flexibility: As explained earlier, having all the AWS resources in one stack obliges us to create or destroy all of them at once. With this three-stack setup, it is possible, for example, to destroy the data and compute stacks while maintaining Elastic IPs and VPC peering configuration defined in the Network stack.</li><li>Maintainability: This splitting offers a better developer experience. <br>For example, if a contributor is working on the Compute stack, they don’t need to create the Data stack in the ephemeral contributor stage to test their changes. In our case, we don’t need to wait for the 30 minutes required to create the Elasticsearch cluster.</li><li>Cost optimization: This splitting offers the possibility to schedule a deletion of the compute stack of non-production stages during the weekend while keeping our dataset. We still can set up a different destruction/creation strategy for the Data stack.</li></ul><p>These three stacks are available under the stacks directory, with each of them in a separate subdirectory. Within each stack, we defined one or more custom constructs<strong> </strong>that use one or more CDK built-in constructs. For example, the Eks<em> </em>custom<em> </em>construct<em> </em>uses aws_eks.Cluster, aws_eks.NodeGroup, and<em> </em>aws_iam.Role<em> </em>as<em> </em>built-in constructs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc976d4f5bdff19c8671c782d6dc88a0/href">https://medium.com/media/cc976d4f5bdff19c8671c782d6dc88a0/href</a></iframe><p>This helps for easier readability and maintainability of our code. At this level, it is more about how to organize your code like you would for any other application.</p><p>Now that we have our three stacks, all we need to do is to instantiate them in the app.py file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9372c805f673c701d5f240a76840d88d/href">https://medium.com/media/9372c805f673c701d5f240a76840d88d/href</a></iframe><h3>File Configuration</h3><p>One of the advantages of Infra as Code is the ability to replicate the same stack for different stages (e.g. dev, prod).</p><p>Building the infra using a programming language like Python gives us the opportunity to handle our CDK app config like we would on any other application. One of the common ways is using YAML files. In our case, these files are stored in the config directory and parsed using the Python <a href="https://pypi.org/project/python-benedict/">benedict</a> library:</p><ul><li>common.yaml: Contains the shared config and default values that are applied to all stages.</li><li>&lt;stage-name&gt;.yaml: Each stage has a dedicated config file that defines stage-specific inputs (AWS account, region, network CIDR) and allows the possibility to override default values set in the common config file (instance type, cluster size, etc.).</li></ul><p>Now that we have our configuration set up, we can start deploying stages using one of these command lines:</p><pre># Deploy the 3 stacks in the prod stage<br>cdk deploy -c stage=dev &#39;*&#39;</pre><pre># Deploy the Network and Compute stacks in the dev stage<br>cdk deploy -c stage=dev NetworkStack ComputeStack</pre><p>Take a look at the <a href="https://github.com/aftouh/cdk-template/blob/main/Makefile">Makefile</a> for more details about stage lifecycle commands.</p><h3>Other Elements</h3><p>There are some other elements of this project that I want to mention here:</p><ul><li>cdk.version: As you can guess, it stores the current CDK version of the application. This is useful, as the CDK version is used in different places (Python dependencies, CI, etc.). Having this in one place is really helpful — especially for an upgrade.</li><li>utils: Contains some util functions used within the stacks (e.g. config parser) and tags builder functions.</li></ul><p>That’s basically how our CDK repository is organized. It is a good fit for our context, and by setting up a solid CICD based on a GitOps workflow, we went serenely to production.</p><p>I recommend reading <a href="https://medium.com/better-programming/aws-cdk-continuous-integration-and-delivery-using-travis-ci-ee5dd7549434">this article</a> if you want to know more about this GitOps workflow.</p><h3>Conclusion</h3><p>If you are used to Terraform, building your cloud environments with CDK requires a different way of thinking and organizing to tackle an infrastructure project. By using Python as a programming language, I get the feeling of dealing with this project as I would any other standard Python one. Other members of the team who were not necessarily comfortable with infra staff were less reluctant to contribute to this project, and I believe that’s due to the fact that there is no need to learn a new “programming” language.</p><p>CDK is a promising tool that could help organizations to go further in their DevOps culture.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1c463aa966e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-to-organize-your-aws-cdk-project-f1c463aa966e">How to Organize Your AWS CDK Project</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>