即使有条件,AWS CloudFormation 也会创建 EFS

即使有条件,AWS CloudFormation 也会创建 EFS

我有一个 CloudFormation 脚本,其内容如下:

"Parameters": {
   "optionalExistingEFSDrive": {
     "Description": "EFS drive to store client content. If left empty a new drive will be created automatically.",
     "Type": "String",
     "Default": ""
   }
},
"Conditions": {
   "CreateEFSDrive": { "Fn::Equals": [ { "Ref": "optionalExistingEFSDrive" }, "" ] },
},
"Resources": {
  "WebFileSystem": {
    "Type": "AWS::EFS::FileSystem",
    "Condition": "CreateEFSDrive",
    "DeletionPolicy": "Retain",
    "Properties": {
      "FileSystemTags": [
        {
          "Key": "Name",
          "Value": "WebFileSystem"
        }
      ],
      "PerformanceMode": "generalPurpose"
    }
  },
  "WebFileSystemMountTarget1": {
    "Type": "AWS::EFS::MountTarget",
    "Properties": {
      "SubnetId": {
        "Ref": "WebFileSystemSubnet1"
      },
      "SecurityGroups": [
        {
          "Ref": "WebFileSystemSecurityGroup"
        }
      ],
      "FileSystemId": {
        "Fn::If": [ "CreateEFSDrive", { "Ref": "WebFileSystem" }, { "Ref": "optionalExistingEFSDrive" } ]
      }
    }
  }
}

传入一个optionalExistingEFSDrive的值,该值是现有文件系统的文件系统id,则会创建一个新的文件系统,但挂载目标会正确挂载到提供的optionalExistingEFSDrive文件系统。

我遗漏了什么?我不想创建新的 EFS,我想重新使用现有的 EFS。

答案1

使用此模板(模板的最小化版本并转换为 YAML)时,我无法重现您描述的行为:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  WebFileSystemSubnet1:
    Type: String
  WebFileSystemSecurityGroup:
    Type: String
  OptionalExistingEFSDrive:
    Type: String
Conditions:
  CreateEFSDrive: !Equals 
    - !Ref OptionalExistingEFSDrive
    - ''
Resources:
  WebFileSystem:
    Type: 'AWS::EFS::FileSystem'
    Condition: CreateEFSDrive
    DeletionPolicy: Retain
    Properties:
      FileSystemTags:
        - Key: Name
          Value: WebFileSystem
      PerformanceMode: generalPurpose
  WebFileSystemMountTarget1:
    Type: 'AWS::EFS::MountTarget'
    Properties:
      SubnetId: !Ref WebFileSystemSubnet1
      SecurityGroups:
        - !Ref WebFileSystemSecurityGroup
      FileSystemId: !If 
        - CreateEFSDrive
        - !Ref WebFileSystem
        - !Ref OptionalExistingEFSDrive

因此,当我使用此模板并传入现有文件系统 ID 时,只会创建挂载目标,正如预期的那样。

相关内容