aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Custom Cloudformation Stacks to manage growing list of custom resolvers and its dependencies #2398

Closed andrew-aernos closed 5 years ago

andrew-aernos commented 5 years ago

Note: If your question is regarding the AWS Amplify Console service, please log it in the official AWS Amplify Console forum

Which Category is your question related to? Amplify CLI custom categories and resources

What AWS Services are you utilizing? Amplify, AppSync GraphQL custom resolvers, Cloudformation

Provide additional details e.g. code snippets Amplify version 3.9.0 Following the guidelines here https://aws-amplify.github.io/docs/cli-toolchain/quickstart#custom-cloudformation-stacks

My CustomResources.json is getting out of hand with all the custom resolvers, appsync datasource, the underlying dynamodb/lambda and its required iam service roles. I don't really know what is the best practice for managing this. The dependency is illustrated below:

aws-cloud-stack

Approach 1 - Package by type Define custom categories and resources, and defined dependsOn in the backend-config.json

amplify/backend

-/api
--/Api
---/stacks/CustomResources.json
--/Resolver
---/Resolvers
---api-resolver-template.json  (!) can't get reference to the needed "S3DeploymentBucket" and "S3DeploymentRootKey" parameters.

-/datasource
--/FooDataSource
---/foo-datasource-template.json

-/iam
--/FooServiceRole
---/foo-servicerole-template.json

-/storage
--/FooTable
---/foo-table-template.json

Problem with this approach: I'm stuck trying to get "S3DeploymentBucket" and "S3DeploymentRootKey" needed for the Resolvers. One weird way I'm trying is to get them output from the api's nested stack in stakcs/CustomResources.json but I can't seem to find documentation on how to reference output from a nested stack.

Approach 2 Package-by-feature Much more ideal and scalable, for each new feature it can independently define its own set of dependencies, datasource type and resolvers. for ex. User

image

Problem with this pattern is currently as of amplify 3.9.0, the appsync service doesn't bundle up everything and upload all the assets, making nested-stack reference impossible if it is organized this way by the developer client. Is there is a way to hook into the appsync service build process to have it bundle everything and load the asset to S3 ???

I'm short of just flattening the package-by-feature pattern to prefixes, which to me would be another absolute hell to deal with.

SwaySway commented 5 years ago

@andrew-aernos Are you looking to add the S3DeploymentBucket and S3DeploymentRootKey within the resolver? If so then one option would be to add these values inline when creating the resolver.

"RequestMappingTemplate": {
                    "Fn::Sub": [
                        { "Fn::Join": [
                            "\n",
                            [
                                "#set($S3DeploymentBucket = ${S3DeploymentBucket}",
                                "#set($S3DeploymentRootKey = ${S3DeploymentRootKey}"
                                "...rest of vtl logic..."
                            ]
                        ]},
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            }
                        }
                    ]
                }
SwaySway commented 5 years ago

Feel free to reopen/comment if you have further questions.

github-actions[bot] commented 3 years ago

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.