Joey Yi Zhao
Joey Yi Zhao

Reputation: 42500

How can I split the appsync into multiple stack?

I have a project to deploy a appsync API using this plugin (https://github.com/sid88in/serverless-appsync-plugin). And I am looking for a solution to split all infra. into multiple stacks (multiple serverless.yml file).

My project structure looks like:

main/serverless.yml
dataSources/serverless.yml
resolvers/serverless.yml
schema/serverless.yml

The main folder only deploys the Appsync instance and logging and authentication. It doesn't include any schema, resolvers etc.

And other folders each of which is to deploy schema, resolvers, dataSources to the Appsync deployed by the main folder. In these folders, they need to import the appsync infra in order to attach these resolvers.

That means there will be multiple cloudformation stacks created and using cross stack reference among them. I wonder how I can make this by using this plugin.

Upvotes: 0

Views: 698

Answers (1)

cyberwombat
cyberwombat

Reputation: 40104

In general you can export CF variables that you might need in other stacks using Output such as:

 resources:
   Resources:
     NotesTable:
       Type: AWS::DynamoDB::Table
       Properties:
         TableName: notes

 # ...

  Outputs:
    Value:
      Ref: NotesTable
    Export:
      Name: NotesTableName

And in another file read them like: 'Fn::ImportValue': NotesTableName

I am using examples from a wonderful source: https://serverless-stack.com/chapters/cross-stack-references-in-serverless.html

Your example is AppSync...

You can define your data sources such as DynamoDb in a dedicated stack and in the AppSync sources only need to reference them by name such as:

- type: AMAZON_DYNAMODB
  name: ItemSource
  description: Item table
  config:
    tableName: ItemTable // Whatever name you used in your DynamoDb stack

For lambdas you would use the Output/import. In resolvers:

- type: AWS_LAMBDA
  name: SomeLambdaSource
  config:
    functionName: someSource
    lambdaFunctionArn:
      Fn::ImportValue: SomeLambdaSource

In your lambda stack:

functions:
  someSource:
    name: SomeSource
    handler: src/handlers/someSource.handler
 

resources:
  Outputs:
    SomeSource:
      Value:
        Fn::GetAtt:
          - SomeSource
          - Arn
      Export:
        Name: SomeSource

Upvotes: 1

Related Questions