簡體   English   中英

AWS Cloudformation 有條件地添加資源屬性

[英]AWS Cloudformation conditionally add resource property

我正在將我們的 cloudformation 堆棧擴展到一個新區域,但希望在我們的 elasticsearch 集群中啟用加密。 但是,我只想為新區域啟用它

我試過這個:

Conditions:
  EnableEnhancedSecurity:
    !Not
      - !Equals
        - 'us-east-1'
        - { Ref: AWS::Region }

Resources:
  MyTestingElasticSearchStore:
    Type: "AWS::Elasticsearch::Domain"
    Properties:
      DomainName:
        Fn::Sub: 'stack-${AWS::Region}-${Stage}'
      ElasticsearchVersion: '7.1'
      ElasticsearchClusterConfig:
        EncryptionAtRestOptions:
            Enabled:
              !If
                - EnableEnhancedSecurity
                - 'true'
                - 'false'
          NodeToNodeEncryptionOptions:
            Enabled:
              !If
                - EnableEnhancedSecurity
                - 'true'
                - 'false'
         ...

但是當我嘗試更新我的測試堆棧時出現以下錯誤: CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename stack-us-west-1-test and update the stack again. CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename stack-us-west-1-test and update the stack again.

我認為這是因為我正在添加屬性(即使它們是“假”)所以我試圖了解如何有條件地添加屬性。

我在嘗試:


Conditions:
  EnableEnhancedSecurity:
    !Not
      - !Equals
        - 'us-east-1'
        - { Ref: AWS::Region }

Resources:
  MyTestingElasticSearchStore:
    Type: "AWS::Elasticsearch::Domain"
    Properties:
      DomainName:
        Fn::Sub: 'stack-${AWS::Region}-${Stage}'
      ElasticsearchVersion: '7.1'
      ElasticsearchClusterConfig:
         ...
      Fn::If:  # only add the properties if needed
        - EnableEnhancedSecurity
        -
          EncryptionAtRestOptions:
            Enabled: 'true'
          NodeToNodeEncryptionOptions:
            Enabled: 'true'
       - { Ref: AWS::NoValue }

但遇到以下錯誤:

YAML Errors:
while parsing a block mapping
  in "<unicode string>", line 76, column 5:
        Type: "AWS::Elasticsearch::Domain"
        ^
expected <block end>, but found '<block sequence start>'
  in "<unicode string>", line 148, column 6:
         - { Ref: AWS::NoValue }

這甚至可能嗎? 如果是這樣,我什至如何在不通過 cloudformation 觸及現有區域的情況下僅在新區域中進行設置?

發生錯誤是因為您使用DomainName為您的域提供了名稱 來自文檔

如果指定名稱,則無法執行需要替換此資源的更新 您可以執行不需要中斷或需要一些中斷的更新。 如果必須替換資源,請指定一個新名稱。

EncryptionAtRestOptionsEncryptionAtRestOptions都需要替換 這意味着由於您使用了DomainName ,因此您無法修改這些屬性。

您必須拆除當前的 ES 域,並在沒有DomainName的情況下重新創建它,才能進行替換更新。 如果您想使用不同的DomainName也可以重命名域來執行此操作,這也會刪除現有的 ES 域並創建新域。

條件更新

您應該能夠使用以下語法實現您想要的:

Resources:
  MyTestingElasticSearchStore:
    Type: "AWS::Elasticsearch::Domain"
    Properties:
      DomainName:
        Fn::Sub: 'stack-${AWS::Region}-${Stage}'
      ElasticsearchVersion: '7.1'
      ElasticsearchClusterConfig:
      EnableEnhancedSecurity:
        Fn::If: 
          - EnableEnhancedSecurity
          - { Enabled: 'true' }
          - !Ref "AWS::NoValue"
      EncryptionAtRestOptions:
        Fn::If: 
          - EnableEnhancedSecurity
          - { Enabled: 'true' }
          - !Ref "AWS::NoValue"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM