0

I've created my first Lambda with Python to create a new entry in a DynamoDB table called Users, which has Partition key UserId of type String. I'm calling it using a POST method in my API Gateway using the test function to send a request body of:

{
    "email":"testemail",
    "name":"testname testerson",
    "password":"testpassword1"
}

The idea behind the method is to generate a UUID to use for the primary key, and on the occurrence of it already being in use, generate it again until it is unique. The lambda function is:

def create_user(event, context):
    status_code = 0
    response = ''
    body = event['body']
    
    # check all required fields are present
    if all(key in body.keys() for key in ['email', 'password', 'name']):
        # generate salt and hashed password
        salt = bcrypt.gensalt()
        hashed = bcrypt.hashpw(body['password'], salt)
        
        # get users table from dynamodb
        dynamodb = boto3.resource('dynamodb')
        table = dynamodb.Table('Users')
        
        inserted = False
        while not inserted:
            user_id = uuid.uuid4().hex
            try:
                response = table.put_item(
                    Item={
                        'UserId' = user_id,
                        'name' = body['name'],
                        'email' = body['email'],
                        'password' = hashed,
                        'salt' = salt
                    },
                    ConditionExpression = 'attribute_not_exists(UserId)'
                )
            except Exception as e:
                if e.response['Error']['Code'] == "ConditionalCheckFailedException":
                    continue
                status_code = 500
                response = 'Could not process your request'
                break
            else:
                status_code = 200
                response = 'Account successfully created'
                inserted = True
    else:
        status_code = 400
        response = 'Malformed request'
    
    return {
        'statusCode': status_code,
        'body': json.dumps(response)
    }

The syntax error appears in the logs on the line containing 'UserId' = user_id,, but I have no idea why.

Any help would be appreciated!

2
  • 1
    Please repeat on topic and How to Ask from the tour. "Teach me this basic language feature" is off-topic for Stack Overflow. Stack Overflow is not intended to replace existing tutorials and documentation. Commented Mar 1, 2021 at 4:04
  • For future reference, if you get a Syntax Error, the first thing would be check the appropriate syntax for what you are doing. Commented Mar 1, 2021 at 4:16

1 Answer 1

3

There are 2 standard ways of defining dictionary literals

Item={
    'UserId' = user_id,
    'name' = body['name'],
    'email' = body['email'],
    'password' = hashed,
    'salt' = salt
}

This is not one of them.

You can either do:

Item={
    'UserId': user_id,
    'name': body['name'],
    'email': body['email'],
    'password': hashed,
    'salt': salt
}

Or:

Item=dict(
    UserId=user_id,
    name=body['name'],
    email=body['email'],
    password=hashed,
    salt=salt
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can’t believe I missed this, thanks for pointing it out!

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.