4

The amplify docks here says that we can configure a lambda function as a dynamodb trigger by running **amplify add function** and selecting the "Lambda Trigger" option, but when I run the "amplify add api" (selected Python as runtime language) I am not getting the lambda trigger option, I'm only getting the "Serverless function" and "lambda layer" options.

Please help me to resolve this issue to access the feature.

docs snapshot - showing 4 options enter image description here

my CLI snapshot - showing only 2 options enter image description here

I know it works for nodejs runtime lambda, but I want this option for Python Lambda as well.

Jagadeesh
  • 119
  • 2
  • 7
  • It is showing the "Lambda trigger" option when NodeJS is selected, but I want that option for the Python lambda function. For now, we have manually added the trigger by editing the CF template generated by amplify framework. – Jagadeesh Aug 23 '21 at 08:47

4 Answers4

5

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:

  • <YourFunctionsName>

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

  • > Lambda Trigger

What event source do you want to associate with the lambda trigger

  • > Amazon DynamoDB Stream

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.

Sam Jett
  • 710
  • 1
  • 6
  • 15
0

Doesn't appear to be available anymore in the CLI codebase - see Supported-service.json deleted and replaced by supported-services.ts

https://github.com/aws-amplify/amplify-cli/commit/607ae21287941805f44ea8a9b78dd12d16d71f85#diff-a0fd8c5607fd81977cb4745b9af3af2c6649ded748991bf9968a7d782b000c6b

https://github.com/aws-amplify/amplify-cli/commits/4e974007d95c894ab4108a2dff8d5996e7e3ce25/packages/amplify-category-function/src/provider-utils/supported-services.ts

user7660047
  • 167
  • 9
0

Select nodejs and you will be able to view lambda trigger

  • 1
    yeah I know that, I even mentioned the same in one of the above comments. I am actually looking for the Python lambda setting only, as of now did that manually by adding into cloudformation template. – Jagadeesh May 18 '21 at 10:25
0

just add the following to {YOUR_FUNCTION_NAME}-cloudformation-template.json, remember to replace (YOUR_TABLE_NAME) to your table name.

"LambdaTriggerPolicyPurchase": {
  "DependsOn": [
    "LambdaExecutionRole"
  ],
  "Type": "AWS::IAM::Policy",
  "Properties": {
    "PolicyName": "amplify-lambda-execution-policy-Purchase",
    "Roles": [
      {
        "Ref": "LambdaExecutionRole"
      }
    ],
    "PolicyDocument": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "dynamodb:DescribeStream",
            "dynamodb:GetRecords",
            "dynamodb:GetShardIterator",
            "dynamodb:ListStreams"
          ],
          "Resource": {
            "Fn::ImportValue": {
              "Fn::Sub": "${apilanguageGraphQLAPIIdOutput}:GetAtt:(YOUR_TABLE_NAME):StreamArn"
            }
          }
        }
      ]
    }
  }
},
"LambdaEventSourceMappingPurchase": {
  "Type": "AWS::Lambda::EventSourceMapping",
  "DependsOn": [
    "LambdaTriggerPolicyPurchase",
    "LambdaExecutionRole"
  ],
  "Properties": {
    "BatchSize": 100,
    "Enabled": true,
    "EventSourceArn": {
      "Fn::ImportValue": {
        "Fn::Sub": "${apilanguageGraphQLAPIIdOutput}:GetAtt:(YOUR_TABLE_NAME):StreamArn"
      }
    },
    "FunctionName": {
      "Fn::GetAtt": [
        "LambdaFunction",
        "Arn"
      ]
    },
    "StartingPosition": "LATEST"
  }
},

i got them by creating a dummy function using the template that shows up after you choose nodejs and checking compare its -cloudformation-template.json with my own function

maydna
  • 1