Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename instead of delete resource?

Is there any way to avoid resource deletion when reorganizing/renaming resources? Example: when I first implemented CloudFront Terraform it was an independent sub directory in my project, later I switched to using it as a module in. my root Terraform config but this caused Terraform to want to delete the old CloudFront distribution and create a new one:

Terraform will perform the following actions:
    - aws_cloudfront_distribution.main_site_distribution
    + module.cloudfront.aws_cloudfront_distribution.main_site_distribution

Is there any way to force Terraform to rename the resource instead?

like image 875
Jack Compton Avatar asked Mar 05 '18 13:03

Jack Compton


People also ask

How do you rename terraform resources without destroying them?

Execute the Terraform move command by passing your old resource name first, followed by your new resource name. Do not manually alter your terraform state, only use the CLI commands or you could end up breaking your state and Terraform setup.

Can you rename a resource in Azure?

Unfortunately, Azure does not support renaming an RG. This rename function request has been in limbo since 2014. If you've only created the RG and have not added resources to it yet, the quickest workaround is to delete the RG and create a new one.


1 Answers

Unfortunately Terraform doesn't know that you've renamed/moved the resource around but you could tell it where the resource should be stored in the state by using terraform state mv.

In your case if you ran:

terraform state mv aws_cloudfront_distribution.main_site_distribution module.cloudfront.aws_cloudfront_distribution.main_site_distribution

and then run another plan you should see no changes (or only the changes to the resource you have made as well as the move).

like image 178
ydaetskcoR Avatar answered Sep 18 '22 22:09

ydaetskcoR