<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Igvir</title>
    <description>The latest articles on DEV Community by Igvir (@igvir).</description>
    <link>https://dev.to/igvir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F803892%2F85b97a34-4046-47ec-a9da-04e299245308.jpg</url>
      <title>DEV Community: Igvir</title>
      <link>https://dev.to/igvir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/igvir"/>
    <language>en</language>
    <item>
      <title>AWS Lambda Functions Failure Management</title>
      <dc:creator>Igvir</dc:creator>
      <pubDate>Fri, 10 May 2024 23:25:21 +0000</pubDate>
      <link>https://dev.to/igvir/aws-lambda-functions-failure-management-i67</link>
      <guid>https://dev.to/igvir/aws-lambda-functions-failure-management-i67</guid>
      <description>&lt;p&gt;In AWS Lambda development, failure management is a crucial aspect to ensure the reliability and robustness of serverless applications. Here's how invocation failures are captured and handled, as well as how functions within distributed transactions handle failures, and how the system ensures duplicate events do not change the outcome:&lt;/p&gt;

&lt;h3&gt;
  
  
  Invocation Failure Capture and Handling:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Logging and Monitoring&lt;/strong&gt;: Lambda functions typically integrate with CloudWatch for logging and monitoring. Invocation failures are captured in CloudWatch Logs, providing detailed insights into the nature of the failure, including any error messages or stack traces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dead Letter Queues (DLQ)&lt;/strong&gt;: Lambda allows configuring Dead Letter Queues for asynchronous invocations. When a Lambda function fails to process an event, the event payload can be automatically redirected to a designated DLQ. This enables further analysis or processing of failed events without losing them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retry Mechanisms&lt;/strong&gt;: Lambda supports automatic retries for asynchronous invocations in case of transient failures. Developers can configure the number of retries and the backoff interval between retries to accommodate different failure scenarios. This helps in handling temporary issues like network timeouts or resource constraints.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling Failures Within Distributed Transactions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Transactional Processing&lt;/strong&gt;: Lambda functions within a distributed transaction are designed to participate in ACID-compliant transactions using AWS Step Functions or similar orchestration services. In case of failure during transactional processing, the transaction coordinator ensures proper rollback of any changes made by the participating functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compensation Logic&lt;/strong&gt;: Each function within a distributed transaction implements compensation logic to revert any partial changes in case of failure. This ensures that the system maintains consistency even if one or more functions encounter errors during execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Idempotent Operations&lt;/strong&gt;: Functions are designed to perform idempotent operations, meaning the same function invocation with the same input results in the same outcome regardless of how many times it's invoked. This ensures that even if a function is retried due to failure or duplicate events, it doesn't lead to unintended side effects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ensuring Outcome Consistency with Duplicate Event Handling:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Idempotent Processing&lt;/strong&gt;: Lambda functions are designed to handle duplicate events gracefully by implementing idempotent processing logic. Before processing an event, functions can check whether the event has already been processed based on a unique identifier or by maintaining state information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Deduplication&lt;/strong&gt;: Event sources like S3, SNS, or Kinesis often provide mechanisms for event deduplication. Lambda functions can leverage these features to ensure that duplicate events are not processed multiple times, maintaining the desired outcome consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Idempotent Data Updates&lt;/strong&gt;: In scenarios where Lambda functions update data in external systems, they use idempotent data update techniques such as conditional writes or versioning. This ensures that even if the same update operation is performed multiple times due to duplicate events, it doesn't lead to inconsistent data states.&lt;/p&gt;

&lt;p&gt;Here is another simplified Java sample code demonstrating how you might handle Lambda invocation failures, distributed transactions, and duplicate event handling using AWS Lambda:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.lambda.runtime.Context&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.lambda.runtime.RequestHandler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.lambda.runtime.events.S3Event&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyLambdaFunction&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;RequestHandler&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S3Event&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;S3Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 1. Invocation Failure Capture and Handling&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Process S3 event&lt;/span&gt;
            &lt;span class="n"&gt;processS3Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Success"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Log failure to CloudWatch&lt;/span&gt;
            &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLogger&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error processing S3 event: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="c1"&gt;// Redirect failed event to Dead Letter Queue&lt;/span&gt;
            &lt;span class="c1"&gt;// (DLQ configuration not shown here)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Failure"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Sample method to process S3 event&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;processS3Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;S3Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Your processing logic here&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Sample method to handle distributed transactions&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleDistributedTransaction&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Begin distributed transaction&lt;/span&gt;

            &lt;span class="c1"&gt;// Perform operations with ACID compliance&lt;/span&gt;
            &lt;span class="c1"&gt;// Rollback transaction on failure&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Handle transaction failure&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Sample method to handle duplicate event handling&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isEventProcessedBefore&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;eventId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Check if the event ID has been processed before&lt;/span&gt;
        &lt;span class="c1"&gt;// Return true if already processed, false otherwise&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this sample:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The handleRequest method handles the incoming S3 events. Any exceptions thrown during event processing are caught and logged, and the failed events can be redirected to a Dead Letter Queue for further analysis.&lt;/li&gt;
&lt;li&gt;The handleDistributedTransaction method demonstrates how to handle distributed transactions within the Lambda function. Transactional processing logic can be implemented here, ensuring rollback in case of failure.&lt;/li&gt;
&lt;li&gt;The isEventProcessedBefore method checks if an event with a specific ID has been processed before, implementing duplicate event handling logic. This method can be used to ensure idempotent processing of events, preventing duplicate events from causing unintended side effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please note that this code is a simplified example for demonstration purposes and may need to be adapted to fit your specific use case and AWS Lambda configuration. Additionally, you'll need to add AWS SDK dependencies to interact with AWS services like S3, Lambda, and CloudWatch. Remember to configure appropriate IAM roles and permissions for your Lambda function to interact with other AWS services like CloudWatch, Dead Letter Queue, etc.&lt;/p&gt;

&lt;p&gt;Below is a more detailed example demonstrating how you might handle distributed transactions within an AWS Lambda function using Java with the AWS SDK for Java. The code uses AWS DynamoDB as the data store for the transaction. The Lambda function performs a simple transactional operation of transferring funds between two accounts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.lambda.runtime.Context&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.lambda.runtime.RequestHandler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.lambda.runtime.events.S3Event&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.dynamodbv2.AmazonDynamoDB&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.dynamodbv2.document.DynamoDB&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.dynamodbv2.document.TableWriteItems&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.dynamodbv2.document.spec.TransactWriteItemsSpec&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.amazonaws.services.dynamodbv2.document.utils.ValueMap&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyLambdaFunction&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;RequestHandler&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S3Event&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;AmazonDynamoDB&lt;/span&gt; &lt;span class="n"&gt;dynamoDBClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AmazonDynamoDBClientBuilder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultClient&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;DynamoDB&lt;/span&gt; &lt;span class="n"&gt;dynamoDB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DynamoDB&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dynamoDBClient&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sourceAccountId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"sourceAccountId"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;destinationAccountId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"destinationAccountId"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;S3Event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Begin distributed transaction&lt;/span&gt;
            &lt;span class="n"&gt;handleDistributedTransaction&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Success"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Log failure to CloudWatch&lt;/span&gt;
            &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLogger&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error processing transaction: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="c1"&gt;// Rollback transaction on failure&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Failure"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Sample method to handle distributed transactions&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleDistributedTransaction&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Create TransactWriteItemsSpec for the transaction&lt;/span&gt;
            &lt;span class="nc"&gt;TransactWriteItemsSpec&lt;/span&gt; &lt;span class="n"&gt;writeItemsSpec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TransactWriteItemsSpec&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Update operation for deducting amount from source account&lt;/span&gt;
            &lt;span class="n"&gt;writeItemsSpec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addUpdateItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TableWriteItems&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Account"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withUpdateExpression&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SET balance = balance - :amount"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withConditionExpression&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"balance &amp;gt;= :amount"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withValueMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValueMap&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;withNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":amount"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withPrimaryKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"accountId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sourceAccountId&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

            &lt;span class="c1"&gt;// Update operation for adding amount to destination account&lt;/span&gt;
            &lt;span class="n"&gt;writeItemsSpec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addUpdateItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TableWriteItems&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Account"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withUpdateExpression&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SET balance = balance + :amount"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withValueMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValueMap&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;withNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":amount"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withPrimaryKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"accountId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;destinationAccountId&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

            &lt;span class="c1"&gt;// Execute the transaction&lt;/span&gt;
            &lt;span class="n"&gt;dynamoDB&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;transactWriteItems&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;writeItemsSpec&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Transaction successful"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Handle transaction failure&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Transaction failed: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Rethrow exception for Lambda to handle&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please notice that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The handleDistributedTransaction method performs a distributed transaction using DynamoDB's transactWriteItems operation. It consists of two update operations: one deducting the amount from the source account and another adding the same amount to the destination account.&lt;/li&gt;
&lt;li&gt;Conditional expressions are used to ensure that the source account has sufficient funds to perform the transfer.&lt;/li&gt;
&lt;li&gt;In case of a transaction failure (e.g., insufficient funds, network issues), the method catches the exception, logs it, and rethrows it for AWS Lambda to handle. This allows Lambda to automatically retry the function according to its error handling behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure to replace "sourceAccountId" and "destinationAccountId" with actual account identifiers in your DynamoDB table. &lt;/p&gt;

&lt;p&gt;By implementing these practices, AWS Lambda development ensures robust failure management, reliable distributed transaction processing, and consistent outcomes even in the presence of duplicate events.&lt;/p&gt;

</description>
      <category>awslambda</category>
      <category>failure</category>
      <category>code</category>
    </item>
    <item>
      <title>Using Hash Codes as Unique Identifiers</title>
      <dc:creator>Igvir</dc:creator>
      <pubDate>Fri, 24 Feb 2023 10:31:00 +0000</pubDate>
      <link>https://dev.to/igvir/using-hash-codes-as-unique-identifiers-aca</link>
      <guid>https://dev.to/igvir/using-hash-codes-as-unique-identifiers-aca</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, we will cover a common use case for software developers: Using and generating Unique Identifiers.&lt;/p&gt;

&lt;p&gt;Let´s start with a definition: &lt;br&gt;
What is an identifier? The simplest explanation usually &lt;a href="https://en.wikipedia.org/wiki/Identifier"&gt;comes from Wikipedia&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique class of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable substance (or class thereof). The abbreviation ID often refers to identity, identification (the process of identifying), or an identifier (that is, an instance of identification). An identifier may be a word, number, letter, symbol, or any combination of those.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unique identifiers (UIDs), or identifiers, can be a string value or an integer that we use to address unique resources in a domain. Consumers (Systems or Users that need to refer to that resource) then use these identifiers to fetch the resource from a collection of resources. Without a unique identifier, the simple task of separating the resources and invoking those resources is almost impossible.&lt;/p&gt;

&lt;p&gt;We use Identifiers to refer to database structural elements, like the names of tables, files in a collection, or elements in a set.&lt;br&gt;
Another important concept here is &lt;a href="https://en.wikipedia.org/wiki/Metadata"&gt;Metadata&lt;/a&gt;. And again from Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Metadata is "data that provides information about other data", but not the content of the data, such as the text of a message or the image itself.&lt;br&gt;
As we will see in this article, metadata and identifiers can be related for different purposes, including generating a Unique Identifier (UID).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Use Metadata to create Unique Identifiers
&lt;/h2&gt;

&lt;p&gt;The metadata can be generated using algorithms, an example of this is a Hashing algorithm. Hashing consists of converting a general string of information into metadata that represent it. This is done to scramble the data so that it completely transforms the original value, making the hashed value utterly different from the original while also describing it in a certain way, to the point of identifying it. &lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Hans_Peter_Luhn"&gt;Hans Peter Luhn&lt;/a&gt; invented Hashing. He was a German researcher and scientist who worked at IBM. During his professional life, he created over 80 patents in the field of Computer Science and Information Science.&lt;/p&gt;

&lt;p&gt;Hashing is used for various functions, but here we are especially interested in its ability to verify the integrity of an object without having to recognize its content. Two files can be compared for equality easily through hashing. There is no need to open the two documents individually. Hashing compares them byte-by-byte and the computed hash value instantly tells if they are distinct.&lt;/p&gt;
&lt;h3&gt;
  
  
  Are Hash Codes Unique Identifiers?
&lt;/h3&gt;

&lt;p&gt;A hash code is of a fixed length, so from a mathematical point of view it cannot be unique for all possible inputs. But all such hash functions are carefully designed to minimize the probability of a collision (two distinct files with the same hash value).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection"&gt;Scott Chacon and Ben Straub in "Pro Git" book&lt;/a&gt; talk about Hashing collision options using SHA-1 as UID:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If all 6.5 billion humans on Earth were programming, and every second, each one was producing code that was the equivalent of the entire Linux kernel history (6.5 million Git objects) and pushing it into one enormous Git repository, it would take roughly 2 years until that repository contained enough objects to have a 50% probability of a single SHA-1 object collision.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore we can say that in most practical cases it is possible to use Hashing algorithms to generate Unique Identifiers.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using Hashing to Create a Unique File Names
&lt;/h2&gt;

&lt;p&gt;A simple use case where a Unique Identifier is needed is the naming process for an &lt;a href="https://en.wikipedia.org/wiki/Object_storage"&gt;Object Storage&lt;/a&gt;. For this example, we assume that objects are immutable so if the object changes a new hashing Identifier will be associated with the new version. The idea is simple we will create a Hash code for every object and use it as a Unique Identifier of that object version.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;We use Java for our sample code but it could be easily ported to other programming languages. To run the file you will need to install a Java SDK. If you need it, you can see &lt;a href="https://openjdk.org/install/"&gt;How to download and install JDK&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Hands-on with Code
&lt;/h2&gt;

&lt;p&gt;The code could be separated into three steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select the algorithm&lt;/li&gt;
&lt;li&gt;Parse file(s)&lt;/li&gt;
&lt;li&gt;Generate the UID&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Step 1: The Algorithm
&lt;/h3&gt;

&lt;p&gt;There are three main options when you think about the Hashing algorithm you want to use: MD5, SHA-1, or SHA-256. A Hash code is fixed length, MD5 generates a 128 bits code, SHA-1 is 160 bits and SHA-256 is 256 bits. Longer numbers minimize the likelihood of a collision of generated codes.&lt;/p&gt;

&lt;p&gt;Federal Information Processing Standards define Secure Hash Standard (SHS) &lt;a href="https://csrc.nist.gov/publications/detail/fips/180/4/final"&gt;FIPS PUB 180-4&lt;/a&gt; names seven algorithms: SHA-1, SHA-224, SHA-256,&lt;br&gt;
SHA-384, SHA-512, SHA-512/224, and SHA-512/256.&lt;/p&gt;

&lt;p&gt;In 2017, researchers were able to produce two different documents with the same SHA-1 hash. It took them 6610 years of processor time to do it. &lt;/p&gt;

&lt;p&gt;We use &lt;a href="https://docs.oracle.com/javase/6/docs/api/java/security/MessageDigest.html"&gt;java.security.MessageDigest&lt;/a&gt; class and SHA-1 Algorithm in this example.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Parse File(s)
&lt;/h3&gt;

&lt;p&gt;As mentioned earlier, we use java.security.MessageDigest class and SHA-1 Algorithm in this example. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.&lt;/p&gt;

&lt;p&gt;The code creates a new file input stream for reading the file content. The data is processed using the update method call. Once all the data has been updated, the digest methods should be called to complete the hash computation.&lt;/p&gt;

&lt;p&gt;See the code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Step 3: Generate the UID
&lt;/h3&gt;

&lt;p&gt;The digest method returns an array of bytes in decimal format. We need to convert it to hexadecimal format before call toString() method to finally to get an UID based on file data. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If the file changes in any way the resultant hash code will be different. You can handle those differences according to the context as a new version or as an integrity check failure. &lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Sample Code
&lt;/h3&gt;

&lt;p&gt;The complete code can be found bellow:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;We have made a tour of the concepts associated with the generation of Hash codes and their applications for the generation of Unique identifiers for objects in our application domain.&lt;br&gt;
The main strength of Hashing algorithms is that they offer, at the same time, the possibility of generating an integrity verification code and an identifier reliable enough to be the unique identifier. Hashing libraries are widely available as standard in practically all programming languages, so their use is very feasible and quick to implement.&lt;/p&gt;

&lt;p&gt;Although in the example we have used the case of generating filenames, many other uses of Hashing algorithms are well known. Just to name a few we can mention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database indexing&lt;/li&gt;
&lt;li&gt;Password storage&lt;/li&gt;
&lt;li&gt;Data compression&lt;/li&gt;
&lt;li&gt;Search algorithms&lt;/li&gt;
&lt;li&gt;Cryptography&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover photo by &lt;a href="https://unsplash.com/@georgeprentzas?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;George Prentzas&lt;/a&gt; on  &lt;a href="https://unsplash.com/photos/SRFG7iwktDk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Gupta, L. (2022, January 25). Java File Checksum. Howtodoinjava. Retrieved February 18, 2033, from &lt;a href="https://howtodoinjava.com/java/java-security/sha-md5-file-checksum-hash/"&gt;https://howtodoinjava.com/java/java-security/sha-md5-file-checksum-hash/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>hashing</category>
      <category>uid</category>
      <category>programming</category>
    </item>
    <item>
      <title>AWS Certified Professional Challenge 2023</title>
      <dc:creator>Igvir</dc:creator>
      <pubDate>Sat, 04 Feb 2023 18:16:59 +0000</pubDate>
      <link>https://dev.to/igvir/aws-certified-professional-challenge-2023-3ilh</link>
      <guid>https://dev.to/igvir/aws-certified-professional-challenge-2023-3ilh</guid>
      <description>&lt;h2&gt;
  
  
  Are you ready?
&lt;/h2&gt;

&lt;p&gt;A new challenge for those interested in AWS certification has already been announced. This time it's the Professional Challenge. A certification exam preparation program with advanced training including live and on-demand Twitch sessions with AWS experts.&lt;/p&gt;

&lt;p&gt;If you join the &lt;a href="https://pages.awscloud.com/GLOBAL-ln-GC-TrainCert-Professional-Certification-Challenge-Registration-2023.html" rel="noopener noreferrer"&gt;Get AWS Certified: Professional Challenge&lt;/a&gt; before April 28, they will send you a 50% discount coupon for the professional level certification exam. The options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS Certified Solutions Architect - Professional&lt;/li&gt;
&lt;li&gt;AWS Certified DevOps Engineer - Professional&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this challenge, the exam must be taken no later than May 31, 2023.&lt;br&gt;
&lt;a href="https://pages.awscloud.com/GLOBAL-ln-GC-TrainCert-Professional-Certification-Challenge-Terms-and-Conditions-2023-learn.html" rel="noopener noreferrer"&gt;See program conditions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>writing</category>
      <category>discuss</category>
    </item>
    <item>
      <title>I want to be a community builder</title>
      <dc:creator>Igvir</dc:creator>
      <pubDate>Sun, 06 Feb 2022 13:46:07 +0000</pubDate>
      <link>https://dev.to/igvir/i-want-to-be-a-community-builder-51c0</link>
      <guid>https://dev.to/igvir/i-want-to-be-a-community-builder-51c0</guid>
      <description>&lt;p&gt;At the end of last year, 2021, I received an email with an invitation: Would you like to be an AWS community builder?&lt;/p&gt;

&lt;p&gt;But, &lt;strong&gt;What is AWS Community Builder program?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The AWS Community Builders program offers technical resources, mentorship, and networking opportunities to AWS technical enthusiasts and emerging thought leaders who are passionate about sharing knowledge and connecting with the technical community.&lt;br&gt;
source: &lt;a href="https://aws.amazon.com/developer/community/community-builders/"&gt;https://aws.amazon.com/developer/community/community-builders/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It was an excellent and maybe logical question after a year of work with AWS services, not only to get my AWS Certification but to improve my career with products and services that are completely aligned with my path. Last year, I enjoyed working and learning about AWS, and after all that joy and work, it could be natural to start thinking about something like the AWS Community Builder Program, even when it represents some challenges.&lt;/p&gt;

&lt;p&gt;So, back to the question, "Would you like to be an AWS Community Builder?", I think I should say:&lt;br&gt;
Yes, I want to be an AWS Community builder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do Community builders get paid?&lt;/strong&gt;&lt;br&gt;
No, they do not get paid but they are rewarded with some AWS special gifts, most of them a surprise.&lt;/p&gt;

&lt;p&gt;To start the process I need to set a personal goal of how much knowledge I want to share through blogs, videos, etc. and I have to complete an application. Unfortunately, the process is closed now until mid-2022. So during this time I will be working on my goal and sharing my work here. &lt;/p&gt;

</description>
      <category>awscommunitybuilders</category>
    </item>
  </channel>
</rss>
