CloudFormation栈引用已删除资源,如何更新或移除该引用?
解决CloudFormation栈引用已删除资源的问题
方法一:通过变更集更新模板中的资源引用
- 先把CloudFormation模板里引用旧授权器的部分,替换成新授权器的ID
- 用CLI创建变更集:
aws cloudformation create-change-set --stack-name <你的栈名称> --template-body file://<更新后的模板路径> --change-set-name update-authorizer-reference - 执行变更集完成更新:
aws cloudformation execute-change-set --change-set-name update-authorizer-reference --stack-name <你的栈名称>
方法二:将手动创建的授权器导入CloudFormation栈管理
这个方法能让新授权器正式归栈管,避免后续状态不同步:
- 先导出当前栈的模板:
aws cloudformation get-template --stack-name <你的栈名称> > current-stack-template.json - 修改导出的模板,添加新授权器的资源定义(逻辑ID建议和旧的保持一致,减少改动)
- 执行导入命令,把手动创建的授权器纳入栈:
aws cloudformation import-resources --stack-name <你的栈名称> --template-body file://<修改后的模板文件> --resources-to-import '[{\"ResourceType\":\"AWS::ApiGateway::Authorizer\",\"LogicalResourceId\":\"<旧授权器的逻辑ID>\",\"ResourceIdentifier\":{\"RestApiId\":\"<你的API网关ID>\",\"AuthorizerId\":\"<新授权器ID>\"}}]'
方法三:强制同步CloudFormation栈的资源状态
如果只是控制台显示过时,可尝试触发栈状态刷新:
- 用CLI获取最新资源状态,确认栈的实际记录:
aws cloudformation describe-stack-resources --stack-name <你的栈名称> --logical-resource-id <授权器的逻辑ID> - 强制触发栈更新(即使模板无变更):
aws cloudformation update-stack --stack-name <你的栈名称> --template-body file://<当前模板路径> --no-fail-on-empty-changeset
关于授权器ID显示不同步的说明
CloudFormation控制台的Resources标签偶尔会有缓存延迟,以aws apigateway get-authorizers --rest-api-id <你的API网关ID>返回的结果为准。完成上述任一操作后,控制台的资源信息会自动同步到最新状态。
内容的提问来源于stack exchange,提问作者elena
相关产品推荐
相关产品推荐

