skip to Main Content

I have a lambda function that perform a soft-delete it doesn’t even invoke the lambda function, I don’t know if I should do more configuration. to the api gateway, but i did enable cors and configured it:

    import { DynamoDBClient, UpdateCommand } from "@aws-sdk/client-dynamodb";

    const client = new DynamoDBClient({ region: 'us-east-1' });
    const { RECORDS_TABLE } = process.env;

export const handler = async (event) => {
  try {
    const id = event.pathParameters?.id;
    
    if (!id) {
      return {
        statusCode: 400,
        headers: {
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
          "Access-Control-Allow-Headers": "Content-Type",
        },
        body: JSON.stringify({ message: "ID is required" }),
      };
    }

    const params = {
      TableName: RECORDS_TABLE,
      Key: {
        id: { S: id }
      },
      UpdateExpression: 'SET is_deleted = :true',
      ExpressionAttributeValues: {
        ':true': { BOOL: true }
      },
      ReturnValues: 'ALL_NEW'
    };

    const command = new UpdateCommand(params);
    const result = await client.send(command);
    
    return {
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
        "Access-Control-Allow-Headers": "Content-Type",
      },
      body: JSON.stringify({ 
        message: "Successfully soft deleted record",
        record: result.Attributes 
      }),
    };
  

    } catch (error) {
        console.error('Error in soft delete:', error);
        return {
          statusCode: 500,
          headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
            "Access-Control-Allow-Headers": "Content-Type",
          },
          body: JSON.stringify({ 
            message: "Failed to soft delete record", 
            error: error.message 
          }),
        };
      }
    };

I couldn’t figure out why it isn’t working all other methods POST and GET are working properly but PUT. Here’s a screenshot of my api gateway:
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    The problem was with UpdateCommand, I should've replace it with UpdateItemCommand


  2. If the lambda function isn’t even being called, it means you have an issue on the API Gateway configuration. Have you deployed your API with a new Stage for it to be accessible? I can’t see the errors or the POST/GET verbs on the same route.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search