我正在开发一个使用 AWS Amplify Hosting(第 2 代)的 Nuxt 3 应用程序,并且我想使用 Lambda 函数通过 AWS SES 发送电子邮件。我创建了以下 Lambda 函数:
import type { Schema } from "../../data/resource";
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
const sesClient = new SESClient({ region: "af-south-1" });
export const handler: Schema["sendEmail"]["functionHandler"] = async (event) => {
const recipient = "[email protected]";
const subject = "Subject";
const body = "Body";
const command = new SendEmailCommand({
Source: "[email protected]",
Destination: {
ToAddresses: [recipient],
},
Message: {
Body: {
Text: { Data: body },
},
Subject: { Data: subject },
},
});
try {
const result = await sesClient.send(command);
console.log(`Email sent to ${recipient}: ${result.MessageId}`);
} catch (error) {
console.error(`Error sending email to ${recipient}: ${error}`);
throw new Error(`Failed to send email to ${recipient}`, { cause: error });
}
return `Hello, ${event.name}!`;
};
在我的前端,我触发 lamda 如下:
import { generateClient } from "aws-amplify/api";
import type { Schema } from "~/amplify/data/resource";
const client = generateClient<Schema>();
const response = await client.queries.sendEmail({
name: "Amplify",
});
当我触发此 lamda“未授权访问类型查询上的 sendEmail”时,我确实收到错误
这是我的后端资源设置
import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
import { sendEmail } from '../functions/send-email/resource';
const schema = a.schema({
sendEmail: a
.query()
.arguments({
name: a.string(),
})
.returns(a.string())
.authorization((allow) => [allow.guest() ,allow.authenticated()])
.handler(a.handler.function(sendEmail)),
Todo: a
.model({
content: a.string(),
})
.authorization((allow) => [allow.publicApiKey(), allow.guest() ,allow.authenticated()]),
});
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'apiKey',
apiKeyAuthorizationMode: { expiresInDays: 30 }
}
});
任何指导将不胜感激!具体来说,我想知道:
我已经在 SES af-south-1 区域验证了我的电子邮件地址。
预先感谢您的帮助!
在前端,您需要指定 authMode。
import { generateClient } from "aws-amplify/api";
import type { Schema } from "~/amplify/data/resource";
const client = generateClient<Schema>();
const response = await client.queries.sendEmail({
name: "Amplify",
},{
authMode: "userPool"
});