Page updated Nov 11, 2023

Build options

In some cases, it might be necessary to execute a script before a function is deployed, e.g. to transpile Typescript or ES6 with Babel or with tsc into a format that is supported by the AWS Lambda's node runtime. amplify push will look for a script definition in the project root's package.json with the name amplify:<resource_name> and run it right after npm install is called in the function resource's src directory.

Example: Transpiling Typescript code with TSC

Make sure you have the tsc command installed globally by running npm install -g typescript or locally by running npm install --save-dev typescript

Let's say, a function resource has been created with amplify function add and it is called generateReport. The ES6 source code for this function is located in amplify/backend/function/generateReport/lib and the resource's src directory only contains the auto-generated package.json for this function. In order to compile TypeScript, you have to add the following script definition to your project root's package.json:

{ "scripts": { "amplify:generateReport": "cd amplify/backend/function/generateReport && tsc -p ./tsconfig.json && cd -" }, }
1{
2 "scripts": {
3 "amplify:generateReport": "cd amplify/backend/function/generateReport && tsc -p ./tsconfig.json && cd -"
4 },
5}

Navigate into amplify/backend/function/generateReport and create tsconfig.json then add the following to it:

{ "compilerOptions": { "allowSyntheticDefaultImports": true, "lib": ["dom", "esnext"], "module": "commonjs", "moduleResolution": "node", "skipLibCheck": true, "resolveJsonModule": true, "outDir": "./src", "baseUrl": "./", "rootDir": "./lib", "paths": { "src": ["./lib"] } } }
1{
2 "compilerOptions": {
3 "allowSyntheticDefaultImports": true,
4 "lib": ["dom", "esnext"],
5 "module": "commonjs",
6 "moduleResolution": "node",
7 "skipLibCheck": true,
8 "resolveJsonModule": true,
9 "outDir": "./src",
10 "baseUrl": "./",
11 "rootDir": "./lib",
12 "paths": {
13 "src": ["./lib"]
14 }
15 }
16}

Note: It is important to note that if you are using aws-sdk in your TypeScript file, you will get a timeout if you attempt to import it with the following:

import AWS from 'aws-sdk';
1import AWS from 'aws-sdk';

Change to this:

import * as AWS from 'aws-sdk';
1import * as AWS from 'aws-sdk';

Once you run amplify push, the amplify:generateReport script will be executed, either by yarn or by npm depending on the existence of a yarn.lock file in the project root directory.

Example: Transpiling ES6 code with Babel

Let's say, a function resource has been created with amplify function add and it is called generateReport. The ES6 source code for this function is located in amplify/backend/function/generateReport/lib and the resource's src directory only contains the auto-generated package.json for this function. In order to run Babel, you have to add the following script definition and dev dependencies to your project root's package.json:

{ "scripts": { "amplify:generateReport": "cd amplify/backend/function/generateReport && babel lib -d src && cd -" }, "devDependencies": { "@babel/cli": "^7.5.5", "@babel/preset-env": "^7.5.5", } }
1{
2 "scripts": {
3 "amplify:generateReport": "cd amplify/backend/function/generateReport && babel lib -d src && cd -"
4 },
5 "devDependencies": {
6 "@babel/cli": "^7.5.5",
7 "@babel/preset-env": "^7.5.5",
8 }
9}

Babel needs to be configured properly so that the transpiled code can be run on AWS Lambda. This can be done by adding a .babelrc file to the resource folder (amplify/backend/function/generateReport/.babelrc in this case):

{ "presets": [ [ "env", { "exclude": ["transform-regenerator"], "targets": { "node": "10.18" } } ] ], "plugins": [ "transform-async-to-generator", "transform-exponentiation-operator", "transform-object-rest-spread" ] }
1{
2 "presets": [
3 [
4 "env",
5 {
6 "exclude": ["transform-regenerator"],
7 "targets": {
8 "node": "10.18"
9 }
10 }
11 ]
12 ],
13 "plugins": [
14 "transform-async-to-generator",
15 "transform-exponentiation-operator",
16 "transform-object-rest-spread"
17 ]
18}

Once you run amplify push, the amplify:generateReport script will be executed, either by yarn or by npm depending on the existence of a yarn.lock file in the project root directory.