Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 云形成,在DependsOn上应用条件_Amazon Web Services_Amazon S3_Amazon Cloudformation - Fatal编程技术网

Amazon web services 云形成,在DependsOn上应用条件

Amazon web services 云形成,在DependsOn上应用条件,amazon-web-services,amazon-s3,amazon-cloudformation,Amazon Web Services,Amazon S3,Amazon Cloudformation,我需要做的任务是使CDN依赖于S3存储桶。但是我们想让它使用现有的bucket,而不是创建一个新的bucket 以下是我正在尝试的示例代码: "Parameters" : { "UseExistingBucket" : { "Description" : "Yes/No", "Default" : "yes", "Type" : "String", "AllowedValues" : [ "yes", "no" ] } }, "Conditions" :

我需要做的任务是使CDN依赖于S3存储桶。但是我们想让它使用现有的bucket,而不是创建一个新的bucket

以下是我正在尝试的示例代码:

"Parameters" : {
  "UseExistingBucket" : {
    "Description" : "Yes/No",
    "Default" : "yes",
    "Type" : "String",
    "AllowedValues" : [ "yes", "no" ]
  }
},
"Conditions" : {
  "CreateS3Resources" : {"Fn::Equals" : [{"Ref" : "UseExistingBucket"}, "no"]}
},
"Resources" : {
  "StaticBucket" : {
    "Type" : "AWS::S3::Bucket",
    "Condition" : "CreateS3Resources",
    "Properties" : {
      "BucketName" : { "Fn::Join": [ "-", [ "app",  { "Ref": "EnvType" }, "static" ] ] }
    },
    "DeletionPolicy": "Retain"
  },
  "MyStaticDistribution": {
    "Type": "AWS::CloudFront::Distribution",
    "Properties": {
      "DistributionConfig": {
        "Origins": [
          {
            "DomainName": {
              "Fn::If" : [
                "CreateS3Resources",
                { "Fn::Join": [ "-", [ "app",  { "Ref": "EnvType" }, "static" ] ] },
                {"Fn::GetAtt": [ "StaticBucket", "DomainName" ] }
              ]
            },
            "Id": "S3Origin",
          }
        ]
      }
    },
    "DependsOn": [{
      "Fn::If" : [
        "CreateS3Resources",
        { "Fn::Join": [ "-", [ "app",  { "Ref": "EnvType" }, "static" ] ] },
        ""
      ]
    }]
  }
}

如果需要,请向我建议更多详细信息(至少stackoverflow需要更多详细信息,但我没有指定任何:-p)

您可以使用包装在条件
Fn:if
中的
Fn:GetAtt
来完成此操作。使用Fn:GetAtt意味着依赖关系,所以CloudFormation在到达该函数后将自动等待,就像使用Dependerson一样

示例

下面的代码段通过有条件地检索尚未创建的嵌套堆栈的名称来说明这一点,但仅当条件UseNestedStack设置为true时才会这样做。如果UseNestedStack为false,它将不会等待,而是检索局部变量名

{
"Fn::If": ["UseNestedStack", {
    "Fn::GetAtt": ["NestedStack", "Outputs.Name"]
}, {
    "Ref": "LocalName"
}]
我怎么知道这个?(另一个例子)

不幸的是,没有官方文档正式说明这一点,但AWS告诉我这样做,在他们的代码示例中,您可以看到,当订单很重要时,他们使用Fn:GetAtt。我已经试过很多次了,每次都有效。在一个简单的堆栈上自己尝试一下。下面是来自AWS lambda示例的一些伪证明,我对其进行了调整并使用了自己的示例。如果AMI函数是在资源AMI info之后创建的,那么下面的堆栈可能无法工作,AMI info需要AMI函数的输出,因此AWS使用Fn:GetAtt将它们链接在一起。要查看此滚动到底部并查看资源AMIInfo,您将看到它通过fn:Gett引用AMIFunction。CloudFormation看到了这一点,并首先返回到AMI函数来创建它

"AMIInfoFunction": {
  "DependsOn":"SourceStack",
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
      "S3Bucket": { "Ref": "DeploymentBucket" },
      "S3Key": {"Fn::Join": [
        "",
        [
          {
            "Ref": "ApplicationName"
          },
          "/amilookup.zip"
        ]
      ]}
    },
    "Handler": "amilookup.handler",
    "Runtime": "nodejs",
    "Timeout": "30",
    "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
    "VpcConfig": {
      "SecurityGroupIds": [ {"Ref": "InstanceSecurityGroup"}],
      "SubnetIds": [ {"Ref":"PrivateSubnetA"},{"Ref":"PrivateSubnetB"} ]
    }
  }
},
"AMIInfo": {
  "Type": "Custom::AMIInfo",
  "Properties": {
    "ServiceToken": { "Fn::GetAtt" : ["AMIInfoFunction", "Arn"] },
    "StackName": { "Ref":"SourceStack" }
  }
}

更新:这一点现在已在相关堆栈的文档中记录(Fn::GetAtt)。感谢您在评论中指出这一点

在模板中,您不需要将DependsOn属性添加到MyStaticDistribution资源中,因为您已经有了对StaticBucket资源的引用

这在优化AWS CloudFormation Templates博客文章的指定依赖项部分中有记录:

当您需要CloudFormation等待配置一个资源,直到配置另一个资源时,您可以使用DependsOn属性。


还可以使用{“Ref”:“MyResource”}或{“Fn::GetAtt”:[“MyResource”,“MyAttribute”]}函数在元素之间引入引用。当您使用其中一个函数时,CloudFormation的行为就像您向资源添加了DependsOn属性一样。

对于yaml用户,您还可以使用:

Conditions:
  CreateConfigRecorder: !Equals [ !Ref ConfigRecorderExists, 'false' ]

Resource:
#my 1st AWS Resource
  ConfigRecorder: 
    Condition: CreateConfigRecorder
    Type: AWS::Config::ConfigurationRecorder
    *more codes below*

#added, since DependsOn: !If is not possible, trigger by WaitCondition if CreateConfigRecorder is true
#Hacks: https://garbe.io/blog/2017/07/17/cloudformation-hacks/
  ConfigRecorderWaitHandle: 
    Condition: CreateConfigRecorder
    DependsOn: ConfigRecorder
    Type: "AWS::CloudFormation::WaitConditionHandle"
#added, since DependsOn: !If is not possible, trigger by WaitCondition if CreateConfigRecorder is false
  WaitHandle: 
    Type: "AWS::CloudFormation::WaitConditionHandle"
#added, since DependsOn: !If is not possible
  WaitCondition: 
    Type: "AWS::CloudFormation::WaitCondition"
    Properties: 
      Handle: !If [CreateConfigRecorder, !Ref ConfigRecorderWaitHandle, !Ref WaitHandle]
      Timeout: "1"
      Count: 0
#my 2nd AWS Resource that requires DependsOn Attribute
  AWSConfigRule:
    Type: AWS::Config::ConfigRule
    DependsOn: WaitCondition #added, since DependsOn: !If is not possible
    *more codes below*

基本上,在运行CFN之前,如果我的第一个资源不存在,我的第二个资源只有DependsOn属性。我是从:

为了扩展上面@Usman Mutawakil的答案,可以在标记中使用GetAtt隐式地施加DependsOn条件

例如,在我们的例子中,我们使用lambda函数在一段时间后自动删除暂存堆栈。此func仅在暂存时部署,必须在堆栈中最后删除

  ShopCluster:
    Type: 'AWS::ECS::Cluster'
    Properties:
      ...
      Tags:
        - Key: ConditionalDependsOn
          Value: !If [IsStaging, !GetAtt DeleteShopStackLambda.Arn, Ignored]

是否有文档表明GetAtt将自动等待其他资源完成后再继续?doeiqts是个好问题。不幸的是,我还没有看到它的文档,但我相信这是真实的,因为它在AWS示例中的使用,我已经看到它工作了多次,以及AWS工程师指示我这样做。我在AWS中添加了一个lambda示例,其中它们以这种方式延迟创建。是的,一些官方文档会更干净。这是
的一个文档化特性!GetAtt
:@idoimaging感谢您指出这一点。我已将您的链接添加到答案。我不敢相信那是5年前的事了哈哈。