admin管理员组文章数量:1023579
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 badges2 Answers
Reset to default 0If 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
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 badges2 Answers
Reset to default 0If 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
版权声明:本文标题:amazon web services - api gateway can't invoke lambda function with put - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745591995a2157945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论