Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AWS Cognito] Congnito 이벤트 트리거로 데이터 DynamoDB에 올리기 #1

Open
sonypark opened this issue Oct 18, 2019 · 0 comments
Assignees
Labels
features Service features

Comments

@sonypark
Copy link
Owner

sonypark commented Oct 18, 2019

목표

Congnito를 통해 회원가입이 완료되면 이벤트를 발생시켜 DynamoDB에 User 정보를 저장한다.

프로세스

  1. 유저가 회원가입을 한 후 이메일 인증을 하면 Cognito의 PostConfrimation 트리거가 동작한다.

  2. 람다 함수에서 해당 이벤트를 받아 이벤트로부터 User 정보를 받아와서 DynamoDB에 저장한다.

  3. 성공 / 실패 여부를 context 를 통해 전달한다.

  4. 람다 함수 생성

    'use strict';
    
    const uuid = require('uuid');
    const AWS = require('aws-sdk');
    AWS.config.setPromisesDependency(require('bluebird'));
    
    const dynamoDb = new AWS.DynamoDB.DocumentClient();
    
    // Cognito Post Confirmation Event를 받은 후 DynamoDB에 User 정보 저장
    module.exports.registerUserToDynamoDB = async (event, context, callback) => {
      const user_info = {
        TableName: process.env.USER_TABLE,
        Item: {
          userId: uuid.v4(),
          email: event.request.userAttributes.email,
          nickname: event.request.userAttributes.nickname
        }
      };
    
      try {
        const res = await dynamoDb.put(user_info).promise();
    
        if (res) {
          // Send successful message
          context.succeed(event);
        }
      } catch (error) {
        // Send error message
        context.fail(err);
      }
    };

**2.** **serverless.yml 파일에 환경 설정**

    resources:
      Resources:
    		CognitoUserPoolLunchSquad:
          Type: 'AWS::Cognito::UserPool'
    
    
    
    RegisterUserToDynamoDB:
        handler: api/users.registerUserToDynamoDB
        description: Register user in DynamoDB after recieve cognito post confirmation event
        events:
          - cognitoUserPool:
              pool: LunchSquad
              trigger: PostConfirmation

3. Cognito에서 람다 함수를 PostConfirmation 트리거로 설정

Untitled

참고 링크

@sonypark sonypark self-assigned this Oct 18, 2019
@sonypark sonypark added the features Service features label Oct 18, 2019
@sonypark sonypark changed the title [Cognito] Congnito 이벤트 트리거로 데이터 DynamoDB에 올리기 [AWS Cognito] Congnito 이벤트 트리거로 데이터 DynamoDB에 올리기 Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
features Service features
Projects
None yet
Development

No branches or pull requests

1 participant