title: Using AWS Lambda to delete a Cognito User
body: <p>I want to give user's the ability to delete their account in my android app. I already set up a login/sig up functionality with AWS Amplify and a <code>AWS Cognito User Pool</code>. But Amplify doesn't provide a &quot;delete User&quot; functionality, so I wanted to use a lambda function to delete a user from my cognito user pool.</p>
  <p>The function will be called when the user clicks on &quot;delete my account&quot; in the app. To test the function, I use a hard coded username in the Lambda function, instead of passing one into the function. But even that doesn't work. After deploying the Lambda function, I run the function by clicking on &quot;Test&quot; in the console. The console then shows <code>Execution result: succeeded</code> but the response is <code>null</code>. I would either epect a Status 200 or 400 as response. And in the CloudWatch logs of the Execution I can only see my first log statement (&quot;I was here&quot;), the other two don't show up. And in the Cognito Console the user is still there.</p>
  <p>This is my <code>Lambda Code</code> (<code>Node.js</code>):</p>
  <pre><code>const AWS = require('aws-sdk');
  
  console.log(&quot;I was here&quot;);
  
  var params = {
   UserPoolId: 'syz****f-dev', 
   Username: '5b53****138'
  };
  var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({
      &quot;region&quot;: 'eu-central-1',
  });
  
  exports.handler = async (event) =&gt; {
    cognitoidentityserviceprovider.adminDeleteUser(params, function(err, data) {
      if (err) { 
        var response = {
          statusCode: 400,
          body: JSON.stringify('Didnt work!'),
        };
        console.log(err, err.stack); 
        return response;
      }
      else  {
        response = {
          statusCode: 200, 
          body: JSON.stringify('yeah!'),
        };
        console.log(data);          
        return response;
      }
    }); 
  };
  </code></pre>
  <p>The user &quot;5b53....138&quot; is still there in my Cognito User Pool &quot;syz....f-dev&quot; after I test this function:</p>
  <p><a href="https://i.stack.imgur.com/pqEwg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pqEwg.png" alt="enter image description here" /></a></p>
  <p>This is the log file that I found in Cloudwatch:</p>
  <p><a href="https://i.stack.imgur.com/nPuw0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nPuw0.png" alt="enter image description here" /></a></p>
  <p>My Lambda Function has a role with these 3 policies and I used the IAM Policy Simulator and the action <code>AdminDeleteUser</code> is allowed with <code>AmazonCognitoAuthenticatedIdentities</code>, so this shouldn`t be the problem:</p>
  <ul>
  <li>AmazonCognitoAuthenticatedIdentities</li>
  <li>AmazonCognitoPowerUser</li>
  <li>AWSLambdaBasicExecutionRole</li>
  </ul>
  <p>In CloudWatch I can see that the function got invoked.</p>