Cloudformation等待同一堆栈中的另一个资源创建

问题描述 投票:0回答:2

我正在创建一个AppSync cloudformation和一个仪表板,并且我注意到ApiId导出已在很长时间之后完成创建。

我像这样在appsync文件中创建输出:

    Outputs:
      GraphQlApiIdOutput:
        Description: Main GraphQl Api ID.
        Value:
          Fn::GetAtt:
            - GraphQlApi
            - ApiId
        Export:
          Name: GraphQlApiIdOutput

在另一个仪表板文件中:

CloudwatchDashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties:
    DashboardBody:
        {
          'Fn::Sub': ['{
            ...
            ,{ 
                ApiId: 
                  !ImportValue GraphQlApiIdOutput}]

但是,我得到了错误:No export named GraphQlApiIdOutput found. Rollback requested by user.解决此问题的唯一方法是先删除导入值,以使堆栈创建成功,然后添加导入值。我注意到有一个等待条件,但是没有任何资源是外部的。我还注意到,仅AWS::AutoScaling::AutoScalingGroup, AWS::EC2::Instance, and AWS::CloudFormation::WaitCondition支持创建策略。是否有一种方法等待Cloudformation中的资源创建,以便模板在同一堆栈中创建之前不会尝试使用输出值?

amazon-web-services amazon-cloudformation
2个回答
0
投票

是,请使用DependsOn Attribute

使用DependsOn属性,您可以指定在创建另一个特定资源之后。当您向资源添加DependsOn属性时,仅在创建DependsOn属性中指定的资源之后才创建该资源。

该属性指向同一堆栈中的另一个资源。如果资源A DependsOn资源B,则仅在资源B完成创建后才创建资源A。


0
投票

当您在代码中包含此内容时:

!ImportValue GraphQlApiIdOutput

这意味着来自Template 1的堆栈必须预先创建

因此通常您会执行以下操作:

  1. 部署Template 1
  2. 如果成功,则部署another template file

DependsOn用于相同堆栈中的资源,不用于不同堆栈WaitCondition同样适用,因为您无法在成功创建another template file堆栈之前从Template 1引用Template 1中的条件。

© www.soinside.com 2019 - 2024. All rights reserved.