aws/aws-cdk-rfcs

Support resource import

NetaNir opened this issue ยท 24 comments

PR Champion
#392 @rix0rrr

Description

CloudFormation released resource import
How should we support it in the CDK?
See #84 for a related feature/discussion.

Progress

  • Tracking Issue Created
  • RFC PR Created
  • Core Team Member Assigned
  • Initial Approval / Final Comment Period
  • Ready For Implementation
  • Released As Feature Preview
  • Resolved (feature stable)

This looks like a duplicate of this issue

I am confused about the Importing Existing External Resources from the developer docs.

It looks likes it is a different thing than this, right?
It's not meant as a way to import a resource and manage it, but just a way to access its attributes. Conflated terminology?

This would be of great help, especially when getting to grips with the CDK and learning about AWS services. For example I've set up some proof-of-concept stuff to show my team, but now want to manage it via a CDK template. I'm probably going to have to destroy and recreate, but if i was able to run the cdk with an import flag on the deploy then I'd be much happier!

I am confused about the Importing Existing External Resources from the developer docs.

It looks likes it is a different thing than this, right?
It's not meant as a way to import a resource and manage it, but just a way to access its attributes. Conflated terminology?

Pulumi is more clear about it: they talk about referencing vs adopting existing resources https://www.pulumi.com/blog/adopting-existing-cloud-resources-into-pulumi/.

A clear distinction between referencing and importing resources will be needed in the cdk documentation, and I guess cdk will follow the naming used by cloudformation. I've just opened an issue aws/aws-cdk#7370 which may require such clarification.

@marcindulak has a very good point; the documentation describes a clear path to referencing external resources, which is definitely a useful feature, but the use case of being able to adopt them isn't. In the short term it would be good to clarify those terms in the documentation and indicate that adoption is not presently supported.

For my part, at least, I can't use CDK in my personal work until it does support that, since everything I'm doing in hobby terms both a) stores state and b) was initially created using Terraform :)

If you want to import existing resource into a new stack, I've had some success running cdk synth to get the CloudFormation templates, then using CloudFormation commands to import it. When it's done, you can go back to CDK updates. The only complication I had was needing to parse the CloudFormation template to get the logical ID it was using.

kennu commented

I wrote in my duplicate issue that I'd like AWS CDK to work as transparently as possible. For instance, if I delete a stack with named & retained resources, and then create the stack again, AWS CDK could automatically detect and import those retained resources back into the new stack, perhaps with a simple confirmation prompt. This happens to me personally usually with retained S3 buckets and DynamoDB tables.

This transparent orphan-adoption would make it simple to recreate stacks and also to split larger stacks that have hit the 200 resource limit. Sometimes you also want to move resources around just to refactor your architecture. At present stacks are pretty rigid if you have data in your buckets or tables, which discourages refactoring.

@kennu woah, that functionality would be absolutely transformative in how teams work with CDK!

Is there any plan to provide support for the requirement @kennu as requested for ?

The use case that brought me to this issue is DynamoDB recoveries. If I build a CDK application with DynamoDB in it, then have to restore the database, I can import the restored database by reference (CDK "import") or create a new DynamoDB table and migrate the data. In CloudFormation "import" I can bring in the restored table and still manage its structure.

anyone tried to import kinesis Delivery stream to Cloudformation using CDK ?

For example:

    const firehose = kinesisfirehose.CfnDeliveryStream.fromArn(myImportedFirehoseArn);
    
    const eventRule = new eventBus.Rule(this, `salesforce-eventbus-eventlake-rule-${this.id}`, {
      enabled: true,
      description: `Send all events to event lake for ${this.id}.`,
      ruleName: `salesforce-eventbus-eventlake-rule-${this.id}`,
      eventBus: bus,
      targets: [
        new eventsTarget.KinesisFirehoseStream(firehose)
      ],
      eventPattern: {
        source: [ { prefix: "" } ] as any[] // send all events to event lake
      }
    });

+1 This would helpful functionality when you restore an RDS DB Cluster and would like to adopt/import that new cluster into an existing stack. Something Iโ€™m working through now.

BDeus commented

+1 Same here, for S3 Bucket or any DB it will be amazing to have choice between create or import a resource if it exist.
For retain resources, when the cloudformation has been destroyed, it will be very helpfull.

This feature would be very useful. My use case is migrating resources created by Terraform to be managed by CDK.

Yes this feature is really missing in CDK. For example... if you have a SNS -> SQS -> Lambda . And your CDK manages the SQS and the lambda and for whatever reason you need to delete the cloudformation you surely would like the SQS to be there (in order not to miss any event). Then when recreating the cloudformation it should automatically import the SQS again.

How can we make sure this feature gets the right attention?

Worth noting that as of 2.20.0 and 1.152.0, this now exists in preview form! Only some docs at the moment.

When this is released will something like this work?

var existingBucket = Bucket.FromBucketName(this, "mybucketid", "mybucket");
if (existingBucket is null)
{
    new Bucket(
        this,
        "mybucketid",
        new BucketProps
        {
            BucketName = "mybucket",
            RemovalPolicy = RemovalPolicy.RETAIN,
        }
    );
}

The docs says to run cdk import.
I was hoping to just do something like the above snippet and then cdk deploy.

I don't see how anyone can live without this feature. As we know, things in SaaS go wrong, and they need to be fixed as fast as possible.

It makes for a very brittle stack if we can't easily create/update resources from the AWS console (e.g. restore an rds instance from a snapshot). CDK wants nothing to do with those if you don't modify your code. Furthermore, even if you modify the code to import them, they are not managed properly and are just unmanaged references.

Feature has been implemented for a while now. Sorry for the poor bookkeeping. Closing

@rix0rrr is there plans to bring the feature out of preview? As far as I'm aware, it's still only GitHub documented and has noted limitations.

@rix0rrr Could you provide some more details? It would be appreciated if you could link to the implementation, user guide, something before closing out the issue so the people subscribed to this issue could learn more. Thanks!

I can see it's implemented and merged to master aws/aws-cdk#17666
@magnoll-amzn @RichiCoder1