How to Amplify: Creating a new Function - a2n-seattle/rms-app GitHub Wiki

  1. Create new api function in amplify/ts-code/src/api/SomeApiFunction.ts. See other APIs as examples. Create corresponding unit tests in amplify/ts-code/__tests__/unit/api/SomeApiFunction.test.ts

  2. Create new handler function in amplify/ts-code/src/handlers/api/SomeHandlerFunction.ts, in the following format: (See other handlers as a example)

import { Handler } from "aws-lambda"
import { DBClient } from "../../injection/db/DBClient"
import { MetricsClient } from "../../injection/metrics/MetricsClient"
import { apiHelper } from "./APIHelper"

export const handler: Handler = async (event: SomeApiFunctionInput): Promise<string> => {
    return apiHelper((dbClient: DBClient, metricsClient: MetricsClient) => new <SomeApiFunction>(dbClient, metricsClient).execute(event))
}

Create corresponding unit test for the handler in amplify/ts-code/__tests__/unit/handlers/api/SomeHandlerFunction.test.ts

  1. Create new integration test case for the handler in amplify/ts-code/__tests__/integration/Amplify.test.ts

  2. Run amplify function add in terminal, with the following params:

? Select which capability you want to add: Lambda function (serverless function)
? Provide an AWS Lambda function name: SomeFunctionName
? Choose the runtime that you want to use: NodeJS
? Choose the function template that you want to use: Hello World

? Do you want to configure advanced settings? Yes
? Do you want to access other resources in this project from your Lambda function? Yes
? Select the categories you want this function to have access to. storage
? Storage has 8 resources in this project. Select the one you would like your Lambda to access batch, history, items, mai
n, schedule, tags
? Select the operations you want to permit on batch create, read, update, delete
? Select the operations you want to permit on history create, read, update, delete
? Select the operations you want to permit on items create, read, update, delete
? Select the operations you want to permit on main create, read, update, delete
? Select the operations you want to permit on schedule create, read, update, delete
? Select the operations you want to permit on tags create, read, update, delete

? Do you want to invoke this function on a recurring schedule? No
? Do you want to enable Lambda layers for this function? No
? Do you want to configure environment variables for this function? No
? Do you want to configure secret values this function can access? No
✔ Choose the package manager that you want to use: · NPM
? Do you want to edit the local lambda function now? No
  1. Delete amplify/backend/function/SomeFunctionName/src/event.json and amplify/backend/function/SomeFunctionName/src/index.js

  2. Modify the following in amplify/backend/function/SomeFunctionName/src/package.json

Modify:

  "main": "./ts-output/handlers/api/SomeHandlerFunction.js"

Add at the bottom of the file:

Note: this is needed for DynamoDb APIs right now. Long term, we're going to migrate off of the old APIs.

  "dependencies": {
    "aws-sdk": "^2.1692.0"
  }

Remove:

  "devDependencies": {
    ...
  }
  1. Modify the following in amplify/backend/function/SomeFunctionName/SomeFunctionName-cloudformation-template.json

Modify in Resources.LambdaFunction.Properties.Handler:

        "Handler": "./ts-output/handlers/api/SomeHandlerFunction.handler"

Add in Resources.AmplifyResourcesPolicy.Properties.PolicyDocument.Statement (Should be the last element in the array, last part of the Resources part of the json before the Outputs section):

Note: this might be moved to the custom-policies.json file in the future.

            {
              "Effect": "Allow",
              "Action": [
                "cloudwatch:PutMetricData"
              ],
              "Resource": "*"
            }
  1. Add API name to const API_NAMES in backend-build.js file.

Can test with npm run build, to see if ts-output gets copied over to amplify/backend/function/SomeFunctionName/src

  1. Push the Pull Request! Once the PR goes through, then the GitHub Action Backend CD will push your new function to AWS!

  2. After request is merged, track deployment, make sure that deployment is successful and integration tests successfully run.

⚠️ **GitHub.com Fallback** ⚠️