Search code examples
amazon-web-servicescloudaws-cloudformationaws-cdk

Importing Resources and Modifying them using Cloud formation/cdk


I am required to modify pre-existing resources using cdk. I understand that there are methods to call upon that allows you to import a given resource, but how can I go about modifying that resource? For example, I have an RDS that was manually created and I want to change the instance type after creation. How do I go about doing that using cdk/cloue formation?


Solution

  • If you'd like to 'take ownership' of a resource in AWS with CloudFormation you can follow the steps outline here. In short:

    1. Create a CloudFormation template that only has the one RDS resource you'd like to take ownership of. Have the template match the resource as much as possible. You should use the CDK to do this, just make sure your CDK code ONLY includes those resources you want to import. That can sometimes be tricky since L2 constructs often create more than just one CloudFormation resource. Trim back the synthesized CloudFormation template as much as needed to get just the one RDS resource you want to import.
    2. Create a new Stack using the 'import' option. It's important that the only resources in the template are resources you are trying to import and 'take ownership of'.
    3. Run Drift Detection and correct anything that is out of sync by updating your template and then running additional Update Stack steps.

    You can, of course, have the CDK generate this template. Same rules apply, though. You need to make sure you have only the RDS instance.

    Please refer to this post as I go into more detail there.

    Additionally, there is a command link option, cdk import which can help do this (not detailed here or in the blog, though).

    Once you have the resource imported into a stack you can continue making future changes using the CDK.