0
votes

I am using code similar to below to trigger an AWS Lambda function on my AWS educate account, when running this nothing triggers on the lambda (the lambda works with the same payload through the test configuration). My session and permissions are also correct as I am able to use boto3 to access S3 resources with the same credentials. What can I try to attempt to fix/troubleshoot this issue?

Apologies if this is vague (I know it is) but I am very confused on why this is happening

import boto3
import json

AWS_ACCESS_KEY_ID ="XXXXXXXXXXXXXXXXXX"
AWS_SECRET_ACCESS_KEY ="XXXXXXXXXXXXXXXXXXXXXXXXXX"
REGION = 'us-east-1'
session = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

payload = json.dumps({"function":"tweets","amount":10,"time":10})

client = boto3.client('lambda',
                        region_name=REGION,
                        aws_access_key_id=AWS_ACCESS_KEY_ID,
                        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                        aws_session_token = session)

response = client.invoke(
    FunctionName="MyFunctionARN",
    InvocationType="RequestResponse",
    Payload=payload
)
1
What are the errors, did you print out your response, print(response)? - Marcin
Please note that Invoking a function is different to Triggering a function. Invoking will directly cause the function to run. Triggering is used when an AWS service runs the function in response to an event (eg new file in S3 triggers the function). - John Rotenstein
check logs to see if it succeeded/failed docs.aws.amazon.com/lambda/latest/dg/… - Prabhakar Reddy
I have added a print response to the code and it now awaits a lambda response and successfully runs, after removing the print statement it has continued to work, I'm not sure what to post as the solution as it has started working with no changes? - Charlie

1 Answers

0
votes

Every lambda function has an handler function which is the entry for the code. By default it is lambda_handler. You can also change the default handler function under Runtime settings. The following code will solve your problem.

import boto3
import json

AWS_ACCESS_KEY_ID ="XXXXXXXXXXXXXXXXXX"
AWS_SECRET_ACCESS_KEY ="XXXXXXXXXXXXXXXXXXXXXXXXXX"
REGION = 'us-east-1'
session = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
def lambda_handler(event,context):
    payload = json.dumps({"function":"tweets","amount":10,"time":10})

    client = boto3.client('lambda',
                        region_name=REGION,
                        aws_access_key_id=AWS_ACCESS_KEY_ID,
                        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                        aws_session_token = session)

    response = client.invoke(
        FunctionName="MyFunctionARN",
        InvocationType="RequestResponse",
        Payload=payload
    )