【问题标题】:DependsOn and Cloudformation Custom ResourcesDependsOn 和 Cloudformation 自定义资源
【发布时间】:2018-03-10 20:06:02
【问题描述】:

据我了解,如果更新了它所依赖的资源,则应该更新指定了 DependsOn 的资源。我在某些资源中看到了这一点,但它似乎不适用于自定义资源。

我正在使用 APIGateway 并尝试在与阶段相关的资源更新时使用自定义资源来部署阶段。这是因为包含的 AWS::ApiGateway::StageAWS::ApiGateway::Deployment 在需要部署更新时似乎不能很好地工作。

我有以下模板(为方便参考而截取):

<snip>
pipelineMgrStateMachine:
  Type: AWS::StepFunctions::StateMachine
  Properties:
    <snip>

webhookEndPointMethod:
  Type: AWS::ApiGateway::Method
  DependsOn: pipelineMgrStateMachine
  Properties:
    RestApiId: !Ref pipelineMgrGW
    ResourceId: !Ref webhookEndPointResource
    HttpMethod: POST
    AuthorizationType: NONE
    Integration:
      Type: AWS
      IntegrationHttpMethod: POST
      Uri: !Sub arn:aws:apigateway:${AWS::Region}:states:action/StartExecution
      Credentials: !GetAtt pipelineMgrGWRole.Arn
      PassthroughBehavior: WHEN_NO_TEMPLATES
      RequestTemplates:
        application/json: !Sub |
          {
            "input": "$util.escapeJavaScript($input.json('$'))",
            "name": "$context.requestId",
            "stateMachineArn": "${pipelineMgrStateMachine}"
          }
      IntegrationResponses:
        - StatusCode: 200
    MethodResponses:
      - StatusCode: 200

pipelineMgrStageDeployer:
  Type: Custom::pipelineMgrStageDeployer
  DependsOn: webhookEndPointMethod
  Properties:
    ServiceToken: !GetAtt apiGwStageDeployer.Arn
    StageName: pipelinemgr
    RestApiId: !Ref pipelineMgrGW
<snip>

当我更新pipelineMgrStateMachine 资源时,我看到webhookEndPointMethod 已更新,即使webhookEndPointMethod 中没有任何变化。正如预期的那样。

但是,pipelineMgrStageDeployer 没有更新。当我让pipelineMgrStageDeployer 直接依赖于pipelineMgrStateMachine 时,情况也是如此。

关于为什么自定义资源在更新它所依赖的资源时不更新的任何想法?还有其他可能有用的想法或见解吗?

谢谢, 乔

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    似乎对DependsOn 的用途存在误解。

    发生了什么

    来自 CloudFormation DependsOn documentation

    使用 DependsOn 属性,您可以指定特定资源的创建跟随另一个资源。将 DependsOn 属性添加到资源时,仅在创建 DependsOn 属性中指定的资源后才会创建该资源。

    您的webhookEndPointMethod 可能会在您的pipelineMgrStateMachine 更新时更新,原因是它在您的RequestTemplates 中具有隐式依赖

    "stateMachineArn": "${pipelineMgrStateMachine}"

    如何让您的自定义资源得到更新

    至于当状态管理器更新时如何让你的部署器自定义资源更新,你可以在你的自定义资源中添加一个你实际上并没有使用的属性,比如PipelineMgStateMachine: !Ref pipelineMgrStateMachine,例如:

    pipelineMgrStageDeployer:
      Type: Custom::pipelineMgrStageDeployer
      DependsOn: webhookEndPointMethod
      Properties:
        ServiceToken: !GetAtt apiGwStageDeployer.Arn
        StageName: pipelinemgr
        RestApiId: !Ref pipelineMgrGW
        PipelineMgStateMachine: !Ref pipelineMgrStateMachine
    

    【讨论】:

    • 感谢您的澄清,参考效果很好!我很困惑,因为我在 DependsOn 文档中看到了这条注释:在堆栈更新期间,依赖于更新资源的资源会自动更新。 AWS CloudFormation 不会对自动更新的资源进行任何更改,但是,如果堆栈策略与这些资源相关联,您的账户必须有权更新它们。 但显然这确实是我认为的意思。
    猜你喜欢
    • 1970-01-01
    • 2019-08-02
    • 2018-03-02
    • 2020-10-09
    • 2021-04-06
    • 2021-06-03
    • 2021-03-09
    • 2021-05-11
    • 2018-12-14
    相关资源
    最近更新 更多