DependsOn 和 Cloudformation 自定义资源

DependsOn and Cloudformation Custom Resources

根据我的理解,如果更新了依赖的资源,则应该更新指定了 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.

时也是如此

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

谢谢, 乔

似乎对DependsOn的用途有误解。

怎么回事

来自 CloudFormation DependsOn documentation

With the DependsOn attribute you can specify that the creation of a specific resource follows another. When you add a DependsOn attribute to a resource, that resource is created only after the creation of the resource specified in the DependsOn attribute.

您的 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