Using Babel with AWS Lambda

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.

Setting up

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 bin).

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:

  "name": "using-babel",
  "version": "1.0.0",
  "files": [ "bin" ],
  "license": "MIT"

Adding a simple API

Let’s create a trivial API with some unsupported ES6 features, for example using import. Save the following content to src/api.js.

import API from 'claudia-api-builder'

const api = new API()

api.get('/', () => 'Hello')

module.exports = api

Remember to add the API builder as a production dependency:

npm install claudia-api-builder -S

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 claudia and babel as development dependencies:

npm install babel-cli babel-preset-es2015 claudia -D

Then let’s add the following scripts to package.json to convert src to bin before deployment:

  "scripts": {
    "transpile": "babel --presets es2015 src --out-dir bin",
    "create": "npm run transpile && claudia create --region us-east-1 --api-module bin/api",
    "update": "npm run transpile && claudia update"

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.

Did you like this tutorial? Get notified when we publish the next one.

Once a month, high value mailing list, no ads or spam. (Check out the past issues)