CDK plus Importing Resources into a CloudFormation Stack

0

With respect to the 2020 How to import existing AWS resources into CDK stack article, there's a line in there that says "..AWS introduced importing mechanism for CloudFormation. Even though it’s not yet supported natively in CDK, there is a workaround.."

The author then does a cdk synth plus uses the AWS Console to perform a “Stack Actions” / “Import resources into stack".

Similar to how the author puts it, is there a more native CDK approach than doing a synth and then leaving the CDK space to do an "Import resources into stack" through the AWS Console?

2 Answers
1

Hi,

The only approach I am aware of is to programmatically use methods to import existing resources. For example, to import an existing bucket in cdk you could use this:

const importedBucket = s3.Bucket.fromBucketName( this, 'imported-bucket', 'BUCKET_NAME', );

Or use fromBucketArn. Similarly methods starting with “from” on different resources can be used.

Hope it helps ;)

profile picture
EXPERT
answered a year ago
  • Kind of you to respond so promptly. As you may know, the “Stack Actions” / “Import resources into stack" as explained in the article is not the same kind of import as the fromBucket.

    Looks like we will need to follow the manual steps as outlined in the article which includes going into the "AWS Console > CloudFormation" and doing the “Stack Actions” / “Import resources into stack" using the Cloudformation-template output of the cdk synth

    Thanks for your time.

1

In general, you can import existing resources provisioned manually into your CDK stack using the relevant AWS service modules. The process for importing a resource will vary depending on the service and resource type. However, the general steps are:

  • Identify the resource you want to import and its unique identifier. This could be an ID, ARN, or other identifier depending on the resource type.
  • Create a construct in your CDK stack that corresponds to the resource type. For example, if you want to import an S3 bucket, you would create an aws_s3.Bucket construct.
  • Use the relevant method in the AWS service module to import the resource. This could be a from_xxx_id method, a from_xxx_arn method, or another method depending on the resource type.
  • Use the imported resource as a property value or reference in your CDK stack.

Here's an example of importing an existing S3 bucket:

from aws_cdk import core
from aws_cdk import aws_s3 as s3

class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Import an existing S3 bucket
        existing_bucket = s3.Bucket.from_bucket_name(self, "ExistingBucket",
            bucket_name="my-existing-bucket"
        )

        # Use the imported bucket as a resource in your CDK stack
        my_resource = my_module.MyResource(self, "MyResource",
            some_property=existing_bucket.bucket_arn
        )

In this example, the Bucket.from_bucket_name method creates a Bucket construct from an existing S3 bucket with the specified bucket name. You can also import other resources, such as DynamoDB tables, Lambda functions, and more, using the relevant AWS service modules.

Once you've imported the resource, you can use its properties and methods as if it were defined in your CDK stack. In this example, the my_resource construct uses the imported bucket's ARN as a property value.

Note that when you import an existing resource, you need to make sure that it's in the same AWS account and region as your CDK stack. You also need to have the necessary permissions to access and modify the resource.

profile pictureAWS
answered a year ago
  • Thanks for your in-depth response. As noted in a comment that I made in response to another answer, the “Stack Actions” / “Import resources into stack" as explained in the article is not the same kind of import as the s3.Bucket.from_bucket_name

    However, hopefully your response will help someone who is looking for that kind of information. Thanks for your time.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions