AWS 使用 Lambda 和 DynamoDB 放大第 2 代 REST API

问题描述 投票:0回答:1

我正在使用 AWS Amplify Hosting(第 2 代)并创建了由 Lambda 支持的 REST API。 Lambda 函数应该发送一封电子邮件(工作正常)并将提交的数据保存到 DynamoDB 表中。但是,当尝试保存数据时,我收到以下错误:

ResourceNotFoundException:找不到请求的资源

我已验证表名称是否正确并使用特定表 ARN 进行测试。此外,我尝试对表名使用通配符以确保更广泛的访问权限。

这是我的 API 堆栈。

// create a new API stack
const apiStack = backend.createStack("api-stack");

// create a new REST API
const myRestApi = new RestApi(apiStack, "RestApi", {
  restApiName: "myRestApi",
  deploy: true,
  deployOptions: {
    stageName: "dev",
  },
  defaultCorsPreflightOptions: {
    allowOrigins: Cors.ALL_ORIGINS, // Restrict this to domains you trust
    allowMethods: Cors.ALL_METHODS, // Specify only the methods you need to allow
    allowHeaders: Cors.DEFAULT_HEADERS, // Specify only the headers you need to allow
  },
});

backend.ApiContactFormFunction.resources.lambda.addToRolePolicy(
  new PolicyStatement({
    actions: ['ses:SendEmail', 'ses:SendRawEmail'],
    resources: ['*']
  }),
)

backend.ApiContactFormFunction.resources.lambda.addToRolePolicy(
  new PolicyStatement({
    effect: Effect.ALLOW,
    actions: [
      'dynamodb:PutItem',
    ],
    resources: [
      "*"
    ]
  })
)

// create a new Lambda integration
const lambdaIntegration = new LambdaIntegration(
  backend.ApiContactFormFunction.resources.lambda
);

// create a new resource path with IAM authorization
const itemsPath = myRestApi.root.addResource("contact-form", {
  defaultMethodOptions: {
    authorizationType: AuthorizationType.NONE,
  },
});

// add methods you would like to create to the resource path
itemsPath.addMethod("GET", lambdaIntegration);
itemsPath.addMethod("POST", lambdaIntegration);
itemsPath.addMethod("DELETE", lambdaIntegration);
itemsPath.addMethod("PUT", lambdaIntegration);

// add a proxy resource path to the API
itemsPath.addProxy({
  anyMethod: true,
  defaultIntegration: lambdaIntegration,
});

// create a new IAM policy to allow Invoke access to the API
const apiRestPolicy = new Policy(apiStack, "RestApiPolicy", {
  statements: [
    new PolicyStatement({
      actions: ["execute-api:Invoke"],
      resources: [
        `${myRestApi.arnForExecuteApi("*", "/contact-form", "dev")}`,
      ],
    }),
  ],
});

// attach the policy to the authenticated and unauthenticated IAM roles
backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(
  apiRestPolicy
);
backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(
  apiRestPolicy
);

// add outputs to the configuration file
backend.addOutput({
  custom: {
    API: {
      [myRestApi.restApiName]: {
        endpoint: myRestApi.url,
        region: Stack.of(myRestApi).region,
        apiName: myRestApi.restApiName,
      },
    },
  },
});

这是我的 lambda 函数:

import type { APIGatewayProxyHandler, APIGatewayProxyHandlerV2 } from "aws-lambda";
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb";

import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';

const ddbClient = new DynamoDBClient({ region: 'eu-west-1' });

const sesClient = new SESClient({ region: 'af-south-1' });

export const handler: APIGatewayProxyHandler = async (event) => {
  console.log("event", event);

  const body = JSON.parse(event.body || '{}');
  const { fullName, email, phoneNumber, message } = body;

const recipient = '[email protected]';
  const subject = `Contact Form - ${fullName}`;

  const htmlTemplate = `Email Body`;

  const command = new SendEmailCommand({
    Source: '[email protected]',
    Destination: {
      ToAddresses: [recipient]
    },
    Message: {
      Body: {
        Html: { Data: htmlTemplate }
      },
      Subject: { Data: subject }
    }
  });

  const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin": "*", // Restrict this to domains you trust
      "Access-Control-Allow-Headers": "*", // Specify only the headers you need to allow
    },
    body: '',
  };

  try {
    const result = await sesClient.send(command);
    console.log(`Email sent to ${recipient}: ${result.MessageId}`);

    const tbCommand = new PutItemCommand({
      TableName: 'Table',
      Item: {
        email: { S: 'Test email' },
        message: { S: 'Test message' },
        phone: { S: 'Test phone' },
        fullName: { S: 'Fullname' }
      },
    });

    await ddbClient.send(tbCommand);

    response.body = JSON.stringify(`Email have been send to ${fullName} - ${email}}!`)
    
  } catch (error) {
    response.statusCode = 500;
    response.body = JSON.stringify(`There was an error sending the email: ${error}`)
  }

  return response;
};

如有任何帮助,我们将不胜感激。

amazon-web-services aws-lambda amazon-dynamodb aws-amplify nuxt3.js
1个回答
0
投票

确保使用以下形式的正确表名称:

The table name is in the form [Name]-[AppSyncApiId]-[env]

然后你可以像这样解决它,具体取决于你如何构建你的基础设施:

"Fn::ImportValue": {
  "Fn::Sub": "${AppSyncApiId}:GetAtt:CoursesTable:Name"
}
© www.soinside.com 2019 - 2024. All rights reserved.