在AWS Amplify推送后,Lambda中如何正确导入amplifyconfiguration.json?
解决Lambda云端无法读取amplifyconfiguration.json的问题
问题根源是你代码里用了本地绝对路径,这个路径在AWS Lambda的运行环境里不存在,所以找不到文件。以下是几种可行的解决方法:
方法1:将配置文件打包进Lambda部署包
把amplifyconfiguration.json放在Lambda函数的源码目录下,修改代码用相对路径读取:
const { Amplify } = require('aws-amplify'); const { signUp } = require('aws-amplify/auth'); const fs = require('fs'); const path = require('path'); async function doSomething() { try { // 用__dirname获取当前Lambda代码所在目录,拼接配置文件路径 const pathToAmplifyConfig = path.join(__dirname, 'amplifyconfiguration.json'); const amplifyConfig = JSON.parse(fs.readFileSync(pathToAmplifyConfig, 'utf8')); Amplify.configure(amplifyConfig); const { isSignUpComplete, userId, nextStep } = await signUp({ username: "mytestusername123", password: "mypassword", options: { userAttributes: { email: "mytestemail@gmail.com", }, autoSignIn: true } }); console.log("User created!"); } catch (error) { console.log('there was an error when singing:', error); } } exports.handler = doSomething; // Lambda需要导出handler作为执行入口
执行amplify push时,配置文件会和代码一起上传到Lambda,云端就能正常读取。
方法2:将配置存储到AWS Secrets Manager(推荐,更安全)
把amplifyconfiguration.json的内容存入Secrets Manager,然后Lambda通过AWS SDK读取:
- 登录AWS控制台,进入Secrets Manager,创建新密钥,将配置文件的JSON内容粘贴进去,记录密钥ARN。
- 给Lambda执行角色添加
secretsmanager:GetSecretValue权限。 - 修改Lambda代码:
const { Amplify } = require('aws-amplify'); const { signUp } = require('aws-amplify/auth'); const { SecretsManagerClient, GetSecretValueCommand } = require("@aws-sdk/client-secrets-manager"); const client = new SecretsManagerClient({ region: '你的AWS区域' }); async function getAmplifyConfig() { const command = new GetSecretValueCommand({ SecretId: '你的密钥ARN或名称' }); const response = await client.send(command); return JSON.parse(response.SecretString); } async function doSomething() { try { const amplifyConfig = await getAmplifyConfig(); Amplify.configure(amplifyConfig); const { isSignUpComplete, userId, nextStep } = await signUp({ username: "mytestusername123", password: "mypassword", options: { userAttributes: { email: "mytestemail@gmail.com", }, autoSignIn: true } }); console.log("User created!"); } catch (error) { console.log('there was an error when singing:', error); } } exports.handler = doSomething;
方法3:直接硬编码配置(仅测试用,不推荐生产环境)
把amplifyconfiguration.json里的内容直接作为对象写入代码:
const { Amplify } = require('aws-amplify'); const { signUp } = require('aws-amplify/auth'); async function doSomething() { try { const amplifyConfig = { // 粘贴amplifyconfiguration.json里的所有配置内容 "auth": { "Cognito": { "userPoolId": "你的UserPoolID", "userPoolClientId": "你的UserPoolClientID", "region": "你的AWS区域" // 其他配置项... } } }; Amplify.configure(amplifyConfig); const { isSignUpComplete, userId, nextStep } = await signUp({ username: "mytestusername123", password: "mypassword", options: { userAttributes: { email: "mytestemail@gmail.com", }, autoSignIn: true } }); console.log("User created!"); } catch (error) { console.log('there was an error when singing:', error); } } exports.handler = doSomething;
内容的提问来源于stack exchange,提问作者Trev347
相关产品推荐
相关产品推荐

