使用CloudFormation导入,条件表达式中带有`DeletionPolicy`的替代名称。

huangapple go评论55阅读模式
英文:

Use CloudFormation import with substitution name in conditional expression with `DeletionPolicy`

问题

其中一个我的AWS CloudFormation模板导出了一个基于堆栈名称的“stage”值(可以是例如“dev”或“prod”),如下所示:

```yaml
Export:
  
      Name: !Sub "${AWS::StackName}:stage"

堆栈名称的形式为foo-${Env},其中Env是诸如“bar”之类的值。此Env参数传递给另一个CloudFormation模板。另一个模板应该能够像这样访问该Env的阶段,使用外部导出:

      SomeResourceProperty:
        Fn::ImportValue:
          !Sub "foo-${Env}:stage"

例如,如果将Env参数设置为“bar”,则会引入外部变量foo-bar:stage。这没问题,它可以正常工作。我使用这种形式从第一个堆栈中导入各种变量。

我想基于此变量创建一个条件,以便根据阶段设置资源的保留期限。首先,我尝试了以下操作:

Conditions:
  IsStageProd: !Equals
    - Fn::ImportValue:
        !Sub "foo-${Env}:stage"
    - "prod"

CloudFormation抱怨告诉我它不能在条件中使用导入的值。

没关系,我会在我的!If函数中直接导入该值。根据删除策略和UpdateReplacePolicy属性中的内在函数引用,我添加了AWS::LanguageExtensions转换:

---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::LanguageExtensions


然后我尝试设置日志组的删除策略:

    DeletionPolicy:
      Fn::If:
        - Fn::Equals:
            - Fn::ImportValue:
                !Sub "foo-${Env}:stage"
            - "prod"
        - Retain
        - Delete

这次CloudFormation崩溃了:

> Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: For expression "Status" we matched expected path: "FAILED" Status: FAILED. Reason: Transform AWS::LanguageExtensions failed with: Fn::If layout is incorrect

当依赖于传递给当前模板的参数的变量的名称时,如何根据从另一个堆栈导入的变量的值有条件地保留资源?

我已经提交了在带有DeletionPolicy的条件表达式中使用CloudFormation导入并替换名称。#126


<details>
<summary>英文:</summary>

One of my AWS CloudFormation templates exports a value for &quot;stage&quot; (which can be e.g. &quot;dev&quot; or &quot;prod&quot;) in a variable based upon the stack name, like this:

```yaml
Export:
  …
      Name: !Sub &quot;${AWS::StackName}:stage&quot;

The stack name is in the form foo-${Env}, where Env is some value such as "bar". This Env param is passed to another CloudFormation template. The other template should be able to access the stage of that Env like this, using the external export:

      SomeResourceProperty:
        Fn::ImportValue:
          !Sub &quot;foo-${Env}:stage&quot;

For example if the Env parameter is set to "bar", this would pull in the external variable foo-bar:stage. And that's no problem. It works fine. I import all sorts of variables from the first stack using this form.

I would like to create a condition based upon this variable, in order to set the retention of a resource based upon the stage. First I try this:

Conditions:
  IsStageProd: !Equals
    - Fn::ImportValue:
        !Sub &quot;foo-${Env}:stage&quot;
    - &quot;prod&quot;

CloudFormation whimpers and tells me that it can't use imported values in conditions.

Never mind; I'll import the value directly in my !If function. As per Intrinsic function references in DeletionPolicy and UpdateReplacePolicy attributes, I add the AWS::LanguageExtensions transformation:

---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::LanguageExtensions


Then I try to set the deletion policy of a log group:

    DeletionPolicy:
      Fn::If:
        - Fn::Equals:
            - Fn::ImportValue:
                !Sub &quot;foo-${Env}:stage&quot;
            - &quot;prod&quot;
        - Retain
        - Delete

This time CloudFormation breaks down in tears:

> Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: For expression &quot;Status&quot; we matched expected path: &quot;FAILED&quot; Status: FAILED. Reason: Transform AWS::LanguageExtensions failed with: Fn::If layout is incorrect

How can I conditionally retain a resource based upon the value of a variable imported from another stack, when the name of the variable depends on a parameter passed to the current template?

I have filed Use CloudFormation import with substitution name in conditional expression with DeletionPolicy. #126.

答案1

得分: 0

这确实是一个限制。已经提交了一个改进请求,详见在条件中使用Fn::ImportValue #127

英文:

This is indeed a limitation. A ticket for improvement has been filed as Use Fn::ImportValue in Conditions #127.

huangapple
  • 本文由 发表于 2023年6月16日 02:50:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定