0

I want to call this Lambda Function with a payload, for example a username that I will choose:

{
  "iamuser": "Joe"
}

I don't understand how the handler of a Lambda function works and how to create my event object in the handler in json. At each run I want to pass a different iamuser value for the Lambda.

import boto3
import botocore.exceptions
import json

iamuser = {}
client_iam = boto3.client('iam')

def create_user(iamuser):
   create_user = client_iam.create_user(UserName=iamuser)
 
def lambda_handler(event, context):
   try:
      client_iam.get_user(UserName=iamuser)
   except ClientError as error:
      if error.response["Error"]["Code"] == "NoSuchEntity":
         create_user(useriam)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
liyas
  • 7
  • 4
  • Have you checked the docs? – Victor Aug 18 '22 at 22:35
  • You don't create the `event` object -- it is passed into the Lambda function when it is invoked. If you want to pass some data to the Lambda function, you provide that as part of the `Invoke()` call (which is performed outside of Lambda). Thus, your code needs to run _somewhere else_, and then invoke the Lambda function with that information as an input. – John Rotenstein Aug 18 '22 at 23:54
  • This is what I don't understand. Why in this post: https://stackoverflow.com/questions/40676617/how-to-pass-and-retrieve-constant-json-data-to-lambda-function the answer say that we have to use for example type = event["type"] and then pass data with {"type": "daily"} – liyas Aug 19 '22 at 07:30

1 Answers1

1

In Java, we can implement RequestHandler and override method handleRequest(paremeters).This way, you can pass the input to the Lambda handle request.

For Python, you can take a look at below examples from AWS Docs: https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html

In short(as per AWS Docs), The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event.

Hope this helps.

  • Following the aws doc I understand that I have to define for exemple the variable: iamuser = event["iamuser"] and then I will be able to pass any iamuser to the Lambda, is that correct ? – liyas Aug 19 '22 at 07:33