<?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 Marin Radjenovic on Medium]]></title>
        <description><![CDATA[Stories by Marin Radjenovic on Medium]]></description>
        <link>https://medium.com/@marinradjenovic?source=rss-29071b449a57------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0aRApJQqQFsO2JQtk1n76g.jpeg</url>
            <title>Stories by Marin Radjenovic on Medium</title>
            <link>https://medium.com/@marinradjenovic?source=rss-29071b449a57------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:09:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marinradjenovic/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[Replatforming and Rearchitecing with Serverless Event-based Architecture Approach #2 code…]]></title>
            <link>https://medium.com/@marinradjenovic/replatforming-and-rearchitecing-with-serverless-event-based-architecture-approach-2-code-034090f3f9b4?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/034090f3f9b4</guid>
            <category><![CDATA[legacy-systems]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[eventbridge]]></category>
            <category><![CDATA[event-based-architecture]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Sun, 25 Feb 2024 11:44:19 GMT</pubDate>
            <atom:updated>2024-02-25T11:44:19.876Z</atom:updated>
            <content:encoded><![CDATA[<h3>Replatforming and Rearchitecing with Serverless Event-based Architecture Approach #2 code explanation</h3><p>In the previous article, I elaborated on certain strategies and tactics on how to convert 3-tier web applications to serverless event-based applications. In this article, we will discuss the most important pieces of infrastructure and application code.</p><p><a href="https://medium.com/crayon-ai/replatforming-and-rearchitecing-with-serverless-event-based-architecture-approach-5ce9f2e326a6">Replatforming and Rearchitecing with Serverless Event-based Architecture Approach</a></p><p>It has been a long time since I have rolled out a continuation article to the previous article. Sorry about that, but life obligations just took over my time…. Anyway, here it is. This article will highlight the code details to achieve this architecture.</p><p>Before we start deep-diving into the code, here are the most important segments of the architecture from the previous article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5S3xCASlCOgPyK8G.png" /></figure><ol><li>RDS table CRUD operation trigger that invokes the AWS Lambda (EventCreatorLambda) and passes the CRUD (inserted/updated/deleted) data payload as JSON.</li><li>Such data is reformated and passed as an event message to <a href="https://aws.amazon.com/eventbridge/">EventBridge’s</a> custom event bus. The Points 1 and 2 are making the solution&#39;s core of the event-based architecture.</li><li>NotificationLambda and CustomerIntegration lambda will be triggered by certain event bridge rule that are sent to the event bus.</li><li>NotificationLambda will publish an SNS message on a certain topic and notify all subscribers who are interested in that kind of event.</li><li>CustomerIntegrationLambda will notify the customer application over API webhooks.</li></ol><h4>Event Creation</h4><p>The best idea to create an event is from the most crucial entity in the source system. Such an entity in this scenario is <strong>Task</strong>, as we have elaborated in the previous article<strong>.</strong> Tasks are stored in the task table in the PostgreSQL relation database running <a href="https://aws.amazon.com/rds/aurora/">Aurora RDS PostgreSQL</a>.<br>In general, OLTP DBs are not the perfect environment for creating event-based architectures. OLTP databases excel at transactional processing and ensuring data consistency in real-time, they may not provide the flexibility, scalability, and loose coupling required for event-based architectures to operate optimally action does not produce events by default. Such practice is commonly implemented with microservices and NoSQL DBs or in-mem DBs that support pub-sub capabilities.<br>However, when your DB is running PostgreSQL on Aurora you do have a variety of features that can be utilized for event creation which will be discussed in continuation of this article.</p><h4>Table Triggers</h4><p>Table triggers are database actions that automatically execute a specified action when a specified event occurs on a table, such as insert, update, or delete operations. For instance, the trigger can automatically insert a record when a certain record is deleted or changed on the operational table into a similar history table. That way, it provides tracking of changes on certain records throughout the history.<br>A simple DB trigger code is explained below.</p><pre>CREATE TRIGGER trigger_name<br>{BEFORE | AFTER} {INSERT | UPDATE | DELETE}<br>ON table_name<br>FOR EACH ROW<br>BEGIN<br>    -- SQL statements to be executed when trigger condition is met<br>END;</pre><p>The great feature of RDS/Aurora is that you can utilize AWS service over table triggers such as S3 and Lambda invocations.</p><h4>Table Triggers on Aurora PostgreSQL</h4><p>Amazon Aurora PostgreSQL enables DB triggers to perform operations on AWS services or using AWS Services. For instance, you can call lambda from the trigger function.<br>The aws_lambda.invoke function operates either synchronously or asynchronously, determined by the invocation_type parameter. Two options exist for this parameter: RequestResponse (default) and Event.</p><p><strong>RequestResponse</strong>: This type is synchronous, and expects the results within the function’s response payload. It’s the default behavior when not explicitly specified.</p><p><strong>Event</strong>: Asynchronous, without including result payloads in the response. Suitable for workflows not reliant on function results.</p><pre>CREATE OR REPLACE FUNCTION somw_lambda_trigger_function() RETURNS TRIGGER AS $$<br>DECLARE<br>  payload JSON;<br>BEGIN<br>   --Some select that will load payload variable as JSON <br>   PERFORM aws_lambda.invoke(aws_commons.create_lambda_function_arn(&#39;[lambda name]&#39;, &#39;[region]&#39;), payload, &#39;Event&#39;);<br>  RETURN NEW;<br>END;<br>$$<br>language plpgsql;</pre><p>To set a trigger that invokes lambda, it is necessary to provide permission to RDS to invoke lambda. More on how to set triggers on Aurora PostreSQL can be found <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL-Lambda.html">here</a>.</p><p>In our case, <strong>EventCreatorLamba</strong> is invoked with an asynchronous <strong>event invocation </strong>that is the actual CRUD change to the <strong>Task</strong> table. The change which is provided to the trigger function will be converted into JSON and lambda will be invoked with the JSON as payload.</p><h4>Publishing Events on the EventBridge</h4><p>EventBridge is a serverless event bus service by AWS enabling event-driven architectures. It enables the decoupling of applications, systems, and services, allowing them to communicate via events, and acts as a central hub for routing events in real time based on predefined rules, allowing for event-driven architectures.</p><p>Lambda that is been invoked by the trigger, receives the change as JSON payload. That payload needs to be further repacked into JSON format which is expected by the Event Bridge. Here is the description of the important fields that are constructing the event message.</p><ul><li><strong>id</strong> — A Version 4 UUID generated for every event.</li><li><strong>detail-type</strong> — The type of event that’s being sent. See <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventBridge.html">Using EventBridge</a> for a list of event types.</li><li><strong>source</strong> — Identifies the service that generated the event.</li><li><strong>account</strong> — The 12-digit AWS account ID.</li><li><strong>time</strong> — The time the event occurred.</li><li><strong>detail</strong> — A JSON object that contains information about the event. For more information about what can be included in this field, see the <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/ev-events.html#ev-events-detail">Event message detail field</a>.</li></ul><pre>const AWS = require(&#39;aws-sdk&#39;);<br><br>exports.handler = async (event, context) =&gt; {<br>    const eventBridge = new AWS.EventBridge();<br>    <br>    const params = {<br>        Entries: [<br>            {<br>                Source: &#39;custom.event.source&#39;,<br>                DetailType: &#39;task.change.event&#39;,<br>                Time: new Date(),<br>                Detail: JSON.stringify(Event) // event received by Table trigger<br>                EventBusName: &#39;[YOUR_BUS_NAME]&#39;<br>            }<br>        ]<br>    };<br>    <br>    try {<br>        await eventBridge.putEvents(params).promise();<br>        return {<br>            statusCode: 200,<br>            body: JSON.stringify(&#39;Event published successfully&#39;)<br>        };<br>    } catch (error) {<br>        console.error(&#39;Error publishing event:&#39;, error);<br>        return {<br>            statusCode: 500,<br>            body: JSON.stringify(&#39;Error publishing event&#39;)<br>        };<br>    }<br>};</pre><h4>Infrastructure Code</h4><p>Infrastructure code, for this purpose, can be any tool that is offered on the market today such as terraform or cloud formation. For this purpose, I have selected AWS SAM as it is very convenient to work with serverless.</p><pre>AWSTemplateFormatVersion: &#39;2010-09-09&#39;<br>Transform: &#39;AWS::Serverless-2016-10-31&#39;<br><br>Resources:<br>  EventCreatorLambda:<br>    Type: &#39;AWS::Serverless::Function&#39;<br>    Properties:<br>      Handler: index.handler<br>      Runtime: nodejs14.x<br>      CodeUri: ./event_creator_lambda/src/<br>      ....<br><br>  NotificationLambda:<br>    Type: &#39;AWS::Serverless::Function&#39;<br>    Properties:<br>      Handler: index.handler<br>      Runtime: nodejs14.x<br>      CodeUri: ./notification_lambda/src/<br>      ....<br><br>      Events:<br>        TaskTriggerRule:<br>          Type: EventBridgeRule<br>          Properties:<br>            EventBusName: [YOUR_BUS_NAME]<br>            EventPattern:<br>              source:<br>                - &quot;custom.event.source&quot;<br>              detail-type:<br>                - &quot;task.change.event&quot;<br></pre><p>In the example above, EventCreatorLambda is defined in AWS SAM by its properties. However, it is not possible to configure RDS event trigger such as events like API Gateway, SNS, SQS, Event Bridge, and etc. Thefore it has to be done over command line. Plese check the instructions described <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL-Lambda.html"><strong>here</strong></a><strong>. <br></strong>NotificationLambda on the other side is triggered by the event created by EventCreatorLambda and sent to EventBridge.</p><h4>Final thoughts</h4><p>This solution is very quick &amp; simple way to turn replatformed 3-tier web applications into event-based applications. Event-based architecture provide various benefits such clear decoupling, better scalability and finally quicker innovation. <br>However, it is good to mind that triggers as technology are having some drawbacks. Triggers can make database systems more complex and harder to understand. Since triggers operate behind the scenes, it can be difficult to track their actions and understand their effects on the database.<br>Also, triggers add overhead to database operations because they execute additional code every time a specified event occurs. This can impact the performance of the database, especially in systems with large numbers of triggers or high-frequency events.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=034090f3f9b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Replatforming and Rearchitecing with Serverless Event-based Architecture Approach]]></title>
            <link>https://medium.com/crayon-ai/replatforming-and-rearchitecing-with-serverless-event-based-architecture-approach-5ce9f2e326a6?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ce9f2e326a6</guid>
            <category><![CDATA[cloud-native]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Mon, 21 Aug 2023 07:52:03 GMT</pubDate>
            <atom:updated>2024-01-04T10:06:28.122Z</atom:updated>
            <content:encoded><![CDATA[<p>Web businesses that run successfully for several years or even a decade can experience the tiredness of perpetual growth. That is usually caused by multiple factors such as technical debt, expensive and exhausting support, lack of automatization, old tech-stacks, and finally keeping and hiring adequate staff. At that time, migrating to a cloud provider such as AWS, gives your application more maneuvering space and the ability to evolve.</p><p>Replatforming the solution very often can be simple. By moving your application to AWS services such as Elastic Beanstalk and RDS, you are able to quickly spot benefits like automated backups, automated scaling, point-in-time recovery, and so on. However, the real benefit comes when you sprinkle some of the cloud native s erverless services into existing applications with some simple rearchitecting/refactoring approaches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R5YJBQs3KMGwLvig" /><figcaption>Photo by <a href="https://unsplash.com/@anniespratt?utm_source=medium&amp;utm_medium=referral">Annie Spratt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4><strong>Intro</strong></h4><p>Most web applications today are basic 3-tier web applications. A 3-tier web application is a software architecture consisting of three layers: presentation, logic, and data. The presentation tier handles user interface interactions, the logic tier manages application processes, and the data tier stores and retrieves information from databases. At the time, 3-tier web applications offered better scalability, maintainability, and separation of concerns than coupled application architectures.<br> As mentioned before, there were multiple reasons over the years which sounded the alarm that it was time for some changes. One obvious choice is to utilize automated/managed services that can be found on cloud providers such as AWS. By moving to the cloud, the cost of operating the environments drops significantly.</p><p><strong>AWS Migration Strategies</strong></p><p>Depending on what you would like to achieve, the complexity of migrating to the cloud can vary. AWS offers 6 strategies for migrating to the cloud that can lead to a planned successful migration.</p><p>1. <strong>Rehost</strong> (lift and shift): This strategy involves moving applications and data from on-premises to AWS without significant modifications. It offers rapid migration, but may not leverage the full potential of cloud services.</p><p>2. <strong>Replatform</strong> (lift, tinker, and shift): This approach involves making minor adjustments to applications for better compatibility with AWS services while maintaining core architecture. It offers a balance between migration speed and optimization.</p><p>3. <strong>Repurchase</strong> (drop and shop): Organizations replace their existing applications with new cloud-native alternatives available in the AWS marketplace. This strategy is suitable when existing applications don’t align with cloud best practices.</p><p>4. <strong>Refactor / Re-architect</strong>: In this strategy, applications are redesigned or rebuilt to take full advantage of AWS cloud services. It offers the highest level of optimization and scalability, but demands more development effort.</p><p>5. <strong>Retire</strong>: This involves identifying and retiring redundant or obsolete applications, reducing operational overhead, and costs.</p><p>6. <strong>Retain / Revisit</strong>: Some applications might not be immediately suitable for migration due to complexity or compliance concerns. Organizations can choose to retain them on-premises or revisit migration options later.</p><p>Choosing the right strategy depends on business goals, application complexity, timeline, and desired cloud benefits. A well-planned migration strategy ensures a smooth transition to AWS while optimizing performance, scalability, and cost efficiency.<br> As we are considering 3-tier web applications, let’s focus on 2 migration strategies; <strong>Replatforming</strong> and <strong>Rearchitecting/Refactoring</strong>. The other strategy called <strong>Rehosting</strong> will not provide automatization and significant reduction in operational requirements that are currently an issue.</p><p><strong>Replatforming 3-tier web applications</strong></p><p>I will briefly explain how my team and I replatformed our application 3-tier application. As mentioned above, a 3-tier web application consists of three layers: presentation, logic, and data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dz93kCza5zlIVhGv" /><figcaption>image source: <a href="https://inui.io/basic-sap-terms/">https://inui.io/basic-sap-terms/</a></figcaption></figure><p>Naturally, we are considering <strong>Application Server </strong>and <strong>Database</strong> for migration as they currently require the most attention when it comes to operational needs and effort. For application server we have selected <a href="https://aws.amazon.com/elasticbeanstalk/"><strong>AWS Elastic Beanstalk </strong></a>and for the database platform, we have selected <a href="https://aws.amazon.com/rds/"><strong>RDS</strong></a><strong>. </strong>So let’s discuss the reasoning behind the decision.</p><p><a href="https://aws.amazon.com/elasticbeanstalk/"><strong>AWS Elastic Beanstalk</strong></a> offers a solution for replatforming our web application because of its ease of use, scalability, and management capabilities. By migrating to Elastic Beanstalk, we can modernize our application architecture without the complexity of managing the underlying infrastructure. In essence, Elastic Beanstalk offers support for multiple programming languages, as well as docker containers. Furthermore, it manages Elastic Load Balancer, autoscaling, and it is directly integrated with Cloudwatch.<br> A significant benefit is that the deployment process is done in the same way as on-prem. The web application is in Java so the WAR file has been built and deployed to the Tomcat server on Elastic Beanstalk in the exactly same way as before.</p><p>Regarding the database, the obvious choice was <a href="https://aws.amazon.com/rds/"><strong>PostgreSQL RDS</strong></a><strong>. </strong>By switching to RDS, our application is benefitting from Multi-AZ deployment which ensures high availability and fault tolerance. Furthermore, our application gains access to automatic backups, security features, and scaling capabilities such as the effortless creation of read replicas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/0*dccjwWrJdhv-z15d" /><figcaption>The infrastructure of the replatformed solution looked like as shown in the above diagram. The networking part was created using Terraform templates. It included VPC, Subnets, and Security Groups in 2 availability zones. While EC2 instances, autoscaling, load balancer, and S3 buckets for application deployments were managed by Elastic Beanstalk.</figcaption></figure><p>RDS was deployed using terraform scripts. It is worth mentioning that Elastic Beanstalk could have also managed it, however, we prefer full control over RDS.</p><p>With the above approach, we have successfully migrated and replatformed our application.</p><p>There are multiple <strong>improvements</strong> that we gained by re-platforming :</p><ul><li>With elastic beanstalk, autoscaling was gained with almost no effort, as well as monitoring the instances and logging on the application level.</li><li>With RDS there were numerous benefits like automatization of backups, point-in-time recovery, DB monitoring and etc.</li><li>Finally, the biggest gain was using managed services that reduced operational effort and provided the ability to integrate with other AWS services.</li></ul><p><strong>Refactoring/Rearchitecting to cloud-native development</strong></p><p>Refactoring and Rearchitetcing the solution, in essence, means that applications are redesigned or rebuilt to take full advantage of AWS cloud services. This sounds like a lot of unpredictable long-lasting work. That might be correct in some circumstances, however, before we start discussing the reasoning, let’s talk about serverless adoption strategies.</p><p>1. <strong>Leapfrog Strategy</strong>: A transformation approach where legacy systems are bypassed, and new technology is directly adopted to achieve rapid innovation. This involves skipping intermediate steps and adopting advanced solutions to gain a competitive edge.</p><p>2. <strong>Organic Strategy</strong>: A gradual modernization method that integrates new technologies into existing systems over time. This strategy focuses on incremental changes to enhance functionality and maintain stability, while accommodating evolving business needs.</p><p>3. <strong>Strangler Strategy</strong>: A migration plan involving gradual replacement of legacy components with cloud-native or serverless alternatives. New functionalities are added in a “strangling” manner until the legacy system is phased out, reducing risk and ensuring a seamless transition.</p><p><strong>How to choose an adequate strategy?</strong></p><p>All 3 strategies can be good or bad depending on the circumstances and timeline.</p><p><strong>Leapfrog Strategy </strong>can be useful, in cases where the only option is to bypass the application that has become hard or impossible to maintain. In that case, the best option is to rebuild the application using modern tech-stack and modern architectures.<br> Developing a fully cloud-native solution (or in other words Serverless) makes this strategy a good way forward because of instant scaling and pay-as-you-go benefits. However, in our case, we do not want to rebuild the application. Such a step would be unnecessary and rather expensive at this moment. What is expected is just an improvement of features on an already existing solution.</p><p>What we are looking for is better defined as a mixture of <strong>Organic &amp; Strangler Strategies. </strong>By using Elastic Beanstalk we are already using <a href="https://aws.amazon.com/cloudwatch/">Cloudwatch</a> to monitor the servers and the application performance. We can utilize Couldwatch logs and alarms so that we are completely aware of the application state. By using Cloudwatch which is a fully managed service, we started injecting certain managed services in order to improve the current application. With that said we have utilized <strong>Organic Strategy</strong>.<br> However, in order to provide new features to the customers while using current data it is necessary to do some <strong>refactoring and rearchitecting.<br> Strangler strategy </strong>does concern the actual improvement of already existing features of the application via the gradual replacement of certain legacy or bottleneck features of the application. In order, to create new features or replace existing ones utilizing cloud-native services, it is important to identify and create events from the web application.</p><p><strong>Rearchitecting for Event-Based architecture</strong></p><p><strong>Event-based architecture</strong> is a design approach where software components communicate through events, to trigger actions and responses. Events represent occurrences like user actions or system updates, enabling loosely coupled systems to communicate and scale efficiently. This architecture is based on modularity, flexibility, and real-time responsiveness by decoupling components. Such an approach results in easier maintenance and integration with other AWS Services. Decoupling already existing monolith 3-tier web applications, in order to be event-based, sounds like a serious undertaking. Well, we do not need to do it all at once but instead focus on gradual migration. With that said, it is important to identify which logical entities in the application should be considered so that we can create events on certain actions like create, update, delete, and so on.</p><p>In order to better explain the idea of how to identify the logical entities that should be aimed for the creation of events, I will need to explain the business concepts of our application.<br> Our application supports simple factory production processes where both customers and the staff are involved to be aware of the timelines and potential delays. The logical entity that the whole application is based on is the <strong>Task </strong>entity (similarly<strong> </strong>JIRA is based on the concept of task). Task entity has many changes during the process, such as created, scheduled, rescheduled, in-progress, completed and etc.<br> Changes on the task entity itself are important in order to create proper notifications to the customer staff, make 3rd party application integrations, data analytics, and so on.</p><p>In order to create events and listen to Task entity changes, there should be changes made on the Application level or Database level. The application level seemed quite complex as multiple parts of the application needed to be modified. However, Database changes can be implemented rather quickly on the table level using DB triggers.</p><p><strong>Introducing Event-based architecture on the monolith 3-tire web application</strong></p><p>Database triggers are predefined actions that automatically execute in response to specific events such as INSERT, UPDATE or DELETE. Using RDS for both PostgreSQL, MySQL, and Aurora(MySQL &amp; PostgreSQL) there is direct integration with AWS services like <a href="https://aws.amazon.com/s3/">S3</a> and <a href="https://aws.amazon.com/lambda/">Lambda</a>.<br> Data can be imported and exported via S3 which is amazing for scenarios like Data Lake integrations and etc. Using Lambda and <a href="https://aws.amazon.com/eventbridge/">EventBridge</a>, will enable the processing and preparing of data to be used by other AWS services. That feature opens up numerous possibilities for quick evolution of the application with new features.<br> With that said, as we are looking to create events out of the changes made to the Task table, <a href="https://aws.amazon.com/lambda/">AWS Lambda</a> integration sounds like the perfect match.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YR--oNKL4F2GIqdHLtW3Fg.png" /></figure><p>The final solution is presented in the above diagram. Let me briefly explain the workflow:</p><p>1. RDS triggers the AWS Lambda (EventCreatorLambda) and is passing the inserted/updated/deleted data as JSON.</p><p>2. Such data is reformated and passed as an event message to EventBridge’s custom event bus. Steps 1 and 2 are presenting the core of the solution for further expansion.</p><p>3. NotificationLambda and CustomerIntegration lambda will be triggered by certain message types that are sent to the event bus.</p><p>4. NotificationLambda will publish an SNS message on a certain topic and notify all subscribers that are interested in that kind of event.</p><p>5. CustomerIntegrationLambda will notify the customer application over API webhooks.</p><p>With the above solution, we are creating a platform not just for the current requirements, but also for further expansion. Steps 1 and 2 are presenting the event creation mechanism that will be the fundament of our future event-based rearchitecting.</p><h4>To be continued…</h4><p>Please stand by for the next article, with implementation steps and code discussion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ce9f2e326a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crayon-ai/replatforming-and-rearchitecing-with-serverless-event-based-architecture-approach-5ce9f2e326a6">Replatforming and Rearchitecing with Serverless Event-based Architecture Approach</a> was originally published in <a href="https://medium.com/crayon-ai">Crayon Data &amp; AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why do you still need to think of scalability when architecting Serverless apps?]]></title>
            <link>https://medium.com/@marinradjenovic/why-do-you-still-need-to-think-of-scalability-when-architecting-serverless-apps-a2e1f14e3eca?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2e1f14e3eca</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[sqs]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[sns]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Fri, 09 Dec 2022 07:45:32 GMT</pubDate>
            <atom:updated>2022-12-09T07:45:32.440Z</atom:updated>
            <content:encoded><![CDATA[<p>“Pay as you go” and “Unlimited Scaling” are the buzzwords that are often associated with Serverless when reading or hearing about it. Yet, we still need to think of scalability. <br>This article will focus on providing you the knowledge on how to approach the architecting of optimized Serverless solutions. It will guide you through a few sets of questions that you should consider when analyzing requirements and enable you to compile optimized solutions for your serverless workload.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/0*BwRtMXn5jWbYOgp3.jpg" /></figure><h4>What to do before you start scathing your solution</h4><p><strong>Functional requirements (FR)</strong> are the bases of any project development. It elaborates on what a certain solution should deliver as a functionality to the customers, however, it does not provide the full image.<br>In lots of cases, developers and architects just start solutionizing their FR without prior analysis of <strong>Non-functional requirements</strong> <strong>(NFR)</strong>. NFRs are very important because they concern expectations such as reliability, security, performance, scalability, cost, and overall sustainability. Most of us have witnessed problems like “applications are not performing well enough”, “infrastructure is just too expensive for the amount of work it does” or “Serverless is just too expensive”. In other words, the application does what it needs to do but not as expected. <br>Well, those problems come mostly from <strong>underestimating</strong> or <strong>overestimating</strong> NFRs, or just <strong>neglecting</strong> them. One of the most important aspects, when architecting an application at scale is to analyze NFRs.</p><blockquote>NFRs, such as, “expected response rate” or “required type of communication (synchronous or asynchronous)” might entirely change the architecture of certain solutions.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*GUI78keKlg-AYIms.jpg" /></figure><h4>Where do I get NFRs?</h4><p>NFRs are not always available like FRs. Sometimes, you get NFRs from the client but more often from your own analysis of the existing solution. However, if it’s a greenfield project then you would probably need to do an analysis together with the stakeholders and then give a knowledgeable guess. But try to use common sense and realistic predictions so that your optimization of the future architecture is adequate.</p><h4>NFRs Analysis</h4><p>In order to execute this analysis, I would point out a few questions in 3 categories. Those questions may guide you to properly analyze future Serverless workloads and create a foundation for your Serverless architecture.</p><h4>Data type &amp; size questions</h4><ul><li><strong>What kind of data are you processing?</strong> — Does your message has only one receiver or should it be handled by multiple receivers? Is your data a message or an event? Based on that, you may determine the best-suited AWS services. If the messages have only one consumer then you may think of <a href="https://aws.amazon.com/sqs/">SQS</a> or <a href="https://aws.amazon.com/kinesis/">Kinesis</a> depending on the purpose. <br>If it’s an event that should be processed by multiple consumers then <a href="https://aws.amazon.com/sns/">SNS</a> or <a href="https://aws.amazon.com/eventbridge/">EventBridge</a> might be your choice.</li><li><strong>What is the size of the expected payload?</strong> — Knowing the size of your payload is of great importance when selecting certain AWS Service. Some AWS services support the transfer of larger-sized payloads, but some can bare payloads only measured in KB. However, data sent as a message or event is not intended for large-sized data transfers. <br>For instance, <a href="https://aws.amazon.com/kinesis/">Kinesis</a> can have a message of 1MB while SNS or SQS can have a message size of up to 256KB. So, have in mind what size of payload would you like to send. In case you need larger size payloads, then you can store it as a file on an S3 bucket and send a path to the file over a message or event.</li></ul><h4>Throughput Questions</h4><ul><li><strong>At what pace is the data coming?</strong> — It is very important to understand the expected speed of consuming and processing data. Let’s say that whatever you pick from AWS Services to ingest your data like SQS, SNS, Kinesis, <a href="https://aws.amazon.com/dynamodb/">DynamoDB</a>, <a href="https://aws.amazon.com/eventbridge/">EventBridge</a>, etc. will properly scale, but your processing might not scale accordingly and you may see quite high latency. <br>Let’s say your app is producing data from Source A and Source B. However, data coming from source A, produces multiple times more data than source B. All of the data is streamed via Kinesis to lambda for processing, which means that data ends up in the same pipe. In case the buffer is overwhelmed by data from source A, processing of Source B data will experience severe latency even though there is much less data coming from source A. In that situation, it is much better to have separate streams according to priority.</li><li><strong>What are desired response times?</strong> — It is very important to understand if your applications require some strict response times. Therefore, you need to be aware of how your services scale and their latency in the process. In the case of Kinesis + Lambda, if your lambda processes much slower than expected (at a constant load) it will fill up the buffer quickly and build up latency over time. You can monitor this by looking for <em>iterator age</em> metrics.</li><li><strong>In case data is coming at high rates, how to speed up processing? — <br></strong>When it comes to <a href="https://aws.amazon.com/kinesis/">Kinesis</a>, an option to speed up processing is by processing in batches. If you need to process data in batches then Kinesis would be probably the easiest option. When working with lambdas and Kinesis, you define a batch that would trigger your lambda. In larger batches, the messages require a different amount of time to be processed, then larger batches might have unpredictable amounts of time to be processing completition. That may lead to lambda timeout which may cause the whole batch to fail. Then your lambda will retry to process the same records again which may cause issues to none idempotent services. <br>So you would need to optimize or mark the records that were processed successfully from the original batch.<br>Also, you can use enhanced fan-out so that lambda invocations are not limited to one shard.<br>When it comes to EventBridge, lambda invocations are limited by default to quota limit for certain regions. You can check the limits on the address <a href="https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html#eb-invocations-limits">https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html#eb-invocations-limits</a><br>SQS on the other hand is scaling lambdas at a rate of 60 lambdas per second up to 1000 lambdas. Be aware that you may hit the soft limit of lambdas quickly in case you have so much data in the queue. This will result in other parts of the application falling due to hitting the soft limit. So mind your soft limits and request quota increases beforehand.</li><li><strong>Is it a constant load, are there any spikes? — </strong>Most AWS services are made to scale. Kinesis can ingest 1000 rec/s per shard with a soft limit of 500 shards. SQS can handle virtually an unlimited number of messages (FIFO up to 3000 a sec). However, your processing side might cause latency, which is ok if the business does not require faster processing.<br>If there is a constant load you may think of reserving concurrency on the lambda side or even switching to <a href="https://aws.amazon.com/fargate/">Fargate</a> (if it makes sense price vise). Also, you can throttle requests if the processing isn’t time critical.</li><li><strong>Are the spikes predictable or unpredictable? — </strong>This question is an extension of the above 2 questions. Luckily we don’t need to think of peaks anymore when using Serverless. But, we need to consider the scaling abilities of data producing/consuming 3rd party dependencies (such as 3rd party services/libraries). Try to cache or degrade the service in those cases. Let the user know that certain services are unavailable at the moment or use cached data (by notifying the user that the data is stale).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/0*0jhRI4ufavg2Uzqy.jpg" /></figure><h4>Fault Tollereance Questions</h4><ul><li><strong>How important is the payload of certain messages or events? Is certain loss acceptable or not? — </strong>This question is very important as certain services guarantee delivery like SQS, while for instance, SNS does not have strong delivery guarantees and neither do Kinesis and EventBridge. Due to that fact, you should investigate how certain messages NOT being delivered effects system functioning. SQS ensures that messages are delivered at least once, but might be more than once. Therefore, you need to ensure that your function is idempotent. <br>In case your system is transactional and requires every transaction to be in order and delivered then you have the option to use SQS — FIFO. SQS-FIFO guarantees to deliver messages only once and in order, however at lower throughput than regular SQS.</li><li><strong>Are your functions idempotent?<br></strong>If you are using SQS you may get one message more than one time. Let’s say your SQS message has a payload that carries information about adding a new user to the DB. In case you got that message multiple times, you might get that many records in the database.<br><em>An </em><strong><em>idempotent</em></strong><em> function in computing is a function that you call more than once with the same input parameters, it will have no additional effect.<br></em>You need to ensure that your services are providing exactly the same result as many times they are called. That is quite obvious when it comes to GET, PUT and DELETE (POST is not considered to be idempotent).</li><li><strong>Are there any integrations with other systems and can they be a bottleneck? —</strong> I have already touched on this topic in the question “<em>Are the spikes predictable or unpredictable?</em>” Absolutely the same applies to this question, so you need to analyze 3rd party dependencies such as libraries and services and be aware of their shortcomings. <br><em>When the core services are dependent on the 3rd party service, then all of the solution performance and reliability are reliable as the weakest link.</em><br>3rd party dependencies that are expected to create some disruptions such as low throughput, often reliability issues or similar, need to be replaced with more reliable service. In case there is no such service there are tactics that can be implemented to avoid bad user experience. You can check the below questions regarding the error handling.</li><li><strong>Is error handling required?</strong> — Sometimes certain loss of messages is acceptable, but sometimes it is very important that messages arrive successfully as they are crucial for the overall functioning. There are many ways where a certain percentage of messages might be lost in transit. It can happen due to different circumstances, for instance, deployment errors, human errors, configuration issues, service unavailability, etc. <br>There are certain tactics that can help save data or notify users that something is out of order atm.<br> <strong>Circuit Breaker</strong> pattern is a pattern that is allowing, disallowing, or partially allowing throughput due to high traffic to overwhelmed service. Which can help to warn users that something is wrong. It can also warn other services that it needs to either report the issue or mitigate the issue by redirecting the data temporarily to certain storage. Such a tactic is called <strong>DLQ</strong> <strong>(Dead letter queue)</strong>.<br>SQS, and SNS are having DLQs. In case the message is not delivered, the messages are stored in DLQ and reside there till it is expired or is deleted. DLQ can be emptied in different ways automatically by application logic or manually. <br>EventBridge also is using SQS as DLQ, so the same applies as above mentioned.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/0*Wep79KnYd2IhVX67.jpg" /></figure><h4>Conclusion</h4><p>There are probably many more questions that you can ask yourself when designing certain solutions. However, the above questions are giving you a foundation in how to set your mind for well-designed Serverless architecture.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2e1f14e3eca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Halloween Serverless Stories — final]]></title>
            <link>https://awstip.com/halloween-serverless-stories-final-442ce4f37a60?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/442ce4f37a60</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Tue, 01 Nov 2022 06:49:36 GMT</pubDate>
            <atom:updated>2022-12-14T14:33:22.506Z</atom:updated>
            <content:encoded><![CDATA[<h3>Halloween Serverless Stories — final</h3><p>The serverless architectures are virtually backed by unlimited storage and processing power. Eventually, at the end of the month, you have to pay what you used. <br>One of the most horrific issues in serverless that can happen is very high unpredictable cost. Most such issues occur due to improper use of AWS services, however, they might happen also by attacks.</p><p>Also please if you didn’t read the previous parts here they are:</p><ul><li><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-3703c4303bb4">Halloween Serverless Stories — Part 1</a></li><li><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-part-2-22266d852d9">Halloween Serverless Stories — Part 2</a></li><li><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-part-3-9318327ce258">Halloween Serverless Stories — part 3</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bVpwczHJpHkiy84u" /><figcaption>Photo by <a href="https://unsplash.com/@dallehj?utm_source=medium&amp;utm_medium=referral">Daniel Jensen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Story 6 — Denial-of-Wallet (DOW)</h3><p>DDOS attacks, try to disrupt the application&#39;s health. The intention is to flood the server with rubbish traffic until it crashes. The outcome is that your service will be distributed by the time when the attacks are happening and if you are not able to mitigate the attack it may drive customers away.</p><p>With the appearance of Serverless, attackers got creative and come up with different, more malicious attacks. Their intention is not to disrupt the service but to <strong>bankrupt the owner</strong>. Denial-of-Wallet attacks exploit the fact that serverless vendors charge users according to the number of resources that are consumed by an application, meaning that if an attacker floods a website with traffic, the site owner could be landed with a huge bill.</p><p>Fortunately, no one in my surrounding didn&#39;t face this attack yet, but it does not mean that they won&#39;t. We need to be aware of such attacks and be prepared if they happen. Therefore this is not going to be a story, but more like brain food for your future projects.</p><p>Denial-of-Wallet (DOW), as we said before, is an attack that tries to bankrupt the owner of a Serverless application. There are multiple ways how to achieve that. Let me discuss a few possibilities that I consider plausible.</p><p><strong>Attack by retries</strong><br>If let’s say your application is running lambda that does some processing using a Kinesis stream that keeps the data for 24 hours or even more. So in case of an attack sends the data over a stream that crashes your lambda. Your Lambda will continuously retry to process that data as long as it is available on stream (didn’t expire). In case Lamba is maxed out (memory and duration) it may generate a huge bill if stays in the loop for some time.<br>In order to prevent such attacks, you need to be aware of those using C<strong>loudwatch alarms </strong>and develop your functions to<strong> investigate the load before processing it</strong>. For instance, you can check the load length if it is in the range of acceptable size, and does it contains data that your application requires. If it does great, but if it doesn’t skip it and report by logging or SNS.</p><p><strong>Slow HTTP attack (Slowloris)</strong><br>This is a DDOS attack that opens multiple connections and keeps connections open by sending requests at a slow rate. In case servers are not timeouting the connections from their side, the connection might be continuously opened. <br>Let’s say you are having an API with Lambda functions. Using API Gateway, the connection will be limited to 30 seconds. However, if your lambda is exposed over ALB (depending on configuration) or LambdaURL, it might be open for a long time. Such a long-time open connection will keep lambda waiting.<strong> Waiting in lambda at scale will seriously increase the cost.<br></strong>It is very hard to defend against those attacks, but it is important that you <strong>DO NOT</strong> max out your lambdas in case it is not required and be prepared using<strong> Cloudwatch logs or X-Ray to monitor and alarm the increased duration of your lambdas. </strong>You should be also protected by AWS Shield or Cloudfront as I found this saying in AWS documentation “CloudFront can automatically close connections from slow reading or slow writing attackers”. However, I am not sure how it works.</p><p><strong>Complex regex patterns — ReDoS<br></strong>Similarly to the previous attack, this attack is trying to keep lambda busy as long as it is possible at scale. For instance, in the time of AJAX, an attack called billion-laughs-attack was targeting XML processors. With the introduction of JSON, that attack fell behind, but still exists for YAML. <br>In pretty much the same way, Serverless architecture can be attacked with specific regex complex phrases.<br>Let’s say you are validating an email on a specific API using regex.</p><pre>Regex:<br>^<strong>(([a-z])+.)+</strong>[A-Z]([a-z])+$</pre><pre>Input:<br>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!</pre><p>The input looks completely valid. However, the above phrase requires significant CPU power and duration in order to be validated. You can read <a href="https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS">here</a> about the details.</p><p>It is very hard to find the best way how to defend from this type of attack. Like with the previous the best way is again to be informed about unexpected behavior using <strong>Clouwatch and X-Ray</strong>.</p><h3>Story 7 — Denonia Miner</h3><p>One of the scariest attacks is by Denonia Miner. It is written in Go. The malware contains and runs a customized version of the XMRig cryptocurrency mining software in memory. It mines Monero, but I believe can be valid for other cryptocurrencies.<br>It is not quite sure how it is being installed in somebody’s account, but it is believed that happens over <strong>compromised account keys</strong>.</p><p>Denonia is using DOH or DNS over HTTPS so that is very hard for AWS to trace malicious domains it tries to communicate with. <br>At this moment it is very hard to track this issue as it can’t be found by:<br>1. CloudTrail: This miner doesn’t make AWS calls <br>2. DNS: This uses DNS over HTTP <br>3. VPC Flow Logs: Potentially can be used if IPs are discovered, but that is not likely.</p><p>The important thing to remember is that it can be installed in any region, so if you don’t see it yours, that doesn’t mean that it isn’t installed.</p><p>Fortunately, I don’t have a story on my own regarding this issue, but here are some tweets that I found which are scary enough.</p><blockquote>“I&#39;m a small time business owner and over $56K in charges! Merry christmas! I was sick in bed from the Covid booster and missed the first email on 12/27. In contact with aws support as soon as I found out on 1/1. Really stressed and worried”</blockquote><blockquote>“I received a bill 13.000.$ but i don&#39;t know why. My account was hacked from 6th December. Until last sunday i can&#39;t enter to my account. I don&#39;t know what kind of services have been activated and i don&#39;t know how can deactivated them. My informatic knowns are small.”</blockquote><p>Unfortunately, I don’t have much information regarding Denonia Miner as I would like to have but it is something that you should be aware of.<br>AWS&#39;s position is also quite hard as they do not consider it malware as it is installed by compromised accounts.</p><blockquote>“Since the software relies entirely on fraudulently obtained account credentials, it is a distortion of facts to even refer to it as malware because it lacks the ability to gain unauthorized access to any system by itself.”</blockquote><p>The best article I have found is this one, so please go ahead and read it.<br><a href="https://www.cadosecurity.com/cado-discovers-denonia-the-first-malware-specifically-targeting-lambda/">https://www.cadosecurity.com/cado-discovers-denonia-the-first-malware-specifically-targeting-lambda/</a></p><p>The best way to be protected is to use <strong>MFA</strong>, and rotate the keys quickly. Also, as your PC might be compromised, the best way is to limit your account to the <strong>region</strong> and <strong>console acces</strong>s only and all deployment should be done in an automated way using CI/CD approach.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=442ce4f37a60" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/halloween-serverless-stories-final-442ce4f37a60">Halloween Serverless Stories — final</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Halloween Serverless Stories — part 3]]></title>
            <link>https://medium.com/@marinradjenovic/halloween-serverless-stories-part-3-9318327ce258?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/9318327ce258</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[antipattern]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Sun, 30 Oct 2022 05:31:26 GMT</pubDate>
            <atom:updated>2022-11-04T07:52:30.965Z</atom:updated>
            <content:encoded><![CDATA[<h3>Halloween Serverless Stories — part 3</h3><p>This is 3rd part of Halloween serverless stories that are trying to explain the issues that might happen due to misunderstanding some of the AWS serverless services’ pricing, behavior &amp; limits.</p><p>If you didn’t read the previous parts, here are the links.</p><ul><li><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-part-2-22266d852d9">Halloween Serverless Stories — Part 2</a></li><li><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-3703c4303bb4">Halloween Serverless Stories — Part 1</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7DY9GBdktujCTwzw" /><figcaption>Photo by <a href="https://unsplash.com/@heftiba?utm_source=medium&amp;utm_medium=referral">Toa Heftiba</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s talk about the misbeliefs that lambda is <strong>slow and expensive</strong>. I have a few stories regarding this topic, but I selected two as the most adequate.</p><h3>Story 4 — Slow Java lambda</h3><p>Many developers found the “pay as you go model” quite comforting for use cases where they were paying for an instance that was occasionally used. <br>Conversion of microservices that were running on an EC2 instance or AWS Beanstalk could be rather simple. In case the microservices are written in Java, just replacing the <strong>main method</strong> with the <strong>handler method</strong> could finish the job. <br>However, microservices usually are not built to run as lambdas. They are much thicker (functionality-wise).</p><p>So the story is about the team that had microservice architecture with quite a few microservices. They were running them on AWS Beanstalk behind a load balancer. The whole architecture was quite satisfactory, performance-wise, but the cost was something they didn&#39;t like… Also, the maintenance of infrastructure was something they didn’t like, even though greatly reduced by AWS Beanstalk. Microservices are built in Java using <a href="https://spring.io/">Spring Framework</a> more exactly <a href="https://spring.io/projects/spring-boot">Spring Boot</a>.</p><p>Anyway, they decided that they will greatly reduce the cost and the price by switching to Lambda. Refactoring took a considerable amount due to reasons that some of the APIs were moved to API Gateway and automated testing was rather difficult to be modified, but they did it.</p><p>When they finally did it, the results seemed fine. However, after deployment on production, problems started to appear. Unfortunately, as they lacked proper monitoring and alarming, users saw the problems first. They were complaining and reporting the issues but the team was unable to reproduce them. Even worse, the price was much higher than with Beanstalk. That led to huge dissatisfaction among the users and at the management level. Developers started blaming technology for the issues, quite clueless about where exactly is the problem and what should they do. So they decided to go back to Beanstalk and dropped a few months of effort to convert microservices into lambdas.</p><p>The aftermath was quite interesting…. Well after they got introduced to the concept of lambdas by examining the issues they saw that most of their issues happened due to cascade failures at different times. Cascade failures mostly happened because of <strong>out-of-memory </strong>and <strong>timeouts. </strong>So one lambda calls another lambda or another few lambdas which due to the demanding <strong>Spring Boot </strong>framework (<strong>Inversion of Control </strong>concept) used a lot of memory and time to start. <strong>Timeouts</strong> happen due to demanding tasks that were given to the lambda so there was no time to finish even though the limits were maxed.</p><blockquote>Overall, migrating from microservices to lambda might be a simple task, but in general, it requires knowledge of serverless services and an broad understanding of workload non functional requirements in order to decide what is the best technology choice.</blockquote><h3>Story 5 — Lambda recursive calls (antipatterns)</h3><p>You may often hear “I would be able to deploy the same functionality on EC2 for much cheaper”. That saying might be true if you use lambda improperly or for an unintended purpose. <br>Lambda is sometimes wrongly accused as very expensive. But that is not an honest comparison if, for instance, you compare 1 minute of Lambda with 1 minute of the same size EC2 instance. What is not taken into consideration is the amount of work you put into the maintenance of such deployments or in other words the overall cost of ownership if your application grows.</p><p>One of the antipatterns in Serverless development is calling one lambda from another lambda or calling lambdas recursively. Both cases may trigger unnecessary costs but also compromise application health.</p><p>Namely, team X had an idea to use lambdas in order to process some files on the S3 bucket. Simply, the lambda was triggered when there was a new file uploaded on the S3 bucket. The S3 bucket would trigger lambda to process the uploaded file.<br>As the files were quite small, the whole idea worked flawlessly. Things started to get complex when the files got bigger as more memory and duration were required. As long as they were able to increase duration and memory it was fine. The real problems occurred when there was no room for vertical scaling of lambdas anymore. So the team had to come up with a plan to continuously process the whole file as huge as it is. <br>You guess, they called lambda recursively as long as the file was completely processed. That idea was quite <strong>costly</strong> as lambdas were running continuously. <br>However, that wasn’t the biggest problem. The problem was that on some occasions files weren’t processed correctly which triggered a <strong>recursive call</strong>. Recursive calls might consume all of your lambda concurrency so other lambdas will be impacted by it and it will significantly <strong>increase the cost</strong> if not found on time.</p><p>Final Part… the scariest one…</p><p><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-final-442ce4f37a60">Halloween Serverless Stories — final</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9318327ce258" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Halloween Serverless Stories — Part 2]]></title>
            <link>https://medium.com/@marinradjenovic/halloween-serverless-stories-part-2-22266d852d9?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/22266d852d9</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless-architecture]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Wed, 26 Oct 2022 07:29:19 GMT</pubDate>
            <atom:updated>2022-11-04T07:50:55.137Z</atom:updated>
            <content:encoded><![CDATA[<h3>Halloween Serverless Stories — Part 2</h3><p>This time we have another story that is so stuck in my mind for two reasons. One reason is knowing that you can’t do anything smart at the moment and you need to let the disaster happen. Second is that I and the team learned a lot from the errors on how to handle serverless applications.</p><p>If you didn’t check 2 stories in the previous article, here is the link</p><p><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-3703c4303bb4">Halloween Serverless Stories — Part 1</a></p><p>There are one more now!! 🎃</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5IUcoh8ytR039tJD" /><figcaption>Photo by <a href="https://unsplash.com/@neonbrand?utm_source=medium&amp;utm_medium=referral">Kenny Eliason</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Story 3 — Big Bang</h3><p>This story happened a few years ago…<br>Namely, there was a SaaS marketing solution that particular mobile phones producer used for their marketing campaigns. However, as the company had multiple marketing channels managed by different applications, there was an enormous amount of material to be copied from one application to another. Also that SaaS application was also costly. <br>We had a bright idea to utilize the content of CMS publications and convert it into an email channel. So the marketeer would use just one application, which will significantly reduce the amount of work and frustration.</p><p>So we have modified CMS which was a monolith application and extended it for new features, such as the creation of email content, setting targets, selection of segmentation filters and etc.</p><p>The email campaigns were planned to be from a few hundred thousand emails to several million. The acceptable throughput was 1m emails per day. We have tested and confirmed that it works according to the required NFRs or better. So we were so happy about our solution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZZJWjIRQoRdnf1IE.png" /></figure><p>Everything worked perfectly for quite some time. We were able to send more than 5 million emails per day.<br>However, one day while having a few long-running global campaigns in parallel, marketers wanted to have an urgent one-day Valentine’s day offer.<br>So they published it…<br>Unfortunately, stats were showing no deliveries for more than a few hours. There was no email coming out for a whole day? Marketing managers were in a panic, more than half-day has already passed and no email has gone yet.<br>When we checked the logs everything was functioning perfectly, there were emails coming out but not for Valentine’s day campaign.<br>We have checked the campaign and everything seemed fine, except for the <strong>iterator age </strong>on kinesis that was triggering Email Sending Lambda. The iterator was 156960405 ms old or almost <strong>2 days old</strong>. So if you publish a campaign right now, you would get the first emails coming out in 2 days :)<br>There was nothing smart we could do at that time. You may say “you could just re-shard it and scale it up” but kinesis re-sharding is something that we avoided for a good reason…<br>So we had to let the campaign fail and break so many hearts for Valentine’s day :(<br>Customers received emails two days after Valentin’s day had passed.</p><p>You can read the whole story on this link.</p><p><a href="https://medium.com/@marinradjenovic/controllable-scaling-in-serverless-big-bang-9a209a56af5b">Controllable Scaling in Serverless Big Bang</a></p><p><strong>You can continue to part 3…</strong></p><p><a href="https://medium.com/@marinradjenovic/halloween-serverless-stories-part-3-9318327ce258">Halloween Serverless Stories — part 3</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22266d852d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Halloween Serverless Stories — Part 1]]></title>
            <link>https://medium.com/@marinradjenovic/halloween-serverless-stories-3703c4303bb4?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/3703c4303bb4</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Sun, 23 Oct 2022 09:58:49 GMT</pubDate>
            <atom:updated>2022-10-26T11:49:45.030Z</atom:updated>
            <content:encoded><![CDATA[<p>Most of us, who are working Serverless, find it irreplaceable. There are so many pros on which we are hooked nowadays, that it is hard to switch back. However, some people, on their quest for a successful Serverless application, have seen its<strong> dark side</strong> 🎃 👹 ☠️ ….</p><p>Building a Serverless application can be pretty neat and may give us a false impression of a very low learning curve. But remember, you are developing an application that is deployed on the most potent computational/storage platform that ever existed on the planet. Having in mind that you are paying for what you use, and if you are wasting it, it can be extremely expensive…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Fnif1UtELtZsuGK_" /><figcaption>Photo by <a href="https://unsplash.com/@freestocks?utm_source=medium&amp;utm_medium=referral">freestocks</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let me tell you about a few stories that happened to me, my colleagues or I read it somewhere.</p><blockquote>Some of them are very scary, so make sure you don’t read them before sleeping…<br>You may find yourself checking the account(s) in the middle of the night.</blockquote><h3><strong>Story 1: Most expensive cost awareness</strong></h3><p>A friend of mine and his team were working on some Serverless applications for some startup. The client was so worried about the costs, as they are very tight with the budget.<br>They decided to create a reporting tool so that the client can be aware any second how much cost is created. The client was overwhelmed by the idea of having a report of the costs anytime he checks.</p><p>They know that AWS Cost Explorer is the service they need. It has the proper API and it seems like they have exactly what they need. Together with the client, they decided that slack reporting would be very easy to integrate and available to both stakeholders and the team.<br>So.. rather easy… anybody could do it!</p><p>The team started development…</p><p>Everything went so smoothly, the API was self-explanatory and worked as expected. Slack was also very simple to be integrated, with just one POST request. It is going to be done in a couple of hours so the team can go on a relaxed long weekend ☀️🏖 <br>One developer had an amazing idea, to report only when there are changes to the costs, not to bother every few minutes.<br>They called Cost Explorer API very frequently (I am not saying they have put it in a loop 🤣) and then compared it with the previous state.<br>Nobody needed any documentation, everything was very easy.</p><p>They deployed it and left for a long weekend to relax and enjoy beautiful summer…</p><p>Nobody checked slack for a few days. <br>The client didn’t even install slack 🤣</p><p>When they got back in the office, the client’s a bit disturbed voice, gently notified them that the bill over the weekend was raised by 8000$ and their monthly bills were at an average of 50$ — 70$. That woke them up better than any horror movie before.</p><p>After some investigation, they have realized that they are using <strong>paid</strong> services!!! When you call a certain API on AWS it has its price.</p><blockquote>The price of API Cost Explorer is 0.01$ per request.</blockquote><p>Imagine if you call that service in a loop and leave it for a few days, to realize that that service is updated once or twice a day 🤣</p><p>Luckily AWS reduced significantly that cost so, in the end, it all went well. But it remained planted in the developer&#39;s brain to check the prices before they started using any AWS service carelessly.</p><h3>Story 2: Malicious 3rd Party Library</h3><p>Everyone wants to do their job as quickly as possible. So we, as developers, tend to use libraries in order to shorten our development time. However, apart from libraries being buggy, they might be malicious also.</p><p>This is not really a story but more heads-up notice. So let’s say that you use a library that wraps some APIs and creates an SDK or some algorithm that you would have to implement yourself otherwise. However, that library might do the job perfectly but on the other side, it might be malicious also. It might carry some code that for instance mines some cryptocurrency. That might be a very big issue in case your app scales a lot. You may provide some of your processing power for somebody&#39;s interest, but you might not be aware of that.</p><p>Here is an example of how easily this can happen. Let’s assume you use <strong>nodejs</strong> and have stated in your <strong>package.json</strong></p><pre>&quot;dependencies&quot;: {<br>  &quot;some_dep&quot;: &quot;^1.0.0&quot;,  <br>  &quot;another_dep&quot;: &quot;~2.2.0&quot;<br>}</pre><p>So let’s say you checked utterly <strong>some_dep 1.0.0 </strong>and you are convinced it does exactly what you need it to do. <br>But…. Guys that developed <strong>some_dep 1.0.0 </strong>have malicious intentions. So they saw that they have so many downloads, and so many people are using the library. They decide to update the release with <strong>some_dep 1.0.1 </strong>stating that it is a bug fix in the change log. Next time you build a new version of your app, it will carry malicious code.</p><blockquote>In the <strong>package.json</strong> you stated that you are ok with updating for patch release (^) or minor release (~). That may leave an open door for potential attacks.</blockquote><p>In essence, you should be aware of the libraries that you find convenient for your app and make sure that they are developed by some trusted, well-known publisher.</p><h3>To be continued…</h3><p>There will be more stories coming up till Halloween, so stand by for some more scary stuff. 👻👻👻👻</p><p><a href="https://medium.com/p/22266d852d9">https://medium.com/p/22266d852d9</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3703c4303bb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating multiple SSO ecosystems with JWT token by conversion in transit]]></title>
            <link>https://towardsdev.com/integrating-multiple-sso-ecosystems-with-jwt-token-by-conversion-in-transit-4ab20ced83fc?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ab20ced83fc</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloudfront]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[sso]]></category>
            <category><![CDATA[jwt]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Tue, 11 Oct 2022 11:19:21 GMT</pubDate>
            <atom:updated>2022-10-21T10:45:07.221Z</atom:updated>
            <content:encoded><![CDATA[<p>As the IT world rapidly grows, applications are becoming more and more complex. One company buys another company and they tend to integrate their solutions, which most of the time requires lots of effort, time, and money.</p><p>This article will try to explain, how SSO can be achieved using JWT. It will try to explain how multiple SSO ecosystems can be integrated by utilizing the AWS CloudFront and the services that it integrates with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y6byS8sgMeMQBCJx" /><figcaption>Photo by <a href="https://unsplash.com/@alexas_fotos?utm_source=medium&amp;utm_medium=referral">Alexas_Fotos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Users are demanding more straightforward and intuitive application usage. So, they need applications to be integrated and share data between applications. Imagine Google&#39;s ecosystem of applications not sharing data with each other. For instance, you open an email in Gmail that contains a link on Gmaps. When you click the link, you need to sign in again to Gmaps, even though they belong to the same ecosystem of applications. That is a rather awkward user experience.</p><p>In essence, users need a simple and uninterrupted flow of application usage with minimal effort.</p><p>The first thing users expect from a certain application is not to remember credentials for any of the related systems that belong to the same ecosystem. Google user does not want to copy and paste data and update user details in every Google app individually. They would require to sign in just once and to be continuously signed in when jumping from one application to another.</p><h4>SSO &amp; JWT</h4><p>To be fair, solutions that enable users to sign in once and access services without re-entering credentials exist for quite some time. Such an authentication schema is known as <a href="https://en.wikipedia.org/wiki/Single_sign-on">Single Sign-On (SSO)</a>. <br>Currently, there are multiple solutions that follow that paradigm. Also, a few standards are derived from that paradigm, such as OpenId, SAML and etc.</p><p>In this article, I will concern with an aspect of SSO based on JWT <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">(JSON Web Token).</a></p><blockquote>JSON Web Token (JWT) is an open standard (<a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a>) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. It can also be encrypted to provide more security in case some sort of sensitive data is transferred.</blockquote><p>In essence, JWT is stateless information of successful authentication, that can be validated by multiple servers autonomously. As such, it represents a powerful tool to achieve SSO.</p><p>Nowdays, you can find JWT implementations fundamental for SSO authentication between the ecosystem of various applications.</p><p>In the proceedings of this article, I will try to explain deeper about the JWT token and its benefits. Also, I will try to explain how it can be achieved using AWS Services.</p><h4>Anatomy of JWT token</h4><p>JWT Token consists of 3 sections: <strong>header</strong>, <strong>payload</strong> &amp; <strong>signature</strong>. <br>* <strong>Header</strong> — consists of an algorithm used for the signature and type of the token.<br>* <strong>Payload</strong> — consists of user data that can be useful to the server when validating the token. Such as <strong>iat(</strong>issued at)<strong>, role, username.<br>* Signature</strong> — is the result of Header and Payload, concatenated and then encrypted with the private key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BOaFJUOHZu2FCuiI.png" /></figure><h4>How is SSO achieved via JWT?</h4><p>I am going to try to run through some real-case scenarios regarding the usage of the JWT token.</p><p><strong>General Usage of JWT <br></strong>Assuming a scenario where we have a client (browser) and Server (backend app). After successful login, the application issues the JWT token. The issued JWT token is sent as part of the request, more particularly, as an authorization header in every interaction between a user’s browser and server. After validation of the JWT token by the server side, a successful response is returned. You can check the sequence diagram below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/0*fXVMePK9OCchrtWf" /></figure><p><strong>An ecosystem of applications connected in SSO</strong><br>Let’s take another example where we have multiple related but autonomous applications that are unaware of each other. As they are unaware of each other, they do not share information (session) if the particular user is logged or not. In order second application to successfully authenticate the user, it needs to validate the token issued by the first one.</p><p>Such related but autonomous applications connected by SSO are part of a bigger system called an <strong>ecosystem.</strong></p><p><strong>Example<br></strong>Let’s assume we have 2 applications Application A and Application B. User uses Application A to authenticate and gets the JWT token in return. Now, he tries to access a resource on Application B just by adding the token in the header issued by JWT. Application B verifies the JWT token issued by Application A and provides access to the user. You can check the diagram below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*sXLNE15DNGg7BNxW" /></figure><p>How is Application B able to verify the JWT token issued by Application A, if they are autonomous? Well, easy… If Application B can verify the signature that is coming from Application A and the token is not expired (based on the issue date), then it can confirm successful authentication on Application B. Signature can be done using hashing algorithms, which produce a digest that is verified/compared by the destination system.</p><p><strong>Connecting two independent SSO ecosystems</strong><br>Assuming that we have an ecosystem, successfully integrated into an SSO using a JWT token that utilizes the same hash algorithm. By moving from one system to another system, your JWT token will be valid and you will be automatically signed in.</p><p>But what if we have a parallel ecosystem that also uses JWT (or a similar approach) and we need to integrate that ecosystem into our existing solution that already operates with a JWT token?</p><p><strong>Example</strong><br>Just for simplicity, let’s assume two companies have merged. One has microservice architected DMS, while the other has CRM. Those two ecosystems are functioning in the above-mentioned way. However, as they are different SSO systems, they do <strong>NOT share the same key</strong>. In order to integrate, those two systems, we would need to transform and then verify and re-sign the token in transit. <br>There needs to be a function that will convert JWT tokens on the fly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*cbY5k1AkFFvZaPziQNfvSA.png" /></figure><p>Assuming we use <a href="https://aws.amazon.com/cloudfront/">Cloudfront</a>, in order to do the conversion, we have two options:</p><ul><li><a href="https://aws.amazon.com/lambda/edge/"><strong>Lambda @ Edge</strong></a></li><li><a href="https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/"><strong>Cloudfront Functions</strong></a></li></ul><p>Even though both can do the job, there are considerable limitations on both sides. Before we start discussing the two approaches, let’s discuss what they are.</p><h4>Lambda@Edge Vs. Cloudfront Functions</h4><p>Both AWS services provide the same concept of delivering processing power with the lowest latency to the user. In essence, they act as HTTP middleware that enables the execution of request &amp; response related processing. However, even though the purpose is the same there are significant differences that can be used in specific cases.</p><p><strong>Cloudfront Functions — </strong>run on the edge locations themselves. There are currently more than 400 edge locations in the world run by AWS. Edge locations are dominantly used for caching the content served by <a href="https://aws.amazon.com/cloudfront/">Cloudfront</a>.<br>Cloudfront functions are also running on the edge locations. With that said, the power and execution time is very limited. However, startup time is measured in submillisecond and scaling is rapidly fast to handle hundreds of million requests per second.<br>However, Cloudfront Functions have a few <strong><em>limitations</em></strong> that might be an issue when architecting a solution. Some of the CF Functions&#39; limitations are: memory is limited to 2 MB, max function code size is limited to 10KB, only JS can be used for coding, no internet access, no access to a request body, no 3rd party libs and etc.<br>Also, there are some <strong><em>advantages</em></strong> over Lambda@Edge functions for instance rapid scaling and lower cost.</p><p><strong>Lambda@Edge — </strong>is similar to Cloudfront Functions, but it runs on a Regional Edge Cache instead of an Edge location. So name as such, might be misleading. Lambda@Edge are following a similar concept as regular AWS Lambda, however, there are some significant differences. Such as It can last 5sec(30sec for origin req/resp), max memory can be 128MB to 3GB, and it can be coded only in JS or Pythons. <br>In comparison to Cloudfront Functions, it has access to the internet, other AWS services, and 3rd party libs that can be used. However, it scales a lot slower and costs much more.</p><p>You can find more in this great <a href="https://medium.com/trackit/cloudfront-functions-vs-lambda-edge-which-one-should-you-choose-c88527647695">article</a> about the pros and cons of both.</p><h4>Pros and Cons for JWT conversion?</h4><p>Minding differences between both technologies, they can be used but in different cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*MC-AfzwiYelHNFOqSZvFlw.png" /></figure><p><strong>Cloudfront functions</strong>, as they can’t access the internet or AWS services, rely only on the libraries that are embedded into the execution environment. With that said, to validate JWT token, <strong>it is required to embed symmetric secret key into the code or provide it as an environment variable</strong>. In order to use the encryption library, you need to import it as shown below.</p><pre>var crypto = require(‘crypto’);<br>....<br>crypto.createHmac(method, key).update(input).digest(&#39;base64url&#39;)</pre><p>However, it is limited only to the algorithms md5, sha1, or sha256. <br>Cloudfront functions <strong>are not an option</strong> if JWT algorithm requires a different way of signing and verification of the above-mentioned algorithms, or if the authentication system does not provide access to the plain text key.</p><p><strong>Lambda@Edge</strong> is on the other hand much more flexible, in terms of options for validating JWT tokens. It has access to the internet, access to other AWS services and can import 3rd party libraries. So integrating services like Cognito would not be a problem. Cognito is an authentication service that does not provide a key in plain text, which means you need to call it over SDK or API to do JWT token validation. <br>However, depending on <strong>scalability requirements</strong>, it may not scale as quickly as expected. Since authentication is one of the most called services in any application and it has to be synchronous, scaling is very important to be well assessed.<br>Lambda@Edge does not have a free tier and it is also 6x more expensive at the lowest settings than Cloudfront functions.</p><h4>Verdict</h4><p>As explained below there is no better or worse solution. It mostly concerns your solution requirement, speed of scaling, and budget. If your solution is fine with the limitations that CloudFront functions have, then CF Functions are a better option. In case not, then you can always use Lambda@Edge with a higher cost and lower scaling speed.<br>Also, if you are not using Cloudfront and your application runs in a single region, you may also use regular lambda functions to perform the above.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ab20ced83fc" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/integrating-multiple-sso-ecosystems-with-jwt-token-by-conversion-in-transit-4ab20ced83fc">Integrating multiple SSO ecosystems with JWT token by conversion in transit</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why AWS Lambda Function URLs will not be part of my projects]]></title>
            <link>https://medium.com/@marinradjenovic/why-aws-lambda-function-urls-will-not-be-part-of-my-projects-6ddf9ef1ac47?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ddf9ef1ac47</guid>
            <category><![CDATA[application-load-balancer]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[api-gateway]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Mon, 11 Apr 2022 15:02:26 GMT</pubDate>
            <atom:updated>2022-04-13T10:50:54.758Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently AWS released a new feature called <a href="https://aws.amazon.com/about-aws/whats-new/2022/04/aws-lambda-function-urls-built-in-https-endpoints/">Lambda Function URLs.</a> The new feature is supposed to simplify the process of exposing Lambda function over HTTP. In essence, you get generated URL to invoke the lambda function over HTTPS. <br>Previously that was only possible by integrating with <a href="https://aws.amazon.com/api-gateway/"><em>API Gateway</em></a> or <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html">Application Load Balancer</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1NrmMi3Smv_raNvx" /><figcaption>Photo by <a href="https://unsplash.com/@tingeyinjurylawfirm?utm_source=medium&amp;utm_medium=referral">Tingey Injury Law Firm</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So why do I think this feature might not be my first choice.</p><h4>Is it simple?</h4><p>Well, it is very simple to generate the lambda function invoked over URL with this new feature. But how complex was it before? If you used AWS SAM or Serverless framework to generate URL for the function, it was rather simple before also.</p><p>With AWS SAM…</p><pre>ApiFunction:     <br>  Type: AWS::Serverless::Function<br>  Properties:  <br>    CodeUri: hello_world/     <br>    Handler: index.handler     <br>    Runtime: nodejs14.x<br>  Events:         <br>    ApiEvent:           <br>    Type: HttpApi</pre><p>With Lambda URL feature</p><pre>HelloWorldFunction:   <br>   Type: AWS::Serverless::Function   <br>   Properties:     <br>     CodeUri: hello_world/     <br>     Handler: index.handler     <br>     Runtime: nodejs14.x     <br>     FunctionUrlConfig:       <br>       AuthType: AWS_IAM</pre><p>All in all, I don’t see it much simpler than it was before. However, it is simpler if you are using Cloudformation or Terraform to code serverless.</p><h4>Is it secure?</h4><p>Using API Gateway you are centralizing incoming requests to your serverless application. This means that all requests will pass over the same gateway which gives you a better perspective on securing your application. By coupling API with functions you are decentralizing the incoming requests which make it harder to control and monitor.<br>Having in mind that Function URLs do support IAM authentication, compared to API Gateway you are having way more options to choose proper authentication. Apart from IAM, you have Cognito and Custom Lambda Authorizers.</p><h4>Is it scalable?</h4><p>Well, it is scalable as much as your lambda is scalable. So there are no buffering capabilities and the exciding the limit will result in HTTPS 429 error. In case your URL is by accident or by intention called in a loop, it would be very hard to limit unwanted requests. There is no support for WAF or limit by plans as on API Gateway.</p><h4>Is it cheaper?</h4><p>Well, Function URLs are included in the price of Lambda requests, so you don’t pay additionally. From that perspective, it’s the cheapest way to invoke the lambda function outside of AWS cloud.</p><h4>Conclusion</h4><blockquote>Function URLs are best for use cases where you must implement a single-function microservice with a public endpoint that doesn’t require the advanced functionality of API Gateway, such as request validation, throttling, custom authorizers, custom domain names, usage plans, or caching.</blockquote><p>The above statement is AWS&#39;s recommendation on when to use Function URLs. Honestly, I rarely have cases where I would not need API Gateway functionalities on production deployments, such as simple request validation throttling, authorizers, and an overall decoupled integration approach with lambdas.<br>Moreover, it’s not even that much simpler if you are using AWS SAM or Serverless. <br>Someone may argue, that it’s free and that’s why you might use it for the beginning and then switch to API Gateway if needed. But there is a switching cost that may apply, in case you used it on a significant number of lambdas. Also in case you want to use it just for a start, API Gateway is cheap also and 1 million requests are covered by the free tier. <br>Finally, I may see the potential benefit of using it for some examples or quick mockups. Especially if you are working with Terraform and you would need much more code in order to create a simple API.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ddf9ef1ac47" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Controllable Scaling in Serverless Big Bang]]></title>
            <link>https://medium.com/swlh/controllable-scaling-in-serverless-big-bang-9a209a56af5b?source=rss-29071b449a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a209a56af5b</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[kinesis]]></category>
            <category><![CDATA[sqs]]></category>
            <category><![CDATA[aws-eventbridge]]></category>
            <dc:creator><![CDATA[Marin Radjenovic]]></dc:creator>
            <pubDate>Wed, 11 Nov 2020 08:17:54 GMT</pubDate>
            <atom:updated>2020-11-11T20:13:44.646Z</atom:updated>
            <content:encoded><![CDATA[<p>With the appearance of microservice architectures, communication using messages become common practice in today’s system design. Tightly coupled monolithic applications are rarely the choice of today’s developers or architects. Such applications mostly exist as legacy applications, waiting to be discontinued at some point in the near future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v_73q11Th7fDn7lE2VhOjA.jpeg" /></figure><p>However, with message-based communication between microservices, we gained some new challenges. For instance, overusing of messages and making the system far more complex than it should be, or making one microservice dependent on the message that might never arrive, or having generic message types that have no exact purpose and might lead to misuse if incorrectly understood by a developer…</p><p>But in this article, I will discuss latency that can build as a result of queueing messages. Also, I will supply some ideas on how to mitigate those problems and avoid overwhelming source systems with minimal effort.</p><h4>Where do we make mistakes?</h4><p>I am sure that all of you had a situation where you need to process some messages or events faster than the others. Some of the messages require different priorities when processed, as well as in real-life cases.<br> <br>For instance, if we take a hospital reception desk as a message router. People with urgent medical needs, such as a heart attack, need urgent attention in comparison with people that have common flu symptoms. So by evaluating the person’s condition, the receptionist is routing those patients to the specific hospital section in a prioritized way. <br>In case, you have all patients in the same queue, some of them might die waiting :(</p><p>Similarly to patients in a hospital, some messages and events need quicker attention. Let’s walk through a real-life scenario with a perfect architecture but also with one shortcoming :)</p><h4><strong>Sluggish e-mail campaign solution</strong></h4><p>We had a perfect idea to create a tool for email campaigns out of a few already existing external applications. So, we have used CMS to create HTML content for the email. <br> We have integrated CRM to personalize email content by collecting user Firstname, Lastname etc. Also, to create target users, we had to integrate Segmentation DB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iXmo9YL1Ny_mM7wmFnv0kw.png" /></figure><p>The solution worked similarly to the assembly line in a factory. Lambdas were responsible for processing work while data was carried over Kinesis stream from lambda to lambda.<br><a href="https://anchor.fm/awsovanje/episodes/ta-ima-novo-na-AWSu---19--Okt--2020---EP07-el88r4">Generate Template Lambda</a> was responsible for assembling the HTML template created in CMS using WYSIWYG editor.<br><a href="https://anchor.fm/awsovanje/episodes/ta-ima-novo-na-AWSu---19--Okt--2020---EP07-el88r4">Prepare Targets Lambda</a> was responsible to retrieve users from the segment that are belonging to certain marketing campaign targets. <br><a href="https://anchor.fm/awsovanje/episodes/ta-ima-novo-na-AWSu---19--Okt--2020---EP07-el88r4">Populating Lambda</a> was responsible for pulling user details from CRM and assembling the email data JSON.<br><a href="https://anchor.fm/awsovanje/episodes/ta-ima-novo-na-AWSu---19--Okt--2020---EP07-el88r4">Email Sending Lambda</a> was responsible for replacing placeholders in the email HTML templates, populating them with data received, and sending them to the users.<br>All in all one great solution on how to reuse already existing infrastructure and produce new value using a serverless paradigm.</p><p>However, some things didn’t go as we had hoped…</p><h4><strong>Facing a sad reality…</strong></h4><p>Everything worked perfectly for quite some time. We were able to send more than 5 million emails per day. <br>However, one day while having a few long-running global campaigns in parallel, marketers wanted to have an urgent one-day Valentine&#39;s day offer. <br>So they published it…<br>Unfortunately, stats were showing no deliveries for more than a few hours. There was no email coming out for a whole day? Marketing managers were in a panic, more than half-day has already passed and no email has gone yet.<br>When we checked the logs everything was functioning perfectly, there were emails coming out but not for Valentine’s day campaign.<br>We have checked the campaign everything seemed fine, except <strong>iterator age </strong>on kinesis that was triggering Email Sending Lambda. Iterator was 156960405 ms old or almost <strong>2 days old</strong>. So if you publish a campaign right now, you would get the first emails coming out in 2 days :)<br>There was nothing smart we could do at that time. You may say “you could just re-shard it and scale it up” but kinesis re-sharding is something that we avoided for a good reason…<br>So we had to let the campaign fail and break so many hearts for Valentine’s day :(<br>Customers received emails two days after Valentin’s day had passed.</p><h4>Why didn’t we just scale everything up?</h4><p>There is one easy solution to the above situation “just scale-up and the job is done…” Even though that might be very often the quickest and easiest solution to overcome big problems, it might, also, be a ticket to disaster.</p><p>From the above example, the solution is calling two <strong>external</strong> systems for the information (SegmentationDB and CRM). In case we would scale up our solution, the load will be moved from our solution to external systems. That would mean that we did not solve the problem but just moved to other systems that we depend on.</p><p>As <strong>external</strong> systems might be a legacy or on-prem, they also might have a limited capacity to scale. Due to increased load, there is a big chance that your solution may potentially cause congestion at the external systems’ side. With that said, users or other systems that integrate with those external systems may experience latency or even unavailability. As your solution depends on those systems it will also experience latency or even failures. What you may experience is a cascade failure on a higher scale.</p><p>In the end, the idea of scaling up could be just a shortcut to disaster.</p><h4>If scaling up was a bad idea, what did we do then?</h4><p>The plan was not to increase the capacity of processing but still be able to process urgent messages quicker. That leads us to message prioritization in the queue. <br>We have determined with marketers that there should be 3 types of priority when publishing campaigns (HI, MED, LOW).</p><p>Unfortunately, nor Kinesis nor SQS supports the prioritization of messages AWS suggests provisioning queues according to priority. So there should be as many queues as priorities. In that way, we could process messages at a different pace.</p><p>So after a few days of thinking, the solution popped up which you can observe below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DLL3nNwh-qBR1-7R4ry0rg.png" /></figure><p>Certain facts had to be considered while making architectural decisions. I will try to explain them below.</p><h4>How are priority queues eliminating the problem?</h4><p>As said before, there is no such option to sort messages based on priority in Kinesis nor SQS. Therefore, message queues need to be split into 3 queues or as many as you have priority types.<br>But how do multiple queues eliminate the problem? Well, if we consider that messages are terminated at the lambda side and that as quickly as they reach lambda they will be executed, we can conclude that by routing messages to the queue with fewer messages the execution will be faster.</p><h4><strong>Why is there a need for both Kinesis and SQS?</strong></h4><p>Well, both services are queueing data but they are doing it differently. Kinesis is buffering data and it can buffer as much as you can put in 1000 puts/second but not more than 1MB per put request. So virtually in a day, you can put 86400000 MBs or 86.4 TBs on one shard.<br>So yes, lots of data… but be careful your data retention is only 24h by default or max 7 days if configured.<br>Similar to Kinesis, SQS can accept a virtually unlimited number of messages per second (if FIFO then 3000 rec/sec), but at a max size of 256KB (or up to 1 GB in combination with S3).<br> Again lots of data… and again you need to be careful regarding data retention which spans from 2 days to 14 days (if configured).</p><h4><strong>From the above said, SQS and Kinesis seem very similar. What is the difference?</strong></h4><p>Well, the catch is in how they operate with lambda. It is very important to know in advance the throughput capabilities between lambdas and source system from where you consume necessary data. It frequently happens that you realize that when the system is already in prod. <br>However, let’s assume that an external system that runs on-prem has limited scalability. Considering that fact, it might happen that virtually unlimited scalability of serverless architecture may overwhelm the external system.<br>So which one should be used and when? Well, SQS is scaling based on queue depth or the number of messages in queue currently. Optimally, Lambda functions with an Amazon SQS trigger can scale up 60 additional instances per minute to a maximum of 1,000 concurrent invocations (depending on the soft limit). So little under 17 minutes, you will have 1000 concurrent lambdas hammering your external system. At one moment lambdas can knock the external system down. <br>Now, about Kinesis… Kinesis is scaling by splitting the shards. Each shard can trigger one lambda and process a configurable batch. This means that you can tune how much data will be acceptable for an external system to process.</p><h4><strong>Why SNS in front of Kinesis and SQS?</strong></h4><p>SNS plays a key role in serving to fanout messages to priority queues. Routing messages is done via <strong>SNS message filtering</strong>. So by attribute defined in the payload of the message, SNS is able to notify specific subscribers. This is a great way how to utilize Serverless AWS services like SNS for such business logic in a scalable way.</p><p>In the example below there is an example message send to SNS. It has payload under attribute “message” and it contained a filtering attribute under “MessageAttribute”.</p><pre>{<br>    &quot;Type&quot;: &quot;Notification&quot;,<br>    &quot;MessageId&quot;: &quot;a1b2c34d-567e-8f90-g1h2-i345j67klmn8&quot;,<br>    &quot;TopicArn&quot;: &quot;arn:aws:sns:us-east-2:123456789012:HighPrio&quot;,<br>    &quot;Message&quot;: &quot;{ \&quot;key1\&quot;:\&quot;value2\&quot;, \&quot;key2\&quot;:\&quot;value2\&quot;}&quot;,<br>    &quot;Timestamp&quot;: &quot;2019-11-03T23:28:01.631Z&quot;,<br>    &quot;SignatureVersion&quot;: &quot;4&quot;,<br>    &quot;Signature&quot;: &quot;<em>signature</em>&quot;,<br>    &quot;UnsubscribeURL&quot;: &quot;<em>unsubscribe-url</em>&quot;,<br>    &quot;MessageAttributes&quot;: {<br>       &quot;priority&quot;: {<br>          &quot;Type&quot;: &quot;String&quot;,<br>          &quot;Value&quot;:&quot;MED&quot;<br>       }<br>    }<br> }</pre><p>As I like Cloudformation a lot, I will describe what it is necessary to be defined in the template. So, you need to define the topic where messages will be published called “MsgPrioTopic”. Then, define queues for “MED” and “LOW” priorities. Finally, subscribe queues to the topic based on the filter policy (MED &amp; LOW).</p><pre>MsgPrioTopic:<br>    Type: &quot;AWS::SNS::Topic&quot;<br>    Properties:<br>    TopicName:&quot;MsgPrio-topic&quot;</pre><pre>MsgMedQueue:<br>    Type: AWS::SQS::Queue<br>    Properties:     <br>      QueueName: !Sub &quot;${AWS::StackName}-Msg-med-queue&quot;</pre><pre>MsgLowQueue:<br>    Type: AWS::SQS::Queue<br>    Properties:     <br>      QueueName: !Sub &quot;${AWS::StackName}-Msg-low-queue&quot;</pre><pre>MsgMedTopicSnsSubscription:<br>    Type: AWS::SNS::Subscription<br>    Properties:<br>      Endpoint: !GetAtt MsgMedQueue.Arn<br>      Protocol: sqs<br>      RawMessageDelivery: true<br>      TopicArn: !Ref MsgPrioTopic<br>      FilterPolicy:<br>        priority: <br>          - MED<br>  <br>  MsgLowTopicSnsSubscription:<br>    Type: AWS::SNS::Subscription<br>    Properties:<br>      Endpoint: !GetAtt MsgLowQueue.Arn<br>      Protocol: sqs<br>      RawMessageDelivery: true<br>      TopicArn: !Ref MsgPrioTopic<br>      FilterPolicy:<br>        priority: <br>          - LOW</pre><h4><strong>EventBridge has far more integrations with AWS services, so why SNS?</strong></h4><p>Well, EventBridge is a great service and in most cases, I would prefer using it over SNS. However, EventBridge is not always the silver bullet in comparison to SNS. What do AWS docs say about SNS comparing to EventBridge?</p><blockquote>“Amazon SNS is recommended when you want to build an application that reacts to high throughput or low latency messages published by other applications”</blockquote><p>So, EventBridge&#39;s typical latency is of around half a second while SNS has a latency of about 30msec. In case you want to decrease latency, this is the fact you should consider.</p><h4><strong>Verdict</strong></h4><p>Quite some time ago, I was stunned by a Pirelli advertisement with Carl Lewis. At first, I was like what a heck… and then it just made a lot more sense. That is basically what I had in mind when I was thinking about this solution.<br>Serverless is a great way to do a lot with minimal effort. You have virtually unlimited scalability power to process data. But you have to think also about the consequences that great power may have on your dependencies. Systems that cannot scale to those levels and might be quickly overwhelmed and potentially choked. So unlimited power can easily turn into an unlimited disaster.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a209a56af5b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/controllable-scaling-in-serverless-big-bang-9a209a56af5b">Controllable Scaling in Serverless Big Bang</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>