dougmoscrop/serverless-plugin-split-stacks

Appsync and split strategy

NevRA opened this issue · 2 comments

NevRA commented

Hey. Thanks for the lib! I have a question about Appsync.

By default, all Appsync resources will be stored in the root stack, so If I have hundreds of resolvers it will not work for me.
If I use recommended approach from the serverless-appsync-plugin:

stacks-map.js:

module.exports = {
  'AWS::AppSync::ApiKey': { destination: 'AppSync', allowSuffix: true },
  'AWS::AppSync::DataSource': { destination: 'AppSync', allowSuffix: true },
  'AWS::AppSync::FunctionConfiguration': { destination: 'AppSync', allowSuffix: true },
  'AWS::AppSync::GraphQLApi': { destination: 'AppSync', allowSuffix: true },
  'AWS::AppSync::GraphQLSchema': { destination: 'AppSync', allowSuffix: true },
  'AWS::AppSync::Resolver': { destination: 'AppSync', allowSuffix: true }
}

It will not change anything, because all the same, resolvers and other things will be together

So the idea is to split this somehow into smaller chunks. I found this solution to work for me.

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]
}

But I'm wondering what the pitfalls are, will there be any problems with it in the future? And what strategy to use is safer?

For example with config:

nestedStackCount: 20
perFunction: false
perType: false
perGroupFunction: true

it will create some additional stacks to hold these new AppSync resources, and it is not clear whether this is a problem or not

Thanks!

@NevRA Where did you place that custom bit of code with the ServerlessPluginSplitStacks.resolveMigration? I am having the same problem but I don't know which file to put that piece of code into.

@dmr121 Same stacks-map.js 😉