title: How to create login endpoint for serverless app with aws cognito service?
body: <p>I need to create login endpoint using cognito and serverless framework, i already created signUP and confirm email endpoints, but whatever i do with logIn endpoint i get error <code>&quot;message&quot;: &quot;Internal server error&quot;</code>. I try everything but still get this error. Help me, please.</p>
  <p>This is my login endpoind code:</p>
  <h1>userSignIn.js</h1>
  <pre><code>'use strict'
  
  const Responses = require('../common/api_responses');
  const AWS = require('aws-sdk');
  const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});
  const cognitoUserPoolClient = process.env.cognitoUserPoolClient
  
  exports.handler = function(event, context, callback){
  
    const user_data = JSON.parse(event.body);
  
    var params = {
      'AuthFlow': 'ADMIN_NO_SRP_AUTH',
      &quot;AuthParameters&quot;:{
        &quot;USERNAME&quot;: user_data.email,
        &quot;PASSWORD&quot;: user_data.password
      },
      &quot;ClientId&quot;: 'xxxxxxxxxxxxxxxxxxxxx',
      &quot;UserPoolId&quot;: 'us-east-1_XXXXXXX'
    };
  
    cognitoidentityserviceprovider.adminInitiateAuth(params, function(err, data) {
      if (err) {
        return Responses._400({err});
      }
      else{
        callback(null, Responses._200({data}));
      }
    });
  };
  
  </code></pre>
  <p>Config file :</p>
  <h1>serverless.yml</h1>
  <pre><code>provider:
    name: aws
    runtime: nodejs12.x
    lambdaHashingVersion: 20201221
    profile: testSLSBlogApi
    region: us-east-1
    stage: dev
    environment:
      tableNamePost: ${self:custom.tableNamePost}
      imageUploadBucket: ${self:custom.imagePostsBucket}
      region: ${self:provider.region}
    iamRoleStatements:
      - Effect: Allow
        Action:
          - dynamodb:*
          - s3:*
          - ses:*
        Resource: '*'
  functions:
    userSignUp:
      handler: lambdas/endpoints/userSignUp.handler
      events:
        - http:
           path: sign-up
           method: POST
           cors: true
    confirmSignUp:
      handler: lambdas/endpoints/confirmSignUp.handler
      events:
        - http:
           path: confirm-email
           method: POST
           cors: true
    userSignIn:
      handler: lambdas/endpoints/userSignIn.handler
      events:
        - http:
           path: sign-in
           method: POST
           cors: true
  resources:
    Resources:
      ImagesPostBucket:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: ${self:custom.imagePostsBucket}
      MyDynamoDBTable:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: ${self:custom.tableNamePost}
          AttributeDefinitions:
            - AttributeName: ID
              AttributeType: S
          KeySchema:
            - AttributeName: ID
              KeyType: HASH
          BillingMode: PAY_PER_REQUEST
      CognitoUserPool:
        Type: AWS::Cognito::UserPool
        Properties:
          UserPoolName: ${self:provider.stage}-user-pool
          UsernameAttributes:
            - email
          AutoVerifiedAttributes:
            - email
          Policies:
            PasswordPolicy:
              MinimumLength: 6
              RequireLowercase: False
              RequireNumbers: True
              RequireSymbols: False
              RequireUppercase: True
      CognitoUserPoolClient:
        Type: AWS::Cognito::UserPoolClient
        Properties:
          ClientName: ${self:provider.stage}-user-pool-client
          UserPoolId:
            Ref: CognitoUserPool
          ExplicitAuthFlows:
            - ADMIN_NO_SRP_AUTH
          GenerateSecret: false
  </code></pre>
  <p>This object i use for returning responses from lambdas handlers:</p>
  <h1>api_responses.js</h1>
  <pre><code>const Responses = {
    _200(data = {}){
      return {
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Methods': '*',
          'Access-Control-Allow-Origin': '*',
        },
        statusCode: 200,
        body: JSON.stringify(data),
      };
    },
  
    _400(data = {}){
      return {
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Methods': '*',
          'Access-Control-Allow-Origin': '*',
        },
        statusCode: 400,
        body: JSON.stringify(data),
      };
    },
  };
  
  module.exports = Responses;
   
  </code></pre>