Amazon Web Services Feed
AppSync: how to inject table names into DynamoDB batch & transact operations

When working with CloudFormation, AWS recommends not to give explicit names to resources and let CloudFormation name them for you. This has several advantages:

  • It’s harder for attackers to guess resource names such as S3 buckets or DynamoDB tables.
  • You can deploy the same stack multiple times to the same account. This is useful when you use temporary stacks for developing feature branches or for running end-to-end tests in your CI pipeline.

However, this makes it harder for you to use AppSync’s batch or transact DynamoDB operations.

I had this exact problem on a recent project where I built the backend of a new social network in AppSync.

One way to get around this problem is to introduce some external build step to transform the VTL templates.

Alternative, you can also wrap the RequestTemplate and ResponseTemplate in a Fn::Sub so you can reference the DynamoDB tables’ logical IDs in the VTL templates themselves.

On this project, I used the Serverless framework with the excellent serverless-appsync-plugin. I didn’t want to introduce an extra build step to my CI pipeline so I chose the 2nd approach.

Since the resolvers and pipeline functions are generated by the serverless-appsync-plugin, I couldn’t wrap them in a Fn::Sub directly. But, as is so often the case, the solution is to create a Serverless framework plugin to manipulate the generated CloudFormation.

In this case, I created a local plugin called VtlPseudoParameters as below.

And added it to the list of plugins for the project.

plugins: - serverless-appsync-plugin - vtl-pseudo-parameters - serverless-plugin-split-stacks

However, this project is non-trivial. It had ~150 resolvers amidst a total of ~500 CloudFormation resources. To mitigate the 200 resources limit on CloudFormation, I also use the serverless-plugin-split-stacks to split the main stack into several nested stacks.

So, the local plugin needs to run AFTER the serverless-appsync-plugin, but BEFORE the serverless-plugin-split-stacks splits up the stack. Which is why its position in the plugins list matters. Also, it needs to transform the generated CloudFormation template on the same hook that both serverless-appsync-plugin and serverless-plugin-split-stacks uses.

With the help of this simple plugin, I can reference the logical ID of my DynamoDB table and CloudFormation would take care of substituting them to the generated table names.

A word of warning though. This is a quick fix. The reason I didn’t publish this as an NPM package is that it can’t function as a standalone plugin. Not only does it depend on the serverless-appsync-plugin, but it also needs to be positioned after the serverless-appsync-plugin in the plugins array.

The right thing to do here is to incorporate this behaviour into the serverless-appsync-plugin plugin itself. Unfortunately, I haven’t had the time to look into it and create the PR myself. If you’re reading and you’re able to do that, then please go ahead and create the PR in my stead. Otherwise, I’ll get to it when I have a moment.

In the meantime, this is a bandaid that makes DynamoDB batch and transact operations a little easier to work with in AppSync.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.

Hi, my name is Yan Cui. I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

You can contact me via Email, Twitter and LinkedIn.

Hire me.

The post AppSync: how to inject table names into DynamoDB batch & transact operations appeared first on