amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

DependsOn attribute in backend-config.json does not add DependsOn attributes between nested stacks.

Open gbumgard opened this issue 5 years ago • 5 comments

Not sure if this a usage error, defect or enhancement. The title is pretty self-explanatory.

I have an Amplify project with two custom resources (rds,sql) and a lambda function (rdsSqlDocumentLoader):

amplify/backend

├── amplify-meta.json
├── awscloudformation
│   └── nested-cloudformation-stack.yml
├── backend-config.json
├── function
│   └── rdsSqlDocumentLoader
│       ├── amplify.state
│       ├── dist
│       │   └── latest-build.zip
│       ├── function-parameters.json
│       ├── rdsSqlDocumentLoader-cloudformation-template.json
│       └── src
│           ├── event.json
│           ├── index.js
│           ├── package-lock.json
│           └── package.json
├── rds
│   └── exampledb
│       ├── parameters.json
│       └── template.yml
├── sql
│   └── exampledb
│       ├── parameters.json
│       └── template.yml
└── tags.json

I am trying to use a lambda-backed custom CloudFormation resource as a custom Amplify resource. In this case, a custom CF resource that executes SQL statements on an RDS database. This lambda function (rdsSqlDocumentLoader) services the CF create, update, and delete requests.

I created the "SQL" lambda function using the Amplify CLI. In this case, my "SQL" CloudFormation resource has a direct dependency on this lambda function.

I use another custom Amplify resource to create the RDS database. The "SQL" CloudFormation resource has a dependency on this custom resource too.

I add the custom resources and the resource as dependencies to backend-config.json. The file contents after all three resources have been created looks like this:


{
"rds": {
    "exampledb": {
      "service": "RDSCluster",
      "providerPlugin": "awscloudformation",
      "dependsOn": []
    }
  },
  "function": {
    "rdsSqlDocumentLoader": {
      "build": true,
      "providerPlugin": "awscloudformation",
      "service": "Lambda",
      "dependsOn": [
      ]
    }
  },
  "sql": {
    "exampledb": {
      "service": "Custom::RdsSqlResource",
      "providerPlugin": "awscloudformation",
      "dependsOn": [
        {
          "category": "rds",
          "resourceName": "exampledb",
          "attributes": []
        },
        {
          "category": "function",
          "resourceName": "rdsSqlDocumentLoader",
          "attributes": []
        }
      ]
    }
  }
}

When I add each resource to backend-config.json one at a time and push each individually, it all works as I can ensure the dependencies are satisfied.

This does not work if I create a new environment after all three resources have already been added to the backend - the nested stacks for all three resources appear start simultaneously and the push fails because the RDS cluster creation process takes much more time to complete than it does for the other two resources to reach the point where they need access to the database. The simultaneous start also makes it impossible to add dependencies on stack outputs as they do not yet exist.

I would expect that the dependencies described in the backend-config.json file would translate into DependsOn attributes in the nested-cloudformation-stack.yml stack generated by Amplify, but they do not.

Does Amplify have the ability generate true stack dependencies between backend resources?

-g.b.

gbumgard avatar Mar 04 '21 05:03 gbumgard

@gbumgard have you tried adding the dependsOn arrays to the resources in amplify-meta.json as well?

edwardfoyle avatar Mar 04 '21 18:03 edwardfoyle

@gbumgard have you tried adding the dependsOn arrays to the resources in amplify-meta.json as well?

The amplify env checkout adds them to amplify-meta.json, so yes, they are there.

gbumgard avatar Mar 04 '21 20:03 gbumgard

Hi @gbumgard I think the issue is that the CLI relies on the input parameters of one stack to be the outputs of another stack. CloudFormation will then realize that it needs to provision the dependency stack first in order to pass the outputs to the dependent stack. You may be able to achieve the behavior you want by assigning a dummy output in your dependency stack and declaring it as a dependency attribute in your dependent stack.

edwardfoyle avatar Mar 05 '21 23:03 edwardfoyle

Hi @gbumgard I think the issue is that the CLI relies on the input parameters of one stack to be the outputs of another stack. CloudFormation will then realize that it needs to provision the dependency stack first in order to pass the outputs to the dependent stack. You may be able to achieve the behavior you want by assigning a dummy output in your dependency stack and declaring it as a dependency attribute in your dependent stack.

I have added dependencies on outputs from the two stacks as you suggest, just as I had before. However, this time it worked when I generated a new environment rather than pushing an update for an existing environment. I have no clue why it wasn't working before - CF definitely was not respecting the dependencies. All good now. Thanks for the help!

OT: this was my first try at creating a custom lambda-backed CloudFormation resource. FWIW, I quickly learned you do not want to deploy broken code for lambda-backed CF resource - it is very difficult to terminate the stack creation/deletion process if the lambda function fails to send a response. I tried the recommended solution of using curl to manually send the HTTPS response, but never succeeded in getting that to work - I just had to wait for the CF timeout. Now I've wrapped everything int the handler function with a try-catch block that sends a SUCCESS response should any unexpected error arise.

gbumgard avatar Mar 07 '21 00:03 gbumgard

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 25 '21 01:05 github-actions[bot]