【问题标题】:Lambda functions do not trigger after cognito eventsLambda 函数不会在 cognito 事件后触发
【发布时间】:2021-12-07 10:10:45
【问题描述】:

我正在尝试使用 AWS Cognito 实施身份验证工作流程,以便将我的用户表(Hasura graphql 后端)与 Cognito 用户同步,但后确认 Lambda 不会触发。 Lambda函数代码如下:

const axios = require('axios');

exports.handler = async (event,context,callback) => {
  console.log(event);
  const id=event.request.userAttributes.sub;
  const name=event.request.userAttributes.username;
  const email=event.request.userAttributes.email;
  
  const hasuraAdminSecret="####"
  const graphAPI="####"
  const body = {
    query: `
    mutation insertUsers($userId: String!, $userName: String!, $userEmail: String!) {
    insert_users(objects: {cognito_id: $userId, email: $userEmail, username: $userName}) {
      affected_rows
    }
  }
  `,
    variables: {
      userId:id,
      userEmail:name,
      userName:email
    }
  }
  var  response = {};
  await axios.post(graphAPI, body, {
    headers: {'content-type' : 'application/json', 'x-hasura-admin-secret': hasuraAdminSecret}
    })
  .catch(err => {
    console.error(err.data);
    response=err.data;
  })
  .then(res => {
    console.log(res.data);
    response = res.data;
  })
  
  callback(null,event);
}

注册和确认页面的代码如下:

import { Auth } from 'aws-amplify';
export default {
    name:'Signin',
    data(){
        return {
            username: undefined,
            email: undefined,
            password: undefined,
            code: undefined,
            user: undefined,
        }
    },
    methods: {
        confirm() {
            // After retrieveing the confirmation code from the user
            Auth.confirmSignUp(this.username, this.code, {
                // Optional. Force user confirmation irrespective of existing alias. By default set to True.
                forceAliasCreation: false
            }).then(this.$router.push("/"))
              .catch(err => console.log(err));
        },
        signup(){
            Auth.signUp({
                username:this.username,
                password: this.password,
                attributes: {
                    email: this.email,
                    name:this.username
                },
                validationData: [],  // optional
                })
                .then(data => this.user = data.user)
                .catch(err => console.log(err));
        }
    }
    
}

注册时,在 AWS 控制台中创建并确认了用户,但未触发 lambda 函数(Cloudwatch 中没有日志,Cognito 也没有错误)。我应该去哪里看?

【问题讨论】:

  • 我想指出 Lambda 在使用控制台进行测试时工作正常。

标签: vue.js amazon-cognito aws-amplify


【解决方案1】:

一旦新用户signupaws-cognito,您可以使用trigger 调用lambda functions

第一步:general setting下打开aws-cognitoUser Pools点击trigger

第 2 步:您可以使用 triggers 自定义 workflow。你可以调用你的lambda函数

  • 预注册
  • 预认证
  • 自定义消息
  • 身份验证后
  • 确认后
  • 定义身份验证挑战
  • 创建身份验证挑战
  • 验证身份验证挑战
  • 用户迁移
  • 预令牌生成

第 3 步: 选择您的工作流触发器 Post confirmation,您可以看到 lambda 函数列表。您必须选择lambda 函数。

添加 Cloudwatch:

第 1 步: 您已在 Permissions 选项卡下的 Configuration 下添加角色

第 2 步:编辑role 并为CloudWatchCloudWatch Logs 附加policy

【讨论】:

  • 感谢您的详细回复。只是想补充一点,cloudwatch 日志没有出现,因为我的 lambda 权限有误。
  • @9rnt 我更新了将 cloudwatch 添加到您的 lambda 函数的答案。希望对您有所帮助。
猜你喜欢
  • 2020-09-13
  • 2018-05-05
  • 2019-09-05
  • 1970-01-01
  • 2020-05-16
  • 2018-08-24
  • 2020-12-28
  • 2014-09-18
  • 2021-08-13
相关资源
最近更新 更多