<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sami Jaballah</title>
    <description>The latest articles on DEV Community by Sami Jaballah (@sami_jaballah).</description>
    <link>https://dev.to/sami_jaballah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2599463%2Fbc64cc21-0c22-4d8c-99e3-f6b82e5bffce.png</url>
      <title>DEV Community: Sami Jaballah</title>
      <link>https://dev.to/sami_jaballah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sami_jaballah"/>
    <language>en</language>
    <item>
      <title>Migrating from AWS CDK v1 to CDK v2</title>
      <dc:creator>Sami Jaballah</dc:creator>
      <pubDate>Sun, 16 Feb 2025 09:40:02 +0000</pubDate>
      <link>https://dev.to/sami_jaballah/migrating-from-aws-cdk-v1-to-cdk-v2-21nd</link>
      <guid>https://dev.to/sami_jaballah/migrating-from-aws-cdk-v1-to-cdk-v2-21nd</guid>
      <description>&lt;p&gt;If you’re currently using CDK v1 in your daily work, you’ve probably hit a roadblock trying to implement new AWS features. AWS isn’t adding those to CDK v1 anymore—so to keep up with the latest and greatest, you’ll need to migrate to CDK v2. It might sound like a hassle, but don’t worry—I’ve got your back. Let’s go through this step-by-step and get you up to speed, Python style.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Should You Care About CDK v2?
&lt;/h2&gt;

&lt;p&gt;Alright, let’s address the big question: why even bother migrating to CDK v2? Well, here are three solid reasons:&lt;/p&gt;

&lt;p&gt;Simplified Dependencies: No more pulling in tons of packages for different AWS services. CDK v2 bundles everything into a single package: aws-cdk-lib. How awesome is that?&lt;/p&gt;

&lt;p&gt;Stay Updated: CDK v1 isn’t getting any love anymore. If you want to keep up with the latest AWS features and updates, v2 is where it’s at.&lt;/p&gt;

&lt;p&gt;Better Developer Experience: AWS has introduced some stability guarantees and cleaned up APIs, making it easier for us to write and maintain our infrastructure code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s New in CDK v2?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Consolidated Package Structure&lt;/strong&gt;&lt;br&gt;
Remember the days of importing a separate package for each AWS service? That’s history now. CDK v2 unifies everything into aws-cdk-lib.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CDK v1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from aws_cdk import core
from aws_cdk.aws_s3 import Bucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CDK v2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from aws_cdk import Stack
from aws_cdk.aws_s3 import Bucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Goodbye to Deprecated APIs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some APIs and constructs from v1 didn’t make the cut in v2. For instance, core.Construct has been replaced by constructs.Construct. A little cleanup never hurts, right?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Shiny New Features&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are some great new features, like improved stability guarantees for low-level (L1) constructs and better testing capabilities with assertions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9f4hu541mv1nntkm1uw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9f4hu541mv1nntkm1uw.png" alt="AWS CDK v1 vs v2 Comparison" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Let’s Get Migrating: Step-by-Step Guide
&lt;/h2&gt;

&lt;p&gt;Ready to dive in? Follow these steps to upgrade your Python CDK project to v2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Update Your Dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Start by upgrading your project dependencies to use CDK v2. Open your requirements.txt or Pipfile and update them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws-cdk-lib&amp;gt;=2.0.0
constructs&amp;gt;=10.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, install the new dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Fix Your Imports&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where the magic happens. Go through your code and replace aws_cdk.core with aws_cdk.Stack, and adjust other imports to use aws-cdk-lib.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before (CDK v1):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from aws_cdk import core
from aws_cdk.aws_s3 import Bucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After (CDK v2):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from aws_cdk import Stack
from aws_cdk.aws_s3 import Bucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Refactor Deprecated Constructs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some constructs have been replaced or removed. For example, core.Construct is now constructs.Construct. Update your code accordingly.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyBucket(core.Construct):
    def __init__(self, scope: core.Construct, id: str):
        super().__init__(scope, id)
        Bucket(self, "MyBucket")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from constructs import Construct

class MyBucket(Construct):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)
        Bucket(self, "MyBucket")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Remove Unnecessary Feature Flags&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CDK v2 has removed or integrated several feature flags that were necessary in v1. To clean up your cdk.json file, remove any obsolete flags.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "app": "python3 app.py",
  "context": {
    "@aws-cdk/core:newStyleStackSynthesis": true,
    "@aws-cdk/aws-ec2:uniqueImds": true,
    "@aws-cdk/core:stackRelativeExports": true,
    "@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
    "@aws-cdk/aws-kms:defaultKeyPolicies": true,
    "@aws-cdk/core:enableStackNameDuplicates": true,
    "aws-cdk:enableDiffNoFail": true,
    "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
    "@aws-cdk/aws-s3:grantWriteWithoutAcl": true,
    "@aws-cdk/aws-efs:defaultEncryptionAtRest": true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "app": "python3 app.py"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Removing these flags ensures your project stays aligned with CDK v2 best practices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Your Migration
&lt;/h2&gt;

&lt;p&gt;Finally, make sure everything works as expected. Run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk synth
cdk diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix any issues that pop up, and you’re good to go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Migration Verification Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; All imports updated to aws-cdk-lib&lt;/li&gt;
&lt;li&gt; Construct imports moved to constructs package&lt;/li&gt;
&lt;li&gt; Feature flags removed&lt;/li&gt;
&lt;li&gt; Property names verified&lt;/li&gt;
&lt;li&gt; Tests passing&lt;/li&gt;
&lt;li&gt; cdk diff shows expected changes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wrapping It Up
&lt;/h2&gt;

&lt;p&gt;And there you have it! Migrating from CDK v1 to v2 isn’t as scary as it might seem. With unified dependencies, better APIs, and future-proofing, this upgrade is worth the effort. Take it one step at a time, and don’t hesitate to ask for help if you hit a roadblock.&lt;/p&gt;

&lt;p&gt;Have you already migrated to CDK v2? Or are you planning to? Share your experience (or any questions) in the comments below!&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful links to help you along the way:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/fr_fr/cdk/v2/guide/migrating-v2.html" rel="noopener noreferrer"&gt;AWS CDK v2 Migration Guide&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/cdk/api/v2/python" rel="noopener noreferrer"&gt;AWS CDK Python Reference&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>devops</category>
      <category>migration</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
