lunes, 3 de noviembre de 2025

Import an existing resource using AWS CDK

To fully manage the existing resource with your CDK stack (allowing you to modify its properties later), you must perform a Resource Import using the cdk import CLI command, which relies on CloudFormation's import capabilities.


Let's test this with new cdk code, importing a sns topic.

cdk init app --language=python

 

Create venv

source .venv/bin/activate && pip install -r requirements.txt

 

 We would like import this resource

 

 

On the cdk code you need to write the following, referencing the topic.

from aws_cdk import (
# Duration,
Stack,
aws_sns as sns,
aws_iam as iam
)
from constructs import Construct

class CdkImportStack(Stack):

def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

existing_topic = sns.Topic(self, "MyManagedTopic", topic_name="MyTopic")

 

Run cdk synth to see the cloudformation template

cdk synth


Resources:
  MyManagedTopic0CEB7327:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: MyTopic
    Metadata:
      aws:cdk:path: CdkImportStack/MyManagedTopic/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/zPcxxxxxxx
    Metadata:
      aws:cdk:path: CdkImportStack/CDKMetadata/Default
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]


Then run and it will ask for the topic arn

cdk import CdkImportStack

 

You will see on the cloudformation stack the resource imported

 

 

domingo, 2 de noviembre de 2025

CDK migrate, a way to migrate your cloudformation template to cdk code

 The migration from the CloudFormation template to the CDK is done with a single command: cdk migrate. Simply point to the local CloudFormation template file (let’s call it demo-template.yaml), and watch as the CLI converts the template into a CDK application. The output and result from running the command will be a directory comprised of the CDK code and dependencies, but will not deploy the stack.

You can grab the template code from cloudformation. 

cdk migrate --stack-name CdkStack --language python --from-path cleaned_template.json

 

After that the template is under cdk_stack.

 

 

Run the code to see the cdk.out code generated.

source .venv/bin/activate
pip install -r CdkStack/requirements.txt 

cd CdkStack
cdk deploy 

 

 

Upgrading Node.js to the latest version Ubuntu

The module n makes version-management easy:

sudo npm install n -g

For the latest stable version:

n stable

For the latest version:

n latest