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...
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.For folks that suggest not naming the function, understood; put a pin in that piece of feedback for the moment.
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:
GetTheFunnyPhraseText
to add DeletionPolicy: "Retain"
, upload to CloudFormationGetTheFunnyPhraseText
entirely from your template. Upload to CloudFormation. This will not actually delete the underlying Lambda because of the previously added DeletionPolicy
GetTheFunnyPhraseText
back) and change the logical name to GetFunnyPhrase
DeletionPolicy
)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.DeletionPolicy
A tedious process for sure, but technically possible if you really don't want to delete the existing resource.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With