Configure Lambda Function

Configure your lambda function to copy changed records from the Orders Kinesis Data Stream to the OrdersHistory table.

  1. Go to the IAM dashboard on the AWS Management Console and inspect the IAM policy, i.e. AWSLambdaMicroserviceExecutionRole…, created when you created the create-order-history-kds lambda function.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:DeleteItem",
                "dynamodb:GetItem",
                "dynamodb:PutItem",
                "dynamodb:Scan",
                "dynamodb:UpdateItem"
            ],
            "Resource": "arn:aws:dynamodb:{aws-region}:{aws-account-id}:table/*"
        }
    ]
}
  1. Update the policy statement by editing and replacing the existing policy using the following IAM policy statement.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kinesis:DescribeStream",
                "kinesis:GetRecords",
                "kinesis:GetShardIterator",
                "kinesis:ListStreams"
            ],
            "Resource": "arn:aws:kinesis:{aws-region}:{aws-account-id}:stream/Orders"
        },
        {
            "Effect": "Allow",
            "Action": "dynamodb:PutItem",
            "Resource": "arn:aws:dynamodb:{aws-region}:{aws-account-id}:table/OrdersHistory"
        },
        {
            "Effect": "Allow",
            "Action": "sqs:SendMessage",
            "Resource": "arn:aws:sqs:{aws-region}:{aws-account-id}:orders-kds-dlq"
        }
    ]
}

Replace {aws-region} and {aws-account-id} in the policy statement above with your AWS region and account ID respectively.

  1. Select Layers then select Add a Layer.

AWS Lambda function console

AWS Lambda function console

  1. Select Specify an ARN, enter the Lambda Layer ARN below.
1
arn:aws:lambda:{aws-region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:58
  1. Click Verify then select Add.

AWS Lambda function console

Replace {aws-region} with ID for the AWS region that you are currently working on.

  1. Go to the configuration section of the lambda console editor. Select Environment variables then select Edit.

AWS Lambda function console

  1. Add a new environment variable called ORDERS_HISTORY_DB and set its value to OrdersHistory.

AWS Lambda function console

  1. Go to the configuration section of the lambda console editor. Select Triggers then select Add trigger.
  2. Select Kinesis as the trigger source.
  3. Select the Orders DynamoDB table.
  4. Set the Batch size to 10 and leave all other values unchanged.

AWS Lambda function console

  1. Click Additional settings to expand the section.
  2. Provide the ARN of the orders-kds-dlq SQS queue you created earlier.
  3. Set the Retry attempts to 3.

AWS Lambda function console

  1. Select Add.