最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

amazon web services - api gateway can't invoke lambda function with put - Stack Overflow

matteradmin6PV0评论

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

Post a comment

comment list (0)

  1. No comments so far