admin管理员组

文章数量:1432159

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:

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:

Share Improve this question edited Nov 18, 2024 at 22:11 John Rotenstein 271k28 gold badges448 silver badges532 bronze badges asked Nov 18, 2024 at 21:51 AbdouAbdou 372 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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.

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

本文标签: amazon web servicesapi gateway can39t invoke lambda function with putStack Overflow