The AWS Node.js execution runtime still uses Node 6, and many modern ES6 features are not available there (notably, imports and async/await). You can use Babel to transpile more modern ES6 code into ES2015.
This tutorial shows how to create a trivial transpiled project and deploy it using Claudia.
With transpiled code, ideally we want to keep both the version control system clean and the deployment package minimal.
We’ll put the more modern source in a single folder (for example
src), and transpile it into a different folder (in this example
The transpilation output folder (in this case bin) will be excluded from version control. In this tutorial, we’ll use
git, so the
.gitignore file should contain this line:
By default, Claudia will ignore everything excluded from version control. In this case, that’s not good. We want it to exclude the
src directory, or even better just include the
bin directory. Because Claudia just uses NPM to package the project, we can use the
files property of
package.json to override the contents:
Adding a simple API
Let’s create a trivial API with some unsupported ES6 features, for example using
import. Save the following content to
Remember to add the API builder as a production dependency:
Transpiling before deployment
The API uses language features which are not available on the AWS Lambda Node.js environment, so it will fail if we just deploy it to Lambda as it is. We need to automate transpilation now. First, add
babel as development dependencies:
Then let’s add the following scripts to
package.json to convert
bin before deployment:
Notice that we have added an additional script
transpile, triggering Babel transpilation.
That’s pretty much it. You can now just run
npm run create, and the transpiled code will be sent to AWS. To deploy a changed version, just use
npm run update.
Check out the full source code of this example on GitHub.