Quantcast
Channel: Active questions tagged aws-lambda - Stack Overflow
Viewing all articles
Browse latest Browse all 4012

AWS Lambda function task timed out

$
0
0

I created a serverless App on AWS using the serverless framework template. The Lambda function is in python3.10, arm architecture. the code will register an user in aws cognito user pool. I created a cognito user pool and cognito user pool client. The lambda handler has the cognito policy to create the user using the admin create user, admin_initate_auth, respond_to_auth_challenge functions. im am not understanding why the lambda is getting timed out. the lambda is running but it is not executing the code client.admin_create_user() function.

import boto3import botocoreimport osdef registerHandler(event, context):    print("Received event: " + str(event))    print("Received context: " + str(context))    print(os.environ)    if event and event['httpMethod'] != 'POST':        return {'statusCode': 405,'body': 'Method Not Allowed'        }    if event['body'] is None:        return {'statusCode': 400,'body': 'Bad request, missing request body'        }    try:        body = event['body']        required_fields = ['firstName','lastName','userName', 'password', 'email', 'phoneNumber']        if not all(field in body for field in required_fields):            return {'statusCode': 400,'body': 'Bad request, missing required fields'            }        if not re.match(r'^[a-zA-Z]+$', body['firstName']):            return {'statusCode': 400,'body': 'Invalid first name'            }        if not re.match(r'^[a-zA-Z]+$', body['lastName']):            return {'statusCode': 400,'body': 'Invalid last name'            }        if not re.match(r'^[a-zA-Z0-9]+$', body['userName']):            return {'statusCode': 400,'body': 'Invalid username'            }        if not re.match(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', body['email']):            return {'statusCode': 400,'body': 'Invalid email'            }        if not re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$', body['password']):            return {'statusCode': 400,'body': 'Invalid password'            }        client = boto3.client('cognito-idp', region_name='us-east-1')        print('client created')        response = client.admin_create_user(                    UserPoolId='us-east-1_2OBxdlwJy',                    Username=body['email'],                    UserAttributes=[                        {'Name': 'given_name','Value': body['firstName'],                        },                        {'Name': 'family_name','Value': body['lastName'],                        },                        {'Name': 'name','Value': body['userName'],                        }                    ],                    ValidationData=[                        {'Name': 'string','Value': 'string'                        },                    ],                    TemporaryPassword='Temporary@123',                    ForceAliasCreation=False                )        print(response)        response4 = client.admin_initiate_auth(            UserPoolId='us-east-1_2OBxdlwJy',            ClientId='iq5gbrkd83uo83mt4g5slueir',            AuthFlow='ADMIN_USER_PASSWORD_AUTH',            AuthParameters={'USERNAME': body['email'],'PASSWORD': 'Temporary@123'            })        print(response4)        challenge_name = response4['ChallengeName']        challenge_responses = {'USERNAME': body['email'],'NEW_PASSWORD': body['password']        }        response3 = client.respond_to_auth_challenge(                    ClientId='iq5gbrkd83uo83mt4g5slueir',                    ChallengeName=challenge_name,                    ChallengeResponses=challenge_responses,                    Session=response4['Session']                )        print(response3)        return {'statusCode': 200,'body': 'User created successfully'        }    except Exception as e:        print(e)        return {'statusCode': 500,'body': 'Internal Server Error'        }

enter image description hereenter image description hereenter image description hereenter image description here


Viewing all articles
Browse latest Browse all 4012

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>