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

 

 

No hay comentarios:

Publicar un comentario