1

I am new to Cloud Programming, and need to trigger the Lambda function below (function name = dbscan-py) with a button-click in my HTML code but can't figure out how to, nor know how to debug to see if it's getting triggered.

import json
import boto3

dynamodb = boto3.client('dynamodb')

def dump_table(table_name):

results = []
last_evaluated_key = None
while True:
    if last_evaluated_key:
        response = client.scan(
            TableName=table_name,
            ExclusiveStartKey=last_evaluated_key
    )
    else: 
        response = dynamodb.scan(TableName=table_name)
        last_evaluated_key = response.get('LastEvaluatedKey')
        results.extend(response['Items'])
    if not last_evaluated_key:
        break
return results

def lambda_handler(event, context):
    table_rows=dump_table('log')
    # table_rows is an array/list of rows objects [{ },{},{}]
    text=repr(table_rows)
    # text is human readable in python
    return {
        'statusCode': 200,
        'body': text
    }

1 Answer 1

2
  1. Deploy the code to AWS or any other provider. Expose an API endpoint for the lambda function. AWS Guide here. (Optional - Enable cors for your APIs.)

  2. On your HTML page. Write an onClick handler for your button

    <button onclick='call_api_end_point()'>Click Me</button>
    
    <script type='text/javascript'>
       const api_url = '<your-api-end-point-url-for-the-lambda>';
       function call_api_end_point(){
         fetch(api_url)
         .then((resp) => resp.json())
         .then(function(data) {
            <handle your response here>
          })
       }
    </script> 
    

Further, there are more ways to invoke your lambda functions. The answer is written assuming you are looking for an API endpoint for your lambda service.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.