AppSync: how to inject table names into DynamoDB batch & transact operations
Tags: appsync, aws, serverless
Published: 2020-07-19 01:47:29

---
title: AppSync: how to inject table names into DynamoDB batch & transact operations
published: true
date: 2020-07-18 23:57:11 UTC
tags: AppSync,AWS,Serverless
canonical_url: https://theburningmonk.com/2020/07/appsync-how-to-inject-table-names-into-dynamodb-batch-transact-operations/
---

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/na5xw8cn4cxnfwhvmxyc.png)

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](https://dev.to/theburningmonk/why-you-should-use-temporary-stacks-when-you-do-serverless-2ai0) 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.

![](https://theburningmonk.com/wp-content/uploads/2020/07/img_5f138bf3d9fc1.png)

I had this exact problem on a recent project where I [built the backend of a new social network in AppSync](https://www.youtube.com/watch?v=evsz__BDprs).

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](https://github.com/sid88in/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.

---

**Update 22/07/2020**: Thanks to reader Owain McGuire for pointing out that the `serverless-appsync-plugin` has a built-in `substitutions` features which does this out-of-the-box, so you don’t need to write your own plugin after all. Somehow I managed to miss this completely :-/

Anyhow, to do this, you need to define a `substitutions` attribute under `custom.appsynclike` this:

```
substitutions:
  userTableName: !Ref UserTable
```

You will be able to reference this as `${userTableName}` in your VTL templates.

---

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

{% gist https://gist.github.com/theburningmonk/1d2c878bfb97519140da56c2ffc9dcb2 %}

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](https://github.com/dougmoscrop/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.

{% gist https://gist.github.com/theburningmonk/577a39409008d4831b78cdddf2ea7cdb %}

{% gist https://gist.github.com/theburningmonk/17073880b6c45b527e455b88c22104bf %}

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.

---

 ![](https://theburningmonk.com/wp-content/uploads/2019/10/img_5dab0c477fc72.png)

Hi, my name is **Yan Cui**. I’m an **[AWS Serverless Hero](https://aws.amazon.com/developer/community/heroes/yan-cui/)** and the author of [**Production-Ready Serverless**](https://bit.ly/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](mailto:theburningmonk.com), [Twitter](https://twitter.com/theburningmonk) and [LinkedIn](https://www.linkedin.com/in/theburningmonk/).

[Hire me.](https://theburningmonk.com/hire-me)

The post [AppSync: how to inject table names into DynamoDB batch & transact operations](https://theburningmonk.com/2020/07/appsync-how-to-inject-table-names-into-dynamodb-batch-transact-operations/) appeared first on [theburningmonk.com](https://theburningmonk.com).

 ![](http://feeds.feedburner.com/~r/theburningmonk/~4/yTQyc4rkptM)