Just followed these steps with amplify CLI version 4.50.2
.
To create a lambda function that is triggered by changes to a DynamoDB table, you can use the following command line actions, which are walked-through inside of the CLI after entering the below command:
amplify add function
Select which capability you want to add:
- ❯ Lambda function (serverless function)
Provide an AWS Lambda function name:
Choose the runtime that you want to use:
- > NodeJS # IMPORTANT: Must be NodeJS as of now, you can change this later by manually editing ...-cloudformation-template.json file inside function directory
Choose the function template you want to use
What event source do you want to associate with the lambda trigger
Choose a DynamoDB event source option
- >Use API category graphql @model backend DynamoDB table(s) in the current Amplify project
Choose the graphql @model(s)
- <Select any models (using spacebar) you want to trigger the function after editing>
Do you want to trigger advanced settings
- Y # IMPORTANT: If you are using a dynamodb event source based on a table defined by graphql schema, you will need to give this function read access to the
api
resource that contains the graphql schema that defines the table that drives the event
Do you want to access other resources in this project from your Lambda function?
- y # See above, select your api that contains the data model and make sure that the function has at least read access.
After this, the other options (layer, call scheduling) are up to you.
After creating the function via the above CLI options, you can change the "Runtime"
field inside the -cloudformation-template.json file inside function directory, eg if you want a python lambda function change the runtime to "python3.8". You will also need to create a file called index.py
inside your function's directory which has a handler(event, context)
function. See example below:
import json
def handler(event, context):
print("Triggered via DynamoDB")
print(event)
return json.dumps({'status_code': 200, "message": "Received from DynamoDB"})
After making these edits, you can run amplify push
and, if you open your fxn in the management console online, it should show an attached dynamoDB stream.