Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Resource in CloudFormation Template

Can you rename a resource in a CloudFormation template?

Let's say I've created a stack template that creates a single lambda function.

GetTheFunnyPhraseText:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: ../Lambda/
    Handler: GetFunnyPhrase.handler
    FunctionName: GetFunnyPhrase
    Role: !GetAtt [ ExecuteFunctionGetFunnyPhrase, Arn ]

For whatever reason, I want to change the resource name GetTheFunnyPhraseText to GetFunnyPhrase. Is there a mechanism to change the name?

A couple things I tried...

  • Changing the resource name in the template. It looks like this acts like a delete on GetTheFunnyPhraseText and create of GetFunnyPhrase. Problem here is the resource creation happens before the resource deletion causing the action to fail because the Lambda function exists.
  • Create two drafts of the template. Draft-1: Change the function name for the existing resource(s). Draft-2: Delete the old resource (omit their definition from the template) and add the new resource. Execute the draft templates in sequence: 1st then 2nd. This works. It's just gross.

For folks that suggest not naming the function, understood; put a pin in that piece of feedback for the moment.

like image 810
Adam Avatar asked May 20 '19 14:05

Adam


2 Answers

Now that CloudFormation import is available it's technically possible to do this, although it's tedious.

Here's what you'd need to do:

  1. Update the definition for GetTheFunnyPhraseText to add DeletionPolicy: "Retain", upload to CloudFormation
  2. Remove GetTheFunnyPhraseText entirely from your template. Upload to CloudFormation. This will not actually delete the underlying Lambda because of the previously added DeletionPolicy
  3. Revert your template back to the previous state in Step #1 (add GetTheFunnyPhraseText back) and change the logical name to GetFunnyPhrase
  4. Start the "Stack Actions" > "Import resources into stack" workflow.
  5. Upload your reverted template (with the changed logical name, still including the DeletionPolicy)
  6. The import process will notice the new GetFunnyPhrase logical name and ask you what actual FunctionName should be mapped to that name. Provide the existing GetFunnyPhrase Lambda name and complete the import.
  7. Finally, you can re-upload your template and remove the DeletionPolicy

A tedious process for sure, but technically possible if you really don't want to delete the existing resource.

like image 186
Sean Anastasi Avatar answered Nov 08 '22 21:11

Sean Anastasi


No! Renaming a resource's logical name is not possible in Cloud Formation.

As you tested as well, CloudFormation sees it as the removal of the old resource and creation of the new one. This is so because the logical resource IDs are bound to the physical IDs of the resources by CloudFormation after creation. But for CloudFormation template language, it only recognizes the logical ID while parsing the template so any changes to that would mean the resource associated to it is going to be changed.

In some resource types though you can set the physical IDs yourself by using certain name properties which are resource-specific, for example a resource AWS::RDS::DBInstance may have a property DBInstanceIdentifier which will be the physical name of the db instance itself.

like image 29
Hassan Mussana Avatar answered Nov 08 '22 19:11

Hassan Mussana