Page updated Jan 16, 2024

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:

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:

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:

1import AWS from 'aws-sdk';

Change to this:

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:

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):

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.

There are no existing build options for Python functions. The process of building and packaging Python functions is in line with Amazon's existing documentation for manually creating a Lambda deployment package which depends on a virtual environment.

Amplify will run pipenv install in your function's source directory during builds using either Pipenv's default virtual environment, or whichever virtual environment happens to be active. Then, during the packaging stage, the contents of the site-packages directory for that virtual environment will be zipped up along with the function-specific files.

The contents of the Python build can include local development dependencies (e.g. for testing) in addition to those necessary for your function to run. Packages installed as "editable" (using the -e flag) will not be packaged, as they are represented as an .egg-link file pointing to the local, editable code of the dependency.