How to overcome Number of resources is greater than maximum allowed, 200
Hi @sid88in I've been using this lovely plugin for a long time.
Recently, I am experiencing one annoying troubles to deploy as my backend is getting bigger and bigger day by day.
Since my AppSync service includes a lot of data sources(backed DynamoDB tables) and templates, I am getting this resource number limit issue.
I am wondering how we can split the AppSync service into multiple stacks. Of course, I've been trying to use serverless-plugin-additional-stacks or serverless-plugin-split-stacks etc. But they won't work with this plugin.
Can you guide me for this issue? Thanks
@harleyguru Yeah, the problem with appsync is that we cannot easily split it into several services like you would with Api gateway because, of course, there is only one endpoint.
I see 2 ways of doing this:
-
We change the plugin to be able to create data sources on an existing Appsync Resource. Basically by allowing overriding this and skipping this on sub-stacks. That way, you could have one "master"
serverless.ymlthat defines the appsync resource (Schema, Authentication, etc.) + a bunch of otherserverless.ymlfiles that just creates data-sources and attaches them to the API. -
Use Nested Stacks somehow... In one single
serverless.yml, we could maybe either detect the limit is reached (not sure how) and split automatically, or allow marking each Datasource as being part of a group (sub-stack)...
On the other hand, I don't know how you built your stack but you could start by moving some non-related resources in another stack: e.g: DynamoDb tables, Cognito User Pool, etc. You can still reference to them in your appsync stack using Outputs
Hello @bboure , First off, Thanks for your quick reply!
Yeah, I guess nested stacks will be a good solution for this problem. The main point of growing number of resources is in a lot of mapping-templates. In CFN console, I see a lot of template resources actually.
The question is if serverless-plugin-split-stacks(this plugin splits the stack into multiple nested stacks) will work for AppSync templates resources.
Thanks
@harleyguru I have not tested this plugin and I don't know either how it works. Does it throw an error or something? Also, maybe it has to do with the order of loading the plugins? My guess is that the split plugin should be loaded/executed last...
If I can I'll have a look
@bboure Thanks for your answer, yeah I also need to try with it. I will notice once it was successful!
@harleyguru my PR for supporting split stacks plugin has been merged and a new version of the appsync plugin has been released. Give it a try :)
Thanks so much for your help @kinyat Can you tell me a short guide how it works and help of splitting AppSync resources?
@harleyguru https://github.com/sid88in/serverless-appsync-plugin/blob/master/README.md#split-stacks-plugin
@kinyat Thanks so much my friend. I've already followed and built the service successfully. It was great PR 👍
One more question. let's say my AppSync service has 300 resolvers, then resolver type of resources will be divided into multiple nested service by split plugin?
I love you.
@harleyguru Yep. It will do it behind the scenes for you.
@kinyat Do you have an example project with this working? I haven't had any luck with this and have tried many different variations. It usually ends up with a circular dependency error.
@affablebloke sorry to hear your troubles, but everything is working for me by following @kinyat 's guide?
I was having all sorts of trouble with the default settings for the Split Stacks Plugin. The trouble could have been related to AWS Cognito event handlers. Anyways, I ended up rolling my own stacks-map.js and it works fine.
I'm not sure if there is some sort of hook that lets you know the number of resources per type. If so, you could make the bin packing dynamic. For now, 10 bins is enough for our project. This will fix the issue with CF Resources greater than 200 and the CF 60 output parameters issue.
As a side note, the amplify-cli project was also experiencing these same issues (https://github.com/aws-amplify/amplify-cli/pull/115). It seems as though AppSync, with its many configuration resources, will push CloudFormation to its limits 😞.
My custom stacks-map.js:
const ServerlessPluginSplitStacks = require('serverless-plugin-split-stacks');
const NUMBER_OF_BINS = 10;
let COUNT = 0;
ServerlessPluginSplitStacks.resolveMigration = function (resource, logicalId, serverless) {
COUNT += 1;
const bin = COUNT % NUMBER_OF_BINS;
if (resource.Type == 'AWS::AppSync::Resolver'){
return { destination: `GraphQLResolverStack${Math.abs(bin)}`, allowSuffix: true};
}
if(resource.Type == 'AWS::AppSync::FunctionConfiguration'){
return { destination: `FunctionConfigurationStack${Math.abs(bin)}`, allowSuffix: true};
}
// Fallback to default:
return this.stacksMap[resource.Type];
};
@affablebloke what do you think about adding AWS::AppSync::DataSource to your custom script? I want to use the approach below, but wondering what are the pitfalls Also, what strategy are you using with it?
const ServerlessPluginSplitStacks = require('serverless-plugin-split-stacks')
const NUMBER_OF_BINS = 10
let COUNT = 0
ServerlessPluginSplitStacks.resolveMigration = function (resource, logicalId, serverless) {
COUNT += 1
const bin = COUNT % NUMBER_OF_BINS
if (resource.Type == 'AWS::AppSync::Resolver'){
return { destination: `GraphQLResolverStack${Math.abs(bin)}`, allowSuffix: true }
}
if (resource.Type == 'AWS::AppSync::FunctionConfiguration'){
return { destination: `FunctionConfigurationStack${Math.abs(bin)}`, allowSuffix: true }
}
if (resource.Type == 'AWS::AppSync::DataSource'){
return { destination: `GraphQLDataSourceStack${Math.abs(bin)}`, allowSuffix: true }
}
if (resource.Type == 'AWS::AppSync::GraphQLApi'){
return { destination: `GraphQLApiStack${Math.abs(bin)}`, allowSuffix: true }
}
if (resource.Type == 'AWS::AppSync::GraphQLSchema'){
return { destination: `GraphQLSchemaStack${Math.abs(bin)}`, allowSuffix: true }
}
// Fallback to default:
return this.stacksMap[resource.Type]
}