Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where should I add my custom resolver CloudFormation config? #1002

Closed
kstro21 opened this issue Mar 8, 2019 · 13 comments
Closed

Where should I add my custom resolver CloudFormation config? #1002

kstro21 opened this issue Mar 8, 2019 · 13 comments
Assignees
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 question General question

Comments

@kstro21
Copy link
Contributor

kstro21 commented Mar 8, 2019

I'm following the steps here https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function but in the steps that says: Add the function as an AppSync data source in the stack’s Resources block.. Ok, but, and the template? which one? amplify\backend\api\myapi\stacks\CustomResources.json or should I add a new one in _amplify\backend\api\myapi\stacks_? Obviously, it is not in the Lambda's template because of the parameters S3DeploymentBucket and S3DeploymentRootKey are not injected/passed to the Lambda's template, right?

I think the documentation is not clear about the process and should be updated accordingly.

@kaustavghosh06 kaustavghosh06 added question General question graphql-transformer-v1 Issue related to GraphQL Transformer v1 labels Mar 8, 2019
@mikeparisstuff
Copy link
Contributor

@kstro21 Thanks for the comment. You are able to add the resource to any stack in the stacks directory of the API project's stacks directory. You may add it at amplify\backend\api\myapi\stacks\CustomResources.json or amplify\backend\api\myapi\stacks\SomeOtherTemplate.json and both will be fed the parameters & uploaded to S3 on push. I will make a note to update the docs with more clear information in this regard.

@mikeparisstuff mikeparisstuff added the pending-response Issue is pending response from the issue author label Mar 8, 2019
@zjullion
Copy link
Contributor

zjullion commented Mar 8, 2019

https://aws-amplify.github.io/docs/cli/graphql#api-category-project-structure basically describes this structure, but it would be very useful if the documentation at https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function referenced the first link.

@kstro21
Copy link
Contributor Author

kstro21 commented Mar 9, 2019

Thanks, @mikeparisstuff I've tested both approaches and works.

@zjullion
Copy link
Contributor

zjullion commented Mar 9, 2019

Additionally, https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function is somewhat misleading / incomplete.

Firstly, the DataSource needs a DependsOn: DataSourceRole, and the Resolver needs a DependsOn: DataSource, otherwise the cloudformation template will fail to create.

Additionally, as noted in aws-amplify/amplify-category-api#440, since the cloudformation stacks are named so arbitrarily, getting the GraphQLApiId export from the parent stack is extremely difficult. You can get around this with the following:

"ApiId": {
          "Fn::ImportValue": {
            "Fn::Join": [
              ":",
              [
                {
                  "Fn::Select": [
                    0,
                    {
                      "Fn::Split": [
                        "-CustomResourcesjson-",
                        {
                          "Fn::Sub": "${AWS::StackName}"
                        }
                      ]
                    }
                  ]
                },
                "GraphQLApiId"
              ]
            ]
          }

but it's very much not ideal.

@mikeparisstuff
Copy link
Contributor

@zjullion You should be able to reference the ApiId directly from the stacks paramters. Here is an example:

The parameter definition that you can add to your template:

Example usage of the parameter:

@kstro21
Copy link
Contributor Author

kstro21 commented Mar 11, 2019

@mikeparisstuff but that only will work for templates inside amplify\backend\api\myapi\stacks\, If we want to get that value from a template of a Lambda function, we can't.

@zjullion
Copy link
Contributor

@mikeparisstuff thank you for that, it's much cleaner and simpler than what I was doing. Obviously, the AppSyncApiId parameter is being passed in to any stacks we create under myapi\stacks by Amplify. Is there a documented list anywhere of which parameters are passed in by amplify to custom stacks? Knowing that would be pretty useful.

@kstro21
Copy link
Contributor Author

kstro21 commented Mar 12, 2019

@zjullion yes, it's much cleaner and simpler for any stacks we create under myapi\stacks, but, if I'm writing a Lambda and I need the AppSyncApiId, how do I get it? It is not crystal clear. The export name is so random that we will need to amplify push, wait to finish, copy the export name, hardcode it in the CloudFormation template, then run amplify push again.

But then, if you want to create a new env, it becomes really hard to maintain. See the point? It would be really nice to have aws-amplify/amplify-category-api#440 but I started to note another issue, the order in which the stacks are created/updated is a mess. aws-amplify/amplify-category-api#440 will also require that some DependsOn be added to make sure we are not trying to get an export from stacks that are not created yet. It sounds like it could be another feature request :-)

CC: @mikeparisstuff

@amirmishani
Copy link

amirmishani commented Mar 12, 2019

@kstro21 Thanks for the comment. You are able to add the resource to any stack in the stacks directory of the API project's stacks directory. You may add it at amplify\backend\api\myapi\stacks\CustomResources.json or amplify\backend\api\myapi\stacks\SomeOtherTemplate.json and both will be fed the parameters & uploaded to S3 on push. I will make a note to update the docs with more clear information in this regard.

@mikeparisstuff Just out of curiosity these CFN templates have to be json or can they be written in YAML?

@cocacrave
Copy link

I'm also confused how to get AppSyncApiId in a function stack. It seems env parameter is being passed in by nested-cloudformation-stack.yml at path amplify/backend/awscloudformation/. Is it possible to pass AppSyncApiId parameter so we can easily use it in our lambda resolvers?

I'm trying to set it as lambda environment variable so I can use it to call the correct dynamodb table which uses this value to suffix the table name.

Is there another way to get this value currently? Please let me know, I'm stuck.

@kaustavghosh06 kaustavghosh06 removed the pending-response Issue is pending response from the issue author label Mar 29, 2019
@mikeparisstuff
Copy link
Contributor

@cocacrave Yes, if you define the parameter in your stack it will be passed down. See https://github.com/mikeparisstuff/amplify-cli-nested-api-sample/blob/7c4c7dec85b4f27e8344bea6063b4d85cf48771f/amplify/backend/api/amplifynestedsample/stacks/CustomResources.json#L5 for an example. The Amplify API category will automatically wire up your custom stacks to get this param when specified.

@ajenkins
Copy link

I agree that the docs for adding a custom resolver should include a link to https://aws-amplify.github.io/docs/cli-toolchain/graphql#api-category-project-structure . In fact, I really wish I had read about the API Category Project Structure a long time ago. Until I read that section, I had no understanding of what amplify push was actually doing or how it worked. I think it would be a good idea to make that article much more prominent than it is now. You have to go really deep in the docs past a lot of advanced topics to find it right now, but it really should be one of the first things you read when learning about Amplify.

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators May 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 question General question
Projects
None yet
Development

No branches or pull requests

7 participants