<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sergei Khazov on Medium]]></title>
        <description><![CDATA[Stories by Sergei Khazov on Medium]]></description>
        <link>https://medium.com/@serkhazov?source=rss-947244454a80------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W8g24BmxXz-I-WoZ2ZivJw.jpeg</url>
            <title>Stories by Sergei Khazov on Medium</title>
            <link>https://medium.com/@serkhazov?source=rss-947244454a80------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 00:52:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@serkhazov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Export users from Amazon Cognito User Pool]]></title>
            <link>https://medium.com/@serkhazov/export-users-from-amazon-cognito-user-pool-bf57a128a9fe?source=rss-947244454a80------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf57a128a9fe</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[boto3]]></category>
            <category><![CDATA[cognito]]></category>
            <category><![CDATA[cognito-users-migration]]></category>
            <dc:creator><![CDATA[Sergei Khazov]]></dc:creator>
            <pubDate>Sat, 09 May 2020 03:15:22 GMT</pubDate>
            <atom:updated>2020-05-09T03:15:22.326Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*D-52CZyJk7VsFFvlpRxkNQ.png" /><figcaption>Cognito User Pool Export/Migration</figcaption></figure><p>If you ever decided to migrate or export existing users from Amazon Cognito User Pool you will notice that Amazon doesn’t support this feature. You can follow the open discussion about Cognito Export/Migration on <a href="https://forums.aws.amazon.com/thread.jspa?messageID=886307">AWS Forum</a>. However, Amazon User Pool has a very flexible API that we can use in order to migrate existing users.</p><p>To start exporting existing Cognito users, we should search for <a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html"><strong>ListUsers</strong></a> API call from <a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/Welcome.html">Amazon Cognito API documentation</a>. <strong>ListUsers</strong> action <em>Lists the users in the Amazon Cognito user pool</em>. So that exactly what we need for our goal. To start using <strong>ListUsers</strong> API call we need to provide the <strong>UserPoolId</strong> parameter with a value of user pool ID for the user pool on which the export should be performed. Also, you need to provide a <strong>Limit</strong> parameter with a maximum supported value which is 60 users per request. (Or you can use other value which is less than 60) Here’s my example of <a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html"><strong>ListUsers</strong></a> API call by using <a href="https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/list-users.html">AWS Command Line Interface</a></p><pre>$ aws --region us-east-1 cognito-idp list-users --user-pool-id us-east-1_aaaaaaaaa --limit 60</pre><blockquote><strong>Note:</strong> Make sure that you have appropriate AWS <strong>credentials </strong>and<strong> permissions </strong>to perform this action and you specify the correct AWS <strong>REGION</strong> where Amazon Cognito User Pool is located.</blockquote><p>If we execute this API call we should get a list of 60 users in JSON format. Each User record contains Cognito record attributes which are default required by Cognito service and <em>custom</em> attributes with a <em>custom</em> prefix. Here’s an example of user record:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69f043ee675308c094ba9a75c088bd11/href">https://medium.com/media/69f043ee675308c094ba9a75c088bd11/href</a></iframe><p>Having these responses that contain all attributes about each user gives us the ability to store or recreate users into another storage like DataBase, CSV file, or other Users Provider service.</p><h3>Pagination and Request the next batch of User Cognito records</h3><p>When we execute the above <strong>ListUsers</strong> API call we get the first 60 users from Amazon Cognito User Pool. And if we will execute the same API request we will get the same response. So in order to get the next 60 users, we should provide the <a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html#API_ListUsers_RequestSyntax"><strong>PaginationToken</strong></a><strong> </strong>parameter with a value that we can find from the first <strong>ListUsers</strong> API call response. As you can see from the above response example there is <strong>PaginationToken </strong>value exist. We need to provide this value for the next set of users. Here’s an example of <strong>ListUsers</strong> API call with <strong>PaginationToken</strong> parameter:</p><pre>$ aws cognito-idp list-users --user-pool-id us-east-1_aaaaa --limit 60 --pagination-token &quot;AAAAAAAAAAAA......FS8=&quot;</pre><p>After running the above API call we should get the response with another <strong>PaginationToken </strong>value and next batch of user records from Amazon Cognito User Pool. So for the third batch of users, we need to replace <strong>PaginationToken </strong>value from the previous <strong>ListUsers</strong> API call until we get the response without <strong>PaginationToken</strong> value. If <strong>PaginationToken </strong>value doesn’t exist in response, it means that there no more user records in Amazon Cognito Pool, so we reached the end of pagination.</p><p>Having this algorithm in mind we can easily create a script that will pull all Cognito User records until it will reach the end of pagination, the last set of users. You can use any language-specific <a href="https://aws.amazon.com/tools/">AWS SDKs</a> that can simplify your integration with Amazon Cognito Service.</p><h3>Python Script for Export Cognito Pool Users into CSV file</h3><p>As a script example, I decided to create a <a href="https://github.com/hawkerfun/cognito-csv-exporter">Python script</a> that exports all Cognito User Pool records into a CSV file. The script uses Python AWS SDK which is called <a href="https://github.com/boto/boto3"><strong>boto3</strong></a><strong> </strong>In order to perform <strong>ListUsers</strong> API call using <strong>boto3</strong> library you need to use the <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp.html#CognitoIdentityProvider.Client.list_users"><em>list_users</em></a><em>(**args) </em>method from<em> </em><a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp.html#id121">CognitoIdentityProvider</a>. Here’s an example of a Python function that utilizes <strong>ListUsers</strong> AWS API call:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f824fbea4001b27743fbccda483804ae/href">https://medium.com/media/f824fbea4001b27743fbccda483804ae/href</a></iframe><p>The complete example of a <a href="https://github.com/hawkerfun/cognito-csv-exporter"><strong>Python script</strong></a> that exports all Cognito User Pool records into a CSV file you can find under my <strong>Github</strong> repository <a href="https://github.com/hawkerfun/cognito-csv-exporter">cognito-csv-exporter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf57a128a9fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Navigate your AWS Lambda Logs from Amazon API Gateway Response Headers]]></title>
            <link>https://medium.com/@serkhazov/navigate-your-aws-lambda-logs-from-amazon-api-gateway-response-headers-c78bd7f73715?source=rss-947244454a80------2</link>
            <guid isPermaLink="false">https://medium.com/p/c78bd7f73715</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[logging]]></category>
            <dc:creator><![CDATA[Sergei Khazov]]></dc:creator>
            <pubDate>Fri, 13 Jul 2018 17:47:39 GMT</pubDate>
            <atom:updated>2018-07-13T17:47:39.954Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vtm7v1C3GDP-Mbj_" /><figcaption>Photo by <a href="https://unsplash.com/@pawel_czerwinski?utm_source=medium&amp;utm_medium=referral">Paweł Czerwiński</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When Front-End team works on integration of the new endpoint into the app, they might catch some unexpected behavior from endpoint. So the Back-End team needs spend some time on tracing down logs, finding the specific request that finished unsuccessfully and then start the investigation. Searching for broken request seems to be a big waist of time. And what if Front-End engineer can include in the bug report some unique request Id that helps Back-End engineer to simply navigate request’s execution logs, so engineer can start the investigation right away. This article explains how to implement this approach into your serverless app.</p><p>For Back-End App implementation I used <a href="https://aws.amazon.com/lambda/">AWS Lambda</a>, <a href="https://aws.amazon.com/api-gateway/">Amazon API Gateway</a>, and <a href="https://serverless.com/">Serverless framework</a>. Since API Gateway has access to data that was return from integration part (in our case it’s Lambda function), the main challenge for this implementation is to describe to the API Gateway where to pull from Lambda <strong><em>awsRequestId</em></strong> value (unique Lambda request id) so it can inject into response headers.</p><p>Lambda <strong><em>awsRequestId </em></strong>can be retrieved as a property from <a href="https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html">$context variable</a> inside your Lambda handler as:</p><p>console.log(&#39;AWSrequestID&#39;, context.aswReestId);</p><blockquote><strong>awsRequestId</strong></blockquote><blockquote>AWS request ID associated with the request. This is the ID returned to the client that called the invoke method.</blockquote><p>So when you can read Lambda <strong><em>awsRequestId</em></strong> you can send it to API Gateway response headers. There two ways how to set up custom headers in Amazon API Gateway:</p><ol><li>Using Lambda Proxy Integration</li><li>Using API Gateway Response Data Mapping for Custom Lambda Integration</li></ol><h3>Lambda Proxy Integration</h3><p>If you develop your application by using <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html"><strong>Proxy Integration</strong></a> you can simply define your custom header, lets say: x-amzn-LambdaRequestId, inside your Lambda response object as one of the property of headers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/499168e52f313d7a49e4ae62c27c59c9/href">https://medium.com/media/499168e52f313d7a49e4ae62c27c59c9/href</a></iframe><p>So now, when you’ll trigger your Lambda through API Gateway endpoint, you will see the additional costume header that you just set up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6E-58vYj3fPm0002HeA8zQ.png" /><figcaption>Lambda awsRequestId presented in API Gateway Response Headers</figcaption></figure><p>Having this value you can simple navigate to Lambda’s <a href="https://aws.amazon.com/cloudwatch/">AWS CloudWatch</a> Logs and search Lambda logs by <strong><em>awsRequestId </em></strong>value: &quot;fbfbe144-8074-11e8-a928-e9557e22f5ec&quot; Please make sure that you place <strong><em>awsRequestId</em></strong> value in double quotes, so CloudWatch will take it as a whole string. The search result display logs flow related to the lambda’s specific request. So you can easily traced down your application on each step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6oe_zjYS7nMWb85bJgNTcg.png" /><figcaption>CloudWatch Filter Logs example</figcaption></figure><h3>Custom Lambda Integration</h3><p>If you develop you serverless application by using <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-lambda-non-proxy-integration.html">Lambda Integration</a> you need to use <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html#mapping-response-parameters">API Gateway Response Data Mapping</a> to define how Lambda will set custom header value for API Gateway.</p><p>Since you already know how to get <strong><em>awsRequestId</em></strong> inside Lambda function (see above), you can start from defining you <em>serverless.yml</em> template file. When you defining lambda function in template file, you need to adjust response section and include headers property with expected headers mapping from lambda.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d1e9ebdfdabd2287b54ff044ba326a2/href">https://medium.com/media/2d1e9ebdfdabd2287b54ff044ba326a2/href</a></iframe><p>As you can see from above example, there new header key x-amzn-LambdaRequestId, that gets <strong><em>awsRequestId</em></strong> from lambda through integration.response object. Integration Response object is the part of <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html#mapping-response-parameters">API Gateway Response Data Mapping variables</a>. It provides the access for data returned from integration part (Lambda function in our case), so since lambda returns <strong><em>awsRequestId</em></strong> as part of body response, you can simply map it to custom header. Let’s take a look how to define the response from lambda, to support above template.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c45f6cdd8c5a4618f38cc582286df3a7/href">https://medium.com/media/c45f6cdd8c5a4618f38cc582286df3a7/href</a></iframe><p>In above example, Lambda response includes header property awsLambdaRequestId that gives the access to API Gateway to read this value as integration.response.body.headers.awsLambdaRequestId When you need to support error response from lambda, you need to stringify the whole response object and read lambda’s <strong><em>awsRequestId</em></strong> through errorMessage property as integration.response.body.errorMessage.headers.awsLambdaRequestId It should set the custom header value as expected. After the application is deployed, you will get the same ability to search logs as you did in Proxy Integration implementation (see above)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_lyRx8HckTn1R9_SyqM9-g.png" /><figcaption>Example filter logs in Lambda Custom Integration</figcaption></figure><p>Having this logging feature improvement on your endpoint can save you time on searching logs and it can improve bug reports information for your Back-End team. Good luck and happy codding!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*ujvGw4LZpqsTPmvSVi3trg.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c78bd7f73715" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Organize your Lambda functions into separate services]]></title>
            <link>https://medium.com/@serkhazov/organize-your-lambda-functions-into-separate-services-1b725a186604?source=rss-947244454a80------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b725a186604</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Sergei Khazov]]></dc:creator>
            <pubDate>Tue, 03 Jul 2018 15:19:19 GMT</pubDate>
            <atom:updated>2018-07-03T15:19:19.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*6KQVZj8FMRKOEgEunIWZWw.png" /></figure><p>When I started my first complex application with full end-to-end serverless approach on AWS ecosystem I faced into the issue with continuously growing number of lambda functions living independently from each other. It become extremely hard to navigate between function and determine which lambda function related to certain service. I started to think, how I can solve this issue, so I can monitor my application at the end, quickly navigate through the functions and categorize lambda functions into different services. So I came up with this approach.</p><p>For developing serverless application I’m using <a href="https://serverless.com/">serverless framework</a> It has tones of useful plugins and ready out of the box features. For having set of lambda functions organized in one application service or some kind of abstracted entity I came up with tagging approach. Lots of AWS services supports <a href="https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/Welcome.html">tags</a>, same as AWS Lambda functions. To apply this approach there no need to install any plugins for <a href="https://serverless.com/">serverless framework,</a> it supported out of the box. Here’s the example for my <em>serverless.yml</em> template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a77af9788836ee0ab5c676b179a9829/href">https://medium.com/media/6a77af9788836ee0ab5c676b179a9829/href</a></iframe><p>In example above I used stackTags provider attribute. stackTags attributes adds list of tags to the whole CloudFormation stack. It means that these tags will be applied for all resources created by CloudFormation stack(only resources for which AWS CloudFormation supports tagging)</p><p>In case if you need to apply tags only for specific lambda functions, you can specify those tags under tags property in function declaration section in <em>serverless.yml</em> file. Here’s an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f26bd788f43a5c8a6f68ec9c304d88b3/href">https://medium.com/media/f26bd788f43a5c8a6f68ec9c304d88b3/href</a></iframe><p>After the application is deployed, I was able to search my lambda function by the tag name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OtzLU6gmrkyTOdbdAaKWSA.png" /><figcaption>Tags search options</figcaption></figure><p>When I need to search some lambda function by service name, I select SERVICE_NAME tag and select CognitoUser service name as a tag value and get only 6 function out of 127, which are related to CognitoUser service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JHm88sxxG13LpM7YnbpmRw.png" /><figcaption>Result Filter by tag CognitoUser</figcaption></figure><p>As the resolute of applying this tagging strategy it helps me a lot to quickly navigate between different services, identify and categorize those resources.</p><p>As one of the additional benefits of apply tags for serverless application, it gives you ability to query your resources through <a href="https://docs.aws.amazon.com/cli/latest/reference/resourcegroupstaggingapi/index.html">AWS resource groups tagging API</a>. Here’s the example how you can get all your resources regarding the tag SERVICE_NAME: CognitoUser</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72971dcc2e87937f6b3f93dd92c4f9ae/href">https://medium.com/media/72971dcc2e87937f6b3f93dd92c4f9ae/href</a></iframe><p>Also I reached out AWS support team regarding the feature of building AWS CloudWatch dashboards based on resource tags. AWS support team replayed that CloudWatch service team is currently working on feature, that will allow developers build CloudWatch dashboard metrics for lambdas based on tags. So keeping eyes on updates and looking forward for new features.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b725a186604" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>