重命名 CloudFormation 模板中的资源 [英] Renaming Resource in CloudFormation Template

查看:27
本文介绍了重命名 CloudFormation 模板中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否重命名 CloudFormation 模板中的资源?

Can you rename a resource in a CloudFormation template?

假设我创建了一个堆栈模板来创建单个 lambda 函数.

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 ]

无论出于何种原因,我想将资源名称 GetTheFunnyPhraseText 更改为 GetFunnyPhrase.有没有改名的机制?

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

我尝试了几件事...

  • 更改模板中的资源名称.看起来这就像删除 GetTheFunnyPhraseText 和创建 GetFunnyPhrase.这里的问题是资源创建发生在资源删除之前,导致操作失败,因为 Lambda 函数存在.
  • 创建模板的两个草稿.Draft-1:更改现有资源的函数名称.Draft-2:删除旧资源(从模板中省略它们的定义)并添加新资源.按顺序执行草稿模板:第一个然后第二个.这有效.太恶心了.
  • 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.

推荐答案

不!在 Cloud Formation 中无法重命名资源的逻辑名称.

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

正如您所测试的那样,CloudFormation 将其视为移除旧资源并创建新资源.之所以如此,是因为创建后,CloudFormation 将逻辑资源 ID 绑定到资源的物理 ID.但是对于 CloudFormation 模板语言,它在解析模板时仅识别逻辑 ID,因此对其的任何更改都意味着与其关联的资源将被更改.

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.

在某些资源类型中,虽然您可以使用特定于资源的某些名称属性自己设置物理 ID,例如资源 AWS::RDS::DBInstance 可能具有属性 DBInstanceIdentifier 这将是数据库实例本身的物理名称.

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.

这篇关于重命名 CloudFormation 模板中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆