<?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"><channel><title><![CDATA[CloudGags]]></title><description><![CDATA[CloudGags.in is your go-to destination for all things cloud computing. Specializing in technical content related to cloud platforms such as AWS, GCP, and Azure.]]></description><link>https://blog.cloudgags.in</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 06:52:29 GMT</lastBuildDate><atom:link href="https://blog.cloudgags.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Amazon GuardDuty EC2 Runtime Monitoring is Now Available]]></title><description><![CDATA[Amazon Web Services (AWS) has recently announced the general availability of Amazon GuardDuty EC2 Runtime Monitoring. This new feature enhances the security of EC2 instances by monitoring their runtime activities.
GuardDuty is AWS’s intelligent threa...]]></description><link>https://blog.cloudgags.in/amazon-guardduty-ec2-runtime-monitoring-is-now-available</link><guid isPermaLink="true">https://blog.cloudgags.in/amazon-guardduty-ec2-runtime-monitoring-is-now-available</guid><category><![CDATA[AWS]]></category><category><![CDATA[#Amazon GuardDuty]]></category><category><![CDATA[ec2]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Fri, 19 Jul 2024 11:10:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1721386637246/d742c85a-514c-48e7-b7b2-63d1cb11cb55.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Amazon Web Services (AWS) has recently announced the general availability of Amazon GuardDuty EC2 Runtime Monitoring. This new feature enhances the security of EC2 instances by monitoring their runtime activities.</p>
<p>GuardDuty is AWS’s intelligent threat detection service, and with the addition of EC2 Runtime Monitoring, it offers even more comprehensive protection for your cloud resources.</p>
<h2 id="heading-what-is-amazon-guardduty">What is Amazon GuardDuty?</h2>
<p>Amazon GuardDuty is a security service that uses machine learning to detect potential threats and malicious activities within your AWS environment.</p>
<p>It continuously monitors your AWS accounts and workloads, providing detailed security findings that help you identify and respond to threats quickly.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721387525639/c3959d85-40dd-4876-927c-fdc3d0cd3a9b.png" alt="Amazon GuardDuty EC2 Runtime Monitoring is Now Available" class="image--center mx-auto" /></p>
<h2 id="heading-key-features-of-guardduty">Key Features of GuardDuty</h2>
<ol>
<li><p><strong>Intelligent Threat Detection</strong>: Uses machine learning to detect threats.<br /> Continuous Monitoring: Keeps an eye on your AWS accounts and workloads 24/7.</p>
</li>
<li><p><strong>Detailed Security Findings</strong>: Provides in-depth information about detected threats.</p>
</li>
<li><p><strong>Integration with Other AWS Services</strong>: Works with AWS Security Hub, Amazon Detective, and more</p>
</li>
</ol>
<h2 id="heading-what-is-ec2-runtime-monitoring">What is EC2 Runtime Monitoring?</h2>
<p>EC2 Runtime Monitoring is a new feature within GuardDuty that focuses on monitoring the operating system-level activities of EC2 instances.</p>
<p>This includes observing network and file events to detect potential runtime threats, such as malware execution and unauthorized network connections.</p>
<h2 id="heading-benefits-of-ec2-runtime-monitoring">Benefits of EC2 Runtime Monitoring</h2>
<ol>
<li><p><strong>Enhanced Threat Detection</strong>: Identifies threats at the operating system level.<br /> Visibility into OS-Level Activities: Offers insights into the activities occurring within your EC2 instances.</p>
</li>
<li><p><strong>Integration with AWS Ecosystem</strong>: Seamlessly integrates with other AWS security services for a unified security strategy.</p>
</li>
</ol>
<h2 id="heading-how-does-it-work">How Does It Work?</h2>
<p>GuardDuty EC2 Runtime Monitoring works by deploying a security agent on your EC2 instances. This agent collects data on runtime activities and sends it to GuardDuty for analysis.</p>
<p>If any suspicious activity is detected, GuardDuty generates a security finding, providing you with the details you need to investigate and respond to the threat.</p>
<h2 id="heading-setting-up-ec2-runtime-monitoring">Setting Up EC2 Runtime Monitoring</h2>
<ol>
<li><p><strong>Enable GuardDuty</strong>: Make sure GuardDuty is enabled in your AWS account.</p>
</li>
<li><p><strong>Deploy the Security Agent</strong>: You can deploy the agent automatically or manually.</p>
</li>
<li><p><strong>Monitor Security Findings</strong>: Use the GuardDuty console to view and manage security findings related to your EC2 instances.</p>
</li>
</ol>
<h2 id="heading-why-is-ec2-runtime-monitoring-important">Why is EC2 Runtime Monitoring Important?</h2>
<p>In today’s cloud-centric world, protecting your virtual machines from threats is crucial.</p>
<p>EC2 Runtime Monitoring helps ensure that your EC2 instances are secure by providing continuous, detailed monitoring of their activities.</p>
<p>This level of visibility is essential for detecting and mitigating threats before they can cause significant damage.</p>
<h2 id="heading-common-threats-detected">Common Threats Detected</h2>
<ol>
<li><p><strong>Malware Execution</strong>: Identifies and alerts you to the presence of malware.</p>
</li>
<li><p><strong>Unauthorized Network Connections</strong>: Detects suspicious network activities.</p>
</li>
<li><p><strong>Cryptocurrency Mining</strong>: Recognizes instances that are involved in unauthorized cryptocurrency mining activities.</p>
</li>
</ol>
<h2 id="heading-real-world-applications">Real-World Applications</h2>
<p>EC2 Runtime Monitoring is beneficial for a wide range of industries and use cases. Whether you are running a small business or managing a large enterprise, this feature can help you maintain the security of your cloud infrastructure.</p>
<h3 id="heading-use-cases">Use Cases</h3>
<ol>
<li><p><strong>E-Commerce</strong>: Protect customer data and ensure the security of online transactions.</p>
</li>
<li><p><strong>Healthcare</strong>: Safeguard sensitive patient information and comply with regulatory requirements.</p>
</li>
<li><p><strong>Financial Services</strong>: Prevent unauthorized access to financial data and transactions.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Amazon GuardDuty EC2 Runtime Monitoring is a powerful tool for enhancing the security of your EC2 instances.</p>
<p>By providing detailed insights into OS-level activities and integrating seamlessly with other AWS security services, it helps you detect and respond to threats more effectively.</p>
<p>Whether you’re new to AWS or a seasoned user, enabling EC2 Runtime Monitoring can significantly improve your cloud security posture.</p>
<p>For more information and to get started, visit the <a target="_blank" href="https://aws.amazon.com/blogs/aws/amazon-guardduty-ec2-runtime-monitoring-is-now-generally-available/">AWS Blog</a>.</p>
]]></content:encoded></item><item><title><![CDATA[How to right use maxage, s-maxage, and stale-if-error headers?]]></title><description><![CDATA[In today's digital market, when milliseconds can mean the difference between user experience and retention, optimizing content delivery is critical for every website or application.
Fast and dependable content delivery around the world is made possib...]]></description><link>https://blog.cloudgags.in/how-to-right-use-maxage-s-maxage-and-stale-if-error-headers</link><guid isPermaLink="true">https://blog.cloudgags.in/how-to-right-use-maxage-s-maxage-and-stale-if-error-headers</guid><category><![CDATA[max-age]]></category><category><![CDATA[s-maxage]]></category><category><![CDATA[AWS]]></category><category><![CDATA[origin]]></category><category><![CDATA[cache]]></category><category><![CDATA[cache-control]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 13 Jul 2024 10:38:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720880440065/7d46ef93-512b-4eb3-abdf-65f9fc51cf38.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In today's digital market, when milliseconds can mean the difference between user experience and retention, optimizing content delivery is critical for every website or application.</p>
<p>Fast and dependable content delivery around the world is made possible in large part by content delivery networks, or CDNs.</p>
<p>However, web developers and administrators must comprehend and make good use of cache headers like max-age, s-maxage, and stale-if-error to fully exploit CDNs.'</p>
<h2 id="heading-understanding-cdns-and-http-headers">Understanding CDNs and HTTP Headers</h2>
<p>HTTP headers are essential parts of online communication; they carry metadata and instructions that servers and browsers need to exchange information efficiently.</p>
<p>These headers allow effective caching strategies that lower latency, lower server load, and enhance overall page performance when paired with CDNs.</p>
<p>A network of dispersed servers known as a content delivery network (CDN) provides users with web content according to their location.</p>
<p>CDNs guarantee that consumers may access data from the closest server, hence drastically lowering latency and speeding up load times.</p>
<p>They do this by caching content at many edge locations. Here's where caching headers come into play, telling these edge servers what to do in certain situations, such as when the origin server fails, and how long to cache content.</p>
<h2 id="heading-exploring-key-headers">Exploring Key Headers</h2>
<ol>
<li><h3 id="heading-max-age">Max-Age</h3>
</li>
</ol>
<p>The <code>max-age</code> directive is used to specify the maximum amount of time in seconds that a cached representation of a resource can be considered fresh.</p>
<p>When a client (browser or CDN edge server) fetches a resource, it stores it locally and reuses it until the <code>max-age</code> duration expires. For example, setting <code>Cache-Control: max-age=3600</code> instructs clients to cache the resource for one hour before requesting a fresh copy from the server.</p>
<p><code>Cache-Control: max-age=3600</code></p>
<p>This directive tells the client to cache the resource for 3600 seconds (1 hour). During this period, the client will use the cached version, improving performance by avoiding repeated requests to the server.</p>
<ol start="2">
<li><h3 id="heading-s-maxage">S-MaxAge</h3>
</li>
</ol>
<p>While <code>max-age</code> is typically set by the origin server for individual clients, <code>s-maxage</code> (shared max-age) is specifically intended for CDNs or other intermediary caches.</p>
<p>It overrides <code>max-age</code> when present, allowing CDN edge servers to serve stale content until <code>s-maxage</code> seconds have elapsed. This header is particularly useful for content that is static or changes infrequently across all users.</p>
<p><strong>Note:</strong><code>max-age</code> works on both browser and edge locations, but <code>s-maxage</code> only works on edge locations.</p>
<p><code>Cache-Control: max-age=3600, s-maxage=7200</code></p>
<p>In this example, the resource will be cached by browsers for 3600 seconds (1 hour) but will be cached by shared caches like CDNs for 7200 seconds (2 hours).</p>
<ol start="3">
<li><h3 id="heading-stale-if-error">Stale-If-Error</h3>
</li>
</ol>
<p>The <code>stale-if-error</code> directive provides a fallback mechanism in case the origin server is temporarily unavailable (e.g., due to maintenance or origin failure).</p>
<p>When specified alongside <code>s-maxage</code>, it allows CDN edge servers to serve a stale (expired) response to users if the origin fails to respond within a certain time frame. This improves user experience by minimizing downtime and reducing the impact of origin failures on end-users.</p>
<p><code>Cache-Control: max-age=3600, s-maxage=7200, stale-if-error=86400</code></p>
<p>Here, the resource will be cached by browsers for 1 hour, by CDNs for 2 hours, and if the origin server fails, the CDN can serve the stale response for up to 86400 seconds (24 hours).</p>
<h2 id="heading-adding-headers-on-cloudfront-using-lambdaedge">Adding Headers on CloudFront Using Lambda@edge</h2>
<p>Amazon CloudFront, a popular CDN service, allows the use of Lambda@Edge to run code closer to users and customize content delivery. Here’s how you can add <code>max-age</code>, <code>s-maxage</code>, and <code>stale-if-error</code> headers using a Lambda@Edge function on origin response.</p>
<ol>
<li><h3 id="heading-create-a-lambda-function">Create a Lambda Function</h3>
</li>
</ol>
<p>First, create a Lambda function in the AWS Lambda console. Ensure the function is set to the Python runtime.</p>
<ol start="2">
<li><h3 id="heading-write-the-lambdaedge-code">Write the Lambda@Edge Code</h3>
</li>
</ol>
<p>Here’s an example of a Lambda@Edge function that adds <code>max-age</code>, <code>s-maxage</code>, and <code>stale-if-error</code> headers to the origin response</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_handler</span>(<span class="hljs-params">event, context</span>):</span>
    response = event[<span class="hljs-string">'Records'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'cf'</span>][<span class="hljs-string">'response'</span>]

    <span class="hljs-comment"># Add Cache-Control headers</span>
    response[<span class="hljs-string">'headers'</span>][<span class="hljs-string">'cache-control'</span>] = [{
        <span class="hljs-string">'key'</span>: <span class="hljs-string">'Cache-Control'</span>,
        <span class="hljs-string">'value'</span>: <span class="hljs-string">'max-age=3600, s-maxage=7200, stale-if-error=86400'</span>
    }]

    <span class="hljs-keyword">return</span> response
</code></pre>
<ol start="3">
<li><h3 id="heading-deploy-the-lambda-function">Deploy the Lambda Function</h3>
</li>
</ol>
<p>After writing the function, deploy it and publish a new version.</p>
<ol start="4">
<li><h3 id="heading-add-a-cloudfront-trigger">Add a CloudFront Trigger</h3>
</li>
</ol>
<p>In the CloudFront console, go to your distribution and create a new Lambda@Edge trigger for the origin response event. Select the Lambda function you deployed and associate it with the CloudFront distribution.</p>
<ol start="5">
<li><h3 id="heading-test-the-setup">Test the Setup</h3>
</li>
</ol>
<p>After deployment, test the setup by accessing your CloudFront distribution and verifying the response headers. The headers should now include <code>max-age</code>, <code>s-maxage</code>, and <code>stale-if-error</code> as specified.</p>
<h2 id="heading-advantages-of-using-caching-headers">Advantages of Using Caching Headers</h2>
<ol>
<li><h3 id="heading-improved-performance-and-user-experience"><strong>Improved Performance and User Experience</strong></h3>
</li>
</ol>
<p>By setting appropriate <code>max-age</code> and <code>s-maxage</code> headers, web developers can significantly reduce page load times and improve responsiveness. Users experience faster access to content since resources are fetched locally or from nearby CDN edge servers rather than the distant origin server.</p>
<ol start="2">
<li><h3 id="heading-reduced-server-load">Reduced Server Load</h3>
</li>
</ol>
<p>Effective caching headers reduce the number of requests hitting the origin server, thereby lowering server load and operational costs. This is particularly beneficial during traffic spikes or when serving content to a global audience.</p>
<ol start="3">
<li><h3 id="heading-resilience-to-origin-failures">Resilience to Origin Failures</h3>
</li>
</ol>
<p>The combination of <code>s-maxage</code> and <code>stale-if-error</code> headers ensures that users continue to receive content even if the origin server experiences temporary issues. This enhances the reliability of the website or application and minimizes the impact of origin failures on user experience.</p>
<h2 id="heading-practical-use-cases">Practical Use Cases</h2>
<p>To better understand the practical application of these headers, let's explore a few scenarios:</p>
<ol>
<li><h3 id="heading-e-commerce-websites">E-commerce Websites</h3>
</li>
</ol>
<p>E-commerce sites frequently update their product listings, but some pages, such as static content or rarely changing product images, can benefit from extended caching. Setting <code>max-age</code> and <code>s-maxage</code> for these resources ensures quick load times and reduces server load during high-traffic events like Black Friday sales. Additionally, using <code>stale-if-error</code>ensures that customers can still view product images and descriptions even if the origin server experiences issues.</p>
<ol start="2">
<li><h3 id="heading-news-portals">News Portals</h3>
</li>
</ol>
<p>News websites often face sudden spikes in traffic, especially during breaking news events. By setting appropriate <code>max-age</code>and <code>s-maxage</code> headers, news articles can be cached at CDN edges, providing quick access to users globally. The <code>stale-if-error</code> header ensures that readers can still access news articles if the origin server becomes temporarily unavailable.</p>
<ol start="3">
<li><h3 id="heading-content-delivery-for-saas-applications">Content Delivery for SaaS Applications</h3>
</li>
</ol>
<p>SaaS applications can have a mix of dynamic and static content. Static assets like JavaScript files, CSS, and images can have longer <code>max-age</code> and <code>s-maxage</code> values to improve load times. Meanwhile, using <code>stale-if-error</code> can ensure continuous access to static resources if the origin server faces downtime, improving the overall reliability of the service.</p>
<h2 id="heading-best-practices-for-using-caching-headers">Best Practices for Using Caching Headers</h2>
<ol>
<li><h3 id="heading-understand-your-content">Understand Your Content</h3>
</li>
</ol>
<p>Analyze the type of content you serve and how frequently it changes. Set shorter caching durations for frequently updated content and longer durations for static content.</p>
<ol start="2">
<li><h3 id="heading-test-and-monitor">Test and Monitor</h3>
</li>
</ol>
<p>Implement caching headers incrementally and monitor their impact on performance and server load. Use monitoring tools to track cache hit ratios and identify potential issues.</p>
<ol start="3">
<li><h3 id="heading-combine-with-other-performance-optimization-techniques">Combine with Other Performance Optimization Techniques</h3>
</li>
</ol>
<p>Caching headers are just one part of the performance optimization puzzle. Combine them with techniques like minification, compression, and image optimization to achieve the best results.</p>
<ol start="4">
<li><h3 id="heading-communicate-with-your-cdn-provider">Communicate with Your CDN Provider</h3>
</li>
</ol>
<p>Work closely with your CDN provider to understand their caching policies and capabilities. Ensure your caching strategy aligns with their recommendations for optimal performance.</p>
<h2 id="heading-summary-and-key-takeaway">Summary and Key Takeaway</h2>
<p>In conclusion, understanding and implementing caching headers such as <code>max-age</code>, <code>s-maxage</code>, and <code>stale-if-error</code> can significantly enhance the performance, reliability, and scalability of your web infrastructure. By leveraging these headers effectively, you can optimize content delivery through CDNs, reduce server load, and ensure a seamless user experience even during server downtime or origin failures.</p>
<p>By prioritizing the correct configuration of caching headers, web developers empower their CDN setups to operate efficiently, delivering content swiftly and reliably to users worldwide. Whether optimizing for speed, resilience, or cost-efficiency, these headers form a cornerstone of modern web architecture.</p>
<h2 id="heading-key-takeaways">Key Takeaways:</h2>
<ul>
<li><p><strong>Maximize Performance:</strong> Use <code>max-age</code> and <code>s-maxage</code> to improve load times and reduce server load.</p>
</li>
<li><p><strong>Enhance Reliability:</strong> Employ <code>stale-if-error</code> to ensure content availability during origin failures.</p>
</li>
<li><p><strong>Leverage CloudFront:</strong> Utilize Lambda@Edge to dynamically set caching headers and customize content delivery.</p>
</li>
<li><p><strong>Adopt Best Practices:</strong> Understand your content, test and monitor your strategy, and combine caching with other optimization techniques for best results.</p>
</li>
</ul>
<p>By implementing these strategies, you can significantly improve the efficiency and reliability of your content delivery, providing a better experience for your users and reducing the strain on your servers.</p>
]]></content:encoded></item><item><title><![CDATA[Introducing AWS Chalice: Stop Serverless Deployment Frustration Now!]]></title><description><![CDATA[In the ever-evolving world of cloud computing, serverless architectures have emerged as a game-changer due to their scalability and cost-effectiveness.
However, deploying serverless applications can be complex and frustrating, especially for develope...]]></description><link>https://blog.cloudgags.in/introducing-aws-chalice-stop-serverless-deployment-frustration-now</link><guid isPermaLink="true">https://blog.cloudgags.in/introducing-aws-chalice-stop-serverless-deployment-frustration-now</guid><category><![CDATA[AWS]]></category><category><![CDATA[lambda]]></category><category><![CDATA[serverless]]></category><category><![CDATA[serverless framework]]></category><category><![CDATA[chalice]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Wed, 03 Jul 2024 14:14:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/Skf7HxARcoc/upload/9a1638bee0ea5e1b5fe2bc9aec3e8ece.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the ever-evolving world of cloud computing, serverless architectures have emerged as a game-changer due to their scalability and cost-effectiveness.</p>
<p>However, deploying serverless applications can be complex and frustrating, especially for developers new to the ecosystem. Enter AWS Chalice, a powerful framework designed to simplify and streamline the deployment of Python serverless applications on AWS.</p>
<p>In this blog, we'll explore what Chalice is, how it differs from the AWS Serverless Application Model (SAM), and how it can help you deploy a Python serverless application in minutes. We'll also cover the AWS resources created during the deployment process.</p>
<h2 id="heading-what-is-chalice">What is Chalice?</h2>
<p>Chalice is a Python framework developed by AWS that enables developers to quickly create and deploy serverless applications.</p>
<p>It allows you to define and manage AWS Lambda functions and API Gateway endpoints with minimal configuration and code.</p>
<p>Chalice abstracts much of the complexity involved in serverless deployments, making it an excellent choice for developers who want to focus on writing code rather than managing infrastructure. For more detailed information, you can refer to the <a target="_blank" href="https://aws.github.io/chalice/main.html">official Chalice documentation</a>.</p>
<h2 id="heading-how-is-chalice-different-from-aws-sam">How is Chalice Different from AWS SAM?</h2>
<p>Both Chalice and the AWS Serverless Application Model (SAM) are designed to facilitate the development and deployment of serverless applications on AWS, but they have some key differences:</p>
<h3 id="heading-1-language-focus">1. Language Focus</h3>
<ul>
<li><p><strong>Chalice:</strong> Specifically designed for Python, providing a Pythonic interface for creating serverless applications.</p>
</li>
<li><p><strong>AWS SAM:</strong> Supports multiple languages (Python, Node.js, Java, etc.) and uses a YAML-based template to define the serverless application.</p>
</li>
</ul>
<h3 id="heading-2-abstraction-level">2. Abstraction Level</h3>
<ul>
<li><p><strong>Chalice:</strong> Higher level of abstraction, allowing developers to define their application using simple Python decorators and classes.</p>
</li>
<li><p><strong>AWS SAM:</strong> Lower level of abstraction, requiring developers to write detailed CloudFormation templates.</p>
</li>
</ul>
<h3 id="heading-3-complexity">3. Complexity</h3>
<ul>
<li><p><strong>Chalice:</strong> Simplifies the development process by handling much of the boilerplate code and configuration.</p>
</li>
<li><p><strong>AWS SAM:</strong> Offers more control and customization options but requires more detailed configuration and understanding of AWS services.</p>
</li>
</ul>
<h2 id="heading-why-and-when-to-use-a-serverless-framework">Why and When to Use a Serverless Framework</h2>
<p>Serverless frameworks are beneficial for various reasons and use cases. Here’s why you should consider using one:</p>
<h3 id="heading-1-cost-efficiency">1. Cost Efficiency</h3>
<p>Serverless architectures operate on a pay-as-you-go model. You are billed only for the compute time you consume, which can lead to significant cost savings compared to traditional server-based architectures.</p>
<h3 id="heading-2-scalability">2. Scalability</h3>
<p>Serverless applications can automatically scale up or down based on demand. This means you don’t have to worry about provisioning and managing server capacity, as AWS handles it for you.</p>
<h3 id="heading-3-reduced-operational-complexity">3. Reduced Operational Complexity</h3>
<p>With serverless, much of the infrastructure management, such as server maintenance, patching, and scaling, is handled by the cloud provider. This allows you to focus more on developing your application’s features.</p>
<h3 id="heading-4-faster-time-to-market">4. Faster Time to Market</h3>
<p>Serverless frameworks like Chalice allow for rapid development and deployment. You can build and deploy applications quickly, which is ideal for startups or projects that need to be launched rapidly.</p>
<h2 id="heading-use-cases">Use Cases</h2>
<ul>
<li><p><strong>Microservices:</strong> Breaking down a monolithic application into smaller, manageable microservices.</p>
</li>
<li><p><strong>Data Processing:</strong> Executing tasks in response to data streams or changes, such as processing data from IoT devices.</p>
</li>
<li><p><strong>APIs and Web Applications:</strong> Building scalable APIs and web applications that can handle variable loads.</p>
</li>
<li><p><strong>Scheduled Tasks:</strong> Running periodic or scheduled tasks without maintaining a dedicated server.</p>
</li>
</ul>
<h2 id="heading-benefits-of-using-chalice">Benefits of Using Chalice</h2>
<p>Chalice provides several benefits that make it an attractive choice for deploying Python serverless applications:</p>
<h3 id="heading-1-rapid-development">1. Rapid Development</h3>
<p>Chalice's intuitive API allows you to quickly define routes, functions, and event handlers using Python decorators. This makes it easy to get your application up and running without dealing with complex configurations.</p>
<h3 id="heading-2-seamless-deployment">2. Seamless Deployment</h3>
<p>With a single command (<code>chalice deploy</code>), Chalice packages your application, uploads it to AWS, and sets up all the necessary resources. This seamless deployment process reduces the time and effort required to get your application online.</p>
<h3 id="heading-3-built-in-local-development">3. Built-in Local Development</h3>
<p>Chalice includes a local development server that allows you to test your application locally before deploying it to AWS. This ensures that you can catch and fix issues early in the development process.</p>
<h3 id="heading-4-integrated-with-aws-services">4. Integrated with AWS Services</h3>
<p>Chalice integrates seamlessly with various AWS services, such as API Gateway, Lambda, S3, <a target="_blank" href="https://cloudgags.in/blog/aws/dynamodb/">DynamoDB</a>, and more. This allows you to build powerful serverless applications that leverage the full range of AWS capabilities.</p>
<h2 id="heading-aws-resources-created-by-chalice">AWS Resources Created by Chalice</h2>
<p>When you deploy a Chalice application, several AWS resources are automatically created and configured. Here are the key resources:</p>
<h3 id="heading-1-aws-lambda-functions">1. AWS Lambda Functions</h3>
<p>Chalice packages your Python code into AWS Lambda functions. Each route you define in your Chalice application is associated with a Lambda function, which handles the corresponding HTTP request.</p>
<h3 id="heading-2-api-gateway">2. API Gateway</h3>
<p>API Gateway is used to create RESTful APIs that act as the entry point for your Lambda functions. Chalice automatically configures API Gateway to route incoming HTTP requests to the appropriate Lambda function.</p>
<h3 id="heading-3-iam-roles-and-policies">3. IAM Roles and Policies</h3>
<p>Chalice creates IAM roles with the necessary permissions for your Lambda functions to execute. These roles include policies that allow your functions to interact with other AWS services, such as DynamoDB or S3.</p>
<h3 id="heading-4-cloudformation-stack">4. CloudFormation Stack</h3>
<p>Chalice generates a <a target="_blank" href="https://cloudgags.in/blog/aws/cloudformation/">CloudFormation</a> stack that defines the resources and their configurations. This stack is used to deploy and manage your serverless application as a single unit.</p>
<h3 id="heading-5-amazon-cloudwatch-logs">5. Amazon CloudWatch Logs</h3>
<p>Each Lambda function created by Chalice automatically generates log entries in Amazon CloudWatch Logs. This allows you to monitor and debug your application by viewing log output.</p>
<h2 id="heading-deploying-a-hello-world-application-with-chalice">Deploying a Hello World Application with Chalice</h2>
<p>Let's walk through an example of deploying a simple "Hello World" application using Chalice. We'll cover the steps required to create, deploy, and test the application.</p>
<h3 id="heading-step-1-install-chalice">Step 1: Install Chalice</h3>
<p>First, you'll need to install Chalice using pip:</p>
<pre><code class="lang-python">pip install chalice
</code></pre>
<h3 id="heading-step-2-create-a-new-chalice-project">Step 2: Create a New Chalice Project</h3>
<p>Next, create a new Chalice project:</p>
<pre><code class="lang-python">chalice new-project hello-world
cd hello-world
</code></pre>
<h3 id="heading-step-3-define-the-application">Step 3: Define the Application</h3>
<p>Open the app.py file in your project directory and define a simple route that returns "Hello, World!":</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> chalice <span class="hljs-keyword">import</span> Chalice
app = Chalice(app_name=<span class="hljs-string">'hello-world'</span>)
<span class="hljs-meta">@app.route('/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span>():</span>
    <span class="hljs-keyword">return</span> {<span class="hljs-string">'message'</span>: <span class="hljs-string">'Hello, World!'</span>}
</code></pre>
<h3 id="heading-step-4-deploy-the-application">Step 4: Deploy the Application</h3>
<p>Deploy your application to AWS with a single command:</p>
<pre><code class="lang-python">chalice deploy
</code></pre>
<p>Chalice will package your application, create the necessary AWS resources (such as Lambda functions and API Gateway endpoints), and deploy the application.</p>
<h3 id="heading-step-5-test-the-application">Step 5: Test the Application</h3>
<p>After the deployment is complete, Chalice will provide you with the URL of your deployed API. You can test the application by visiting the URL in your web browser or using a tool like curl:</p>
<pre><code class="lang-python">curl https://&lt;api-id&gt;.execute-api.&lt;region&gt;.amazonaws.com/api/
</code></pre>
<p>You should see a JSON response with the message "Hello, World!".</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>AWS Chalice is a powerful and user-friendly framework that simplifies the process of deploying Python serverless applications on AWS. By abstracting much of the complexity involved in serverless deployments, Chalice allows developers to focus on writing code and building features. Whether you're a seasoned developer or new to the world of serverless computing, Chalice can help you deploy your applications quickly and efficiently. Give it a try, and say goodbye to serverless deployment frustration!</p>
<p>For more detailed information, check out the official Chalice documentation.</p>
<h2 id="heading-faqs">FAQs</h2>
<ol>
<li><h3 id="heading-what-is-aws-chalice">What is AWS Chalice?</h3>
</li>
</ol>
<p>AWS Chalice is a Python framework developed by AWS that simplifies the creation and deployment of serverless applications. It abstracts much of the complexity involved in managing AWS Lambda functions and API Gateway endpoints, allowing developers to focus on writing code.</p>
<ol start="2">
<li><h3 id="heading-how-is-chalice-different-from-aws-sam-1">How is Chalice different from AWS SAM?</h3>
</li>
</ol>
<p>Chalice is specifically designed for Python and offers a higher level of abstraction with Pythonic interfaces, whereas AWS SAM supports multiple languages and requires detailed CloudFormation templates. Chalice simplifies the development process, while SAM provides more control and customization options.</p>
<ol start="3">
<li><h3 id="heading-what-aws-resources-are-created-when-deploying-a-chalice-application">What AWS resources are created when deploying a Chalice application?</h3>
</li>
</ol>
<p>When deploying a Chalice application, the following AWS resources are created: AWS Lambda functions, API Gateway endpoints, IAM roles and policies, a CloudFormation stack, and Amazon CloudWatch Logs.</p>
<ol start="4">
<li><h3 id="heading-can-i-test-my-chalice-application-locally">Can I test my Chalice application locally?</h3>
</li>
</ol>
<p>Yes, Chalice includes a built-in local development server that allows you to test your application locally before deploying it to AWS. This helps you catch and fix issues early in the development process.</p>
<ol start="5">
<li><h3 id="heading-how-do-i-deploy-a-chalice-application">How do I deploy a Chalice application?</h3>
</li>
</ol>
<p>To deploy a Chalice application, navigate to your project directory and run the chalice deploy command. Chalice will package your application, create the necessary AWS resources, and deploy the application.</p>
<ol start="6">
<li><h3 id="heading-is-chalice-suitable-for-production-applications">Is Chalice suitable for production applications?</h3>
</li>
</ol>
<p>Yes, Chalice is suitable for both development and production applications. It integrates seamlessly with various AWS services, providing the scalability, reliability, and performance needed for production workloads.</p>
]]></content:encoded></item><item><title><![CDATA[How to Use AWS CloudFront for Image Redirection to Default Images]]></title><description><![CDATA[In this blog, we'll explore how to use AWS CloudFront to serve a default image when your origin returns a 404 error. By implementing a Lambda@Edge function, we can ensure that users always receive a placeholder image instead of a broken link.
Why Red...]]></description><link>https://blog.cloudgags.in/how-to-use-aws-cloudfront-for-image-redirection-to-default-images</link><guid isPermaLink="true">https://blog.cloudgags.in/how-to-use-aws-cloudfront-for-image-redirection-to-default-images</guid><category><![CDATA[origin response]]></category><category><![CDATA[AWS]]></category><category><![CDATA[cloudfront]]></category><category><![CDATA[lambda@edge]]></category><category><![CDATA[404 page]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 29 Jun 2024 18:33:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1719685518016/d8b88b5c-ff97-4e1e-b642-0b5c6c892a34.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this blog, we'll explore how to use <a target="_blank" href="https://cloudgags.in/blog/aws/cloudfront/">AWS CloudFront</a> to serve a default image when your origin returns a 404 error. By implementing a Lambda@Edge function, we can ensure that users always receive a placeholder image instead of a broken link.</p>
<h2 id="heading-why-redirect-images-on-404">Why Redirect Images on 404?</h2>
<p>When a requested image is missing, returning a 404 error can lead to a poor user experience. Redirecting to a default image ensures that your website remains visually consistent and user-friendly.</p>
<h2 id="heading-setting-up-aws-cloudfront-with-lambdaedge">Setting Up AWS CloudFront with Lambda@Edge</h2>
<h3 id="heading-step-1-create-a-cloudfront-distribution">Step 1: Create a CloudFront Distribution</h3>
<p>First, create a CloudFront distribution that points to your origin server where your images are hosted. Ensure that your distribution is properly configured to handle the necessary requests.</p>
<h3 id="heading-step-2-write-the-lambdaedge-function">Step 2: Write the Lambda@Edge Function</h3>
<p>The core of this solution involves using a Lambda@Edge function triggered on the <strong>Origin Response</strong> event. This function checks the status code from the origin and redirects to a default image if a 404 error is detected.</p>
<p>Here’s a breakdown of the code:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_handler</span>(<span class="hljs-params">event, context</span>):</span>
    response = event[<span class="hljs-string">'Records'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'cf'</span>][<span class="hljs-string">'response'</span>]
    response_headers = response[<span class="hljs-string">'headers'</span>]

    <span class="hljs-keyword">if</span> response[<span class="hljs-string">'status'</span>] == <span class="hljs-string">'404'</span>:
        <span class="hljs-keyword">if</span> <span class="hljs-string">'transfer-encoding'</span> <span class="hljs-keyword">in</span> response_headers:
            <span class="hljs-keyword">return</span> {
                <span class="hljs-string">'status'</span>: <span class="hljs-string">'302'</span>,
                <span class="hljs-string">'headers'</span>: {
                    <span class="hljs-string">'location'</span>: [{
                        <span class="hljs-string">'key'</span>: <span class="hljs-string">'Location'</span>,
                        <span class="hljs-string">'value'</span>: <span class="hljs-string">"DEFAULT-IMAGE-URL"</span>
                    }],
                    <span class="hljs-string">'transfer-encoding'</span>: [{
                        <span class="hljs-string">'key'</span>: <span class="hljs-string">'Transfer-Encoding'</span>,
                        <span class="hljs-string">'value'</span>: <span class="hljs-string">'chunked'</span>
                    }]
                }
            }
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> {
                <span class="hljs-string">'status'</span>: <span class="hljs-string">'302'</span>,
                <span class="hljs-string">'headers'</span>: {
                    <span class="hljs-string">'location'</span>: [{
                        <span class="hljs-string">'key'</span>: <span class="hljs-string">'Location'</span>,
                        <span class="hljs-string">'value'</span>: <span class="hljs-string">"DEFAULT-IMAGE-URL"</span>
                    }]
                }
            }
    <span class="hljs-keyword">return</span> response
</code></pre>
<h3 id="heading-code-explanation">Code Explanation</h3>
<ol>
<li><p><strong>Event Trigger</strong>: The function is triggered on the <strong>Origin Response</strong> event.</p>
</li>
<li><p><strong>Status Check</strong>: It checks if the response status code is <code>404</code>.</p>
</li>
<li><p><strong>Transfer-Encoding Handling</strong>: If the <code>Transfer-Encoding</code> header is present, it includes it in the redirection response to prevent any CloudFront validation errors.</p>
</li>
<li><p><strong>Redirection</strong>: If a 404 is detected, it redirects to the specified default image URL.</p>
</li>
</ol>
<h3 id="heading-step-3-deploy-the-lambdaedge-function">Step 3: Deploy the Lambda@Edge Function</h3>
<ol>
<li><p><strong>Create the Lambda Function</strong>: Go to the AWS Lambda console and create a new function.</p>
</li>
<li><p><strong>Set Permissions</strong>: Ensure the function has the necessary permissions to execute.</p>
</li>
<li><p><strong>Attach to CloudFront</strong>: In the CloudFront distribution settings, attach the Lambda@Edge function to the <strong>Origin Response</strong> event.</p>
</li>
</ol>
<h3 id="heading-step-4-test-the-configuration">Step 4: Test the Configuration</h3>
<p>After deployment, test the configuration by requesting an image that doesn’t exist on your origin. You should be redirected to the default image.</p>
<h3 id="heading-step-5-monitor-and-optimize">Step 5: Monitor and Optimize</h3>
<p>Monitor your CloudFront logs to ensure that redirections occur as expected and optimize the Lambda function as necessary to handle additional scenarios.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>By implementing a Lambda@Edge function with AWS CloudFront, you can efficiently handle missing images by redirecting to a default placeholder. This enhances the user experience and maintains the integrity of your website’s design.</p>
<h3 id="heading-key-takeaways">Key Takeaways:</h3>
<ul>
<li><p><strong>Use Lambda@Edge</strong>: To customize content delivery behavior at the edge.</p>
</li>
<li><p><strong>Handle 404 Errors Gracefully</strong>: Redirect users to a default image.</p>
</li>
<li><p><strong>Maintain Transfer-Encoding</strong>: To prevent CloudFront validation errors.</p>
</li>
</ul>
<p>By following these steps, you’ll ensure a seamless experience for your users, even when images are missing. Happy coding!</p>
]]></content:encoded></item><item><title><![CDATA[AWS CloudFront: How to Secure Your S3, ALB, and EC2 Origins]]></title><description><![CDATA[Securing your origins in AWS CloudFront is essential to protect your resources from unauthorized access and ensure efficient content delivery. This blog will guide you through the steps to restrict traffic to different origins (S3, ALB, and EC2) and ...]]></description><link>https://blog.cloudgags.in/aws-cloudfront-how-to-secure-your-s3-alb-and-ec2-origins</link><guid isPermaLink="true">https://blog.cloudgags.in/aws-cloudfront-how-to-secure-your-s3-alb-and-ec2-origins</guid><category><![CDATA[AWS]]></category><category><![CDATA[cloudfront]]></category><category><![CDATA[origin]]></category><category><![CDATA[alb]]></category><category><![CDATA[S3]]></category><category><![CDATA[ec2]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[oai]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 29 Jun 2024 17:14:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1719680723732/b3b7c4c6-6397-441e-8285-89c27f532058.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Securing your origins in AWS CloudFront is essential to protect your resources from unauthorized access and ensure efficient content delivery. This blog will guide you through the steps to restrict traffic to different origins (S3, ALB, and EC2) and make them accessible only via CloudFront.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#introduction">Introduction</a></p>
</li>
<li><p><a class="post-section-overview" href="#what-is-oac-and-aws-managed-prefix-list">What is OAC and AWS Managed Prefix List</a></p>
</li>
<li><p><a class="post-section-overview" href="#securing-s3-origins">Securing S3 Origins</a></p>
</li>
<li><p><a class="post-section-overview" href="#securing-alb-origins">Securing ALB Origins</a></p>
<ul>
<li><p>Custom Header-Based Authentication</p>
</li>
<li><p>Using AWS Managed Prefix Lists</p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#securing-ec2-origins">Securing EC2 Origins</a></p>
</li>
<li><p><a class="post-section-overview" href="#best-practices-and-recommendations">Best Practices and Recommendations</a></p>
</li>
<li><p><a class="post-section-overview" href="#summary">Summary</a></p>
</li>
</ol>
<h2 id="heading-introduction">Introduction</h2>
<p>AWS CloudFront is a content delivery network (CDN) that allows you to distribute content with low latency and high transfer speeds. To ensure the security of your content, it's crucial to restrict access to your origins (S3, ALB, and EC2) so that they can only be accessed via CloudFront. This blog will detail the methods to secure each type of origin.</p>
<h2 id="heading-what-is-oac-and-aws-managed-prefix-list">What is OAC and AWS Managed Prefix List</h2>
<h3 id="heading-origin-access-control-oac">Origin Access Control (OAC)</h3>
<p>Origin Access Control (OAC) is a feature introduced by AWS CloudFront that allows you to restrict access to your Amazon S3 buckets so that they can only be accessed through CloudFront. This enhances the security of your content by preventing direct access to the S3 bucket. OAC replaces the older Origin Access Identity (OAI) with a more flexible and powerful approach.</p>
<p>For more details, you can refer to the <a target="_blank" href="https://aws.amazon.com/blogs/networking-and-content-delivery/amazon-cloudfront-introduces-origin-access-control-oac/">official AWS blog on OAC</a>.</p>
<h3 id="heading-aws-managed-prefix-list">AWS Managed Prefix List</h3>
<p>AWS Managed Prefix List for Amazon CloudFront is a feature that allows you to create a list of IP ranges used by CloudFront. This list can then be used to configure security groups and network ACLs to ensure that only traffic from CloudFront can reach your origins (ALB and EC2). By using the managed prefix list, you simplify the process of managing IP ranges and enhance security.</p>
<p>For more details, you can refer to the <a target="_blank" href="https://aws.amazon.com/blogs/networking-and-content-delivery/limit-access-to-your-origins-using-the-aws-managed-prefix-list-for-amazon-cloudfront/">official AWS blog on AWS Managed Prefix Lists</a>.</p>
<h2 id="heading-securing-s3-origins">Securing S3 Origins</h2>
<p>Amazon S3 (Simple Storage Service) is commonly used as an origin for CloudFront distributions. To restrict access to your S3 bucket so that it can only be accessed through CloudFront, we will use Origin Access Control (OAC).</p>
<h3 id="heading-steps-to-secure-s3-using-oac">Steps to Secure S3 Using OAC</h3>
<ol>
<li><p><strong>Create an S3 Bucket</strong>:</p>
<ul>
<li><p>Go to the S3 console and create a new bucket or use an existing one.</p>
</li>
<li><p>Ensure that your bucket and objects are not publicly accessible.</p>
</li>
</ul>
</li>
<li><p><strong>Set Up Origin Access Control (OAC)</strong>:</p>
<ul>
<li><p>In the CloudFront console, navigate to your distribution and choose the "Origins and Origin Groups" tab.</p>
</li>
<li><p>Select your S3 origin and choose "Edit".</p>
</li>
<li><p>In the "Restrict Bucket Access" section, select "Yes".</p>
</li>
<li><p>Create a new OAC or use an existing one. Ensure that the OAC is associated with your CloudFront distribution.</p>
</li>
</ul>
</li>
<li><p><strong>Update S3 Bucket Policy</strong>:</p>
<ul>
<li><p>Go to your S3 bucket permissions and update the bucket policy to allow access from CloudFront.</p>
</li>
<li><p>The policy should look something like this:</p>
<pre><code class="lang-json">  {
    <span class="hljs-attr">"Version"</span>: <span class="hljs-string">"2012-10-17"</span>,
    <span class="hljs-attr">"Statement"</span>: [
      {
        <span class="hljs-attr">"Effect"</span>: <span class="hljs-string">"Allow"</span>,
        <span class="hljs-attr">"Principal"</span>: {
          <span class="hljs-attr">"AWS"</span>: <span class="hljs-string">"arn:aws:iam::cloudfront:user/Input_CloudFront_Origin_Access_Identity_OAC_ID"</span>
        },
        <span class="hljs-attr">"Action"</span>: <span class="hljs-string">"s3:GetObject"</span>,
        <span class="hljs-attr">"Resource"</span>: <span class="hljs-string">"arn:aws:s3:::your-bucket-name/*"</span>
      }
    ]
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>By following these steps, you ensure that your S3 bucket can only be accessed through your CloudFront distribution.</p>
<h2 id="heading-securing-alb-origins">Securing ALB Origins</h2>
<p>Application Load Balancers (ALB) can also be used as origins for CloudFront. To restrict access to your ALB, we will use two methods: custom header-based authentication and AWS Managed Prefix Lists.</p>
<h3 id="heading-method-1-custom-header-based-authentication">Method 1: Custom Header-Based Authentication</h3>
<ol>
<li><p><strong>Configure Custom Headers in CloudFront</strong>:</p>
<ul>
<li><p>In the CloudFront console, navigate to your distribution and select the "Origins and Origin Groups" tab.</p>
</li>
<li><p>Edit your ALB origin and add a custom header, e.g., <code>X-CloudFront-Secret: YourSecretValue</code>.</p>
</li>
</ul>
</li>
<li><p><strong>Set Up ALB Listener Rules</strong>:</p>
<ul>
<li><p>Go to the EC2 console and select your ALB.</p>
</li>
<li><p>Navigate to the "Listeners" tab and select "View/edit rules".</p>
</li>
<li><p>Add a rule to check for the custom header <code>X-CloudFront-Secret</code> with the value <code>YourSecretValue</code>. If the header matches, forward the request to your target group; otherwise, return a 403 Forbidden response.</p>
</li>
</ul>
</li>
<li><p><strong>Use HTTPS for Origin Requests</strong>:</p>
<ul>
<li><p>To improve the security of this solution, configure your CloudFront distribution to always use HTTPS when sending requests to your Application Load Balancer.</p>
</li>
<li><p>This solution only works if you keep the custom header name and value secret. Using HTTPS can help prevent an eavesdropper from discovering the header name and value.</p>
</li>
<li><p>Rotate the header name and value periodically to enhance security.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-method-2-aws-managed-prefix-lists">Method 2: AWS Managed Prefix Lists</h3>
<ol>
<li><p><strong>Create a Managed Prefix List</strong>:</p>
<ul>
<li>In the VPC console, create a new managed prefix list and add the IP ranges used by CloudFront.</li>
</ul>
</li>
<li><p><strong>Update ALB Security Group</strong>:</p>
<ul>
<li><p>Go to the EC2 console and select your ALB.</p>
</li>
<li><p>Navigate to the "Security Groups" section and edit the inbound rules.</p>
</li>
<li><p>Add a rule to allow traffic only from the IP ranges in your managed prefix list.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-note">Note:</h3>
<p>The managed prefix list for CloudFront counts as 55 routes in a route table. The default quota is 50 routes, so you must request a quota increase before you can add the prefix list to a route table.</p>
<p>By implementing these methods, you ensure that only CloudFront can access your ALB, adding an extra layer of security.</p>
<h2 id="heading-securing-ec2-origins">Securing EC2 Origins</h2>
<p>While it's recommended to use an ALB as an origin for better management and security, you can also use EC2 instances directly as origins. To restrict access to your EC2 instances, we will use AWS Managed Prefix Lists.</p>
<h3 id="heading-steps-to-secure-ec2-using-managed-prefix-lists">Steps to Secure EC2 Using Managed Prefix Lists</h3>
<ol>
<li><p><strong>Create a Managed Prefix List</strong>:</p>
<ul>
<li>In the VPC console, create a new managed prefix list and add the IP ranges used by CloudFront.</li>
</ul>
</li>
<li><p><strong>Update EC2 Security Group</strong>:</p>
<ul>
<li><p>Go to the EC2 console and select your instance.</p>
</li>
<li><p>Navigate to the "Security Groups" section and edit the inbound rules.</p>
</li>
<li><p>Add a rule to allow traffic only from the IP ranges in your managed prefix list.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-note-1">Note:</h3>
<p>For better scalability and security, it's recommended to use an ALB as an origin and connect your EC2 instances to the ALB. This setup simplifies management and enhances security by leveraging the features of ALB.</p>
<h2 id="heading-best-practices-and-recommendations">Best Practices and Recommendations</h2>
<ul>
<li><p><strong>Use CloudFront Origin Access Control (OAC)</strong>: For S3 buckets, always use OAC to restrict access.</p>
</li>
<li><p><strong>Implement Custom Header Authentication</strong>: For ALBs, use custom headers to ensure only CloudFront can access your ALB.</p>
</li>
<li><p><strong>Leverage Managed Prefix Lists</strong>: Use AWS Managed Prefix Lists to restrict access based on CloudFront's IP ranges. Be mindful of the route table limit and request a quota increase if necessary.</p>
</li>
<li><p><strong>Prefer ALB over Direct EC2 Access</strong>: Use an ALB as an origin and connect your EC2 instances to the ALB for better security and management.</p>
</li>
</ul>
<h2 id="heading-summary">Summary</h2>
<p>Securing your AWS CloudFront origins is crucial to protect your resources and ensure secure content delivery. By following the steps outlined in this blog, you can effectively restrict access to your S3, ALB, and EC2 origins and make them accessible only via CloudFront. Implementing these security measures will help safeguard your content and improve the overall security posture of your AWS infrastructure.</p>
<p>By using OAC for S3, custom header-based authentication and managed prefix lists for ALB, and managed prefix lists for EC2, you can ensure that your origins are secure and only accessible through CloudFront. For best practices, consider using an ALB as an origin instead of direct EC2 access to enhance security and simplify management.</p>
]]></content:encoded></item><item><title><![CDATA[AWS Lambda@Edge: How to control cache TTL?]]></title><description><![CDATA[Hello friends! Today we will see how we can control the cache generated by the AWS CloudFront using the AWS Lambda@Edge functions.
As we all know, AWS CloudFront is a Content Delivery service which helps us in boosting our content delivery.  AWS Clou...]]></description><link>https://blog.cloudgags.in/aws-lambdaedge-how-to-control-cache-ttl</link><guid isPermaLink="true">https://blog.cloudgags.in/aws-lambdaedge-how-to-control-cache-ttl</guid><category><![CDATA[cache-control]]></category><category><![CDATA[AWS]]></category><category><![CDATA[lambda]]></category><category><![CDATA[lambda@edge]]></category><category><![CDATA[cache]]></category><category><![CDATA[TTL]]></category><category><![CDATA[edge locations]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sun, 16 Jun 2024 15:50:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1719681495833/05e95484-a85e-4b1d-81e6-f5a77cadddf9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello friends! Today we will see how we can control the cache generated by the AWS CloudFront using the AWS <a target="_blank" href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html">Lambda@Edge</a> functions.</p>
<p>As we all know, AWS CloudFront is a Content Delivery service which helps us in boosting our content delivery.  AWS CloudFront achieves this by maintaining cache on its edge locations. Since our content is already cached, our content is delivered to the user with the lowest latency.</p>
<p><a target="_blank" href="https://aws.amazon.com/cloudfront/">AWS CloudFront</a> allows us to manage the cache built on the edge location by defining cache policies based on the requirement.</p>
<p>These <a target="_blank" href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html">cache policies</a> have different parameters like what should be the default time for which the content should be caches, along with the minimum and maximum time.</p>
<p>But if we can use cache policies then why do we need to mimic the same using the lambda@edge function?</p>
<p>Actually, in ideal cases there is no need. Cache policies are capable enough to get the job done. This approach/technique is more focused towards someone who needs some customizations.</p>
<h2 id="heading-problem">Problem</h2>
<p>To understand the use case let's take an example. Assume that you are having a blog site just like <a target="_blank" href="https://cloudgags.in/">CloudGags</a> which is getting served using the AWS CloudFront.</p>
<p>There is one behaviour configures "<strong>/blog/*"</strong> which serves all your blog content. Now a situation arises that for a specific URL you need a separate TTL configured.</p>
<p>The above problem can be solved by creating a URL specific behaviour, but what if you are running out of behaviours.</p>
<p>Thats right, in AWS CloudFront there is a <a target="_blank" href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html">limit</a> on the no. of behaviour you can create. You can create only 250 behaviours at max. Now if you have consumed all of the behaviour limit then how you can configure that one separate caching?</p>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>As a solution to the above problem, you can use Lambda@Edge functions. Lambda@Edge allows us to do some customizations on the fly while your content is getting served via the AWS CloudFront.</p>
<p>To control the TTL of our content we will be using the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching">Cache-Control</a> header. The Cache-Control header allows us to control the TTL(Time-To-Live) of the cache available on the AWS Edge locations.</p>
<pre><code class="lang-python"><span class="hljs-comment">#ATTACH THIS FUNCTION ON THE ORIGIN RESPONSE</span>

<span class="hljs-keyword">import</span> json

<span class="hljs-comment"># URL to cache for 20 seconds</span>
CUSTOM_CACHE_URL = [<span class="hljs-string">"/some/url"</span>]

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_handler</span>(<span class="hljs-params">event, context</span>):</span>
    <span class="hljs-comment"># Extracting request and response objects from the Lambda event</span>
    request = event[<span class="hljs-string">'Records'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'cf'</span>][<span class="hljs-string">'request'</span>]
    response = event[<span class="hljs-string">'Records'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'cf'</span>][<span class="hljs-string">'response'</span>]
    <span class="hljs-comment"># Checking for url</span>
    <span class="hljs-keyword">if</span> request[<span class="hljs-string">'uri'</span>] <span class="hljs-keyword">in</span> CUSTOM_CACHE_URL:
        <span class="hljs-comment"># Setting cache value</span>
        response[<span class="hljs-string">'headers'</span>][<span class="hljs-string">'cache-control'</span>] = [{<span class="hljs-string">"key"</span>: <span class="hljs-string">"Cache-Control"</span>,<span class="hljs-string">"value"</span>: <span class="hljs-string">"s-maxage=20"</span>}]

    <span class="hljs-comment"># Returning response</span>
    <span class="hljs-keyword">return</span> response
</code></pre>
<h2 id="heading-explanation"><strong>Explanation</strong></h2>
<p><mark>Note: We will be applying the above code on the Origin-Response event of the CloudFront.</mark></p>
<p>The python code contains a list "<strong>CUSTOM_CACHE_URL</strong>" which holds all the URLS which needs to be cached for 20 seconds.</p>
<p>Then the function checks weather the URI falls in the list available for custom caching. If the condition is satisfied, the cache control header is set in the response and the response is returned.</p>
<p>The "<strong>s-maxage"</strong> value is used when we need to manipulate the cache on the edge locations. You can also use "<strong>max-age"</strong> in place of "<strong>s-maxage"</strong> if you want to manipulate the cache TTL on edge locations as well as on the user browser too.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>In conclusion, you can dynamically manage cache settings, ensuring that your content is served quickly and efficiently. As the digital landscape continues to evolve, mastering these techniques will keep you ahead of the curve, providing your users with fast, responsive, and reliable access to your content.</p>
<p>Embrace AWS Lambda@Edge for effective cache management and take your website's performance to the next level.</p>
]]></content:encoded></item><item><title><![CDATA[AWS CloudFormation: How to create a CodeCommit repository?]]></title><description><![CDATA[Hello guys! Today we will be learning how to create an AWS CodeCommit repository using the AWS CloudFormation service.
What is AWS CloudFormation Service?
To explain in short, AWS CloudFormation is a service provided AWS which helps AWS cloud users t...]]></description><link>https://blog.cloudgags.in/aws-cloudformation-how-to-create-a-codecommit-repository</link><guid isPermaLink="true">https://blog.cloudgags.in/aws-cloudformation-how-to-create-a-codecommit-repository</guid><category><![CDATA[AWS]]></category><category><![CDATA[CodeCommit]]></category><category><![CDATA[cloudformation]]></category><category><![CDATA[S3]]></category><category><![CDATA[Git]]></category><category><![CDATA[#IaC]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Wed, 12 Jun 2024 12:32:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/B3l0g6HLxr8/upload/98506a1fdbe43de2d3e01c2ff4893833.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello guys! Today we will be learning how to create an AWS CodeCommit repository using the AWS CloudFormation service.</p>
<h2 id="heading-what-is-aws-cloudformation-service">What is AWS CloudFormation Service?</h2>
<p>To explain in short, <a target="_blank" href="https://aws.amazon.com/cloudformation/">AWS CloudFormation</a> is a service provided AWS which helps AWS cloud users to create infrastructure/resources on AWS in a very efficient way.</p>
<p>The CloudFormation service comes under the category of Infrastructure as Code (IaC). By using this service, we can create all out AWS infrastructure just by using a single JSON/YAML file. And the best part is we can use this file whenever and wherever in an AWS account.</p>
<p>To know more on AWS CloudFormation, you can refer the <a target="_blank" href="https://cloudgags.in/blog/aws/cloudformation/mastering-aws-cloudformation-a-comprehensive-overview/">blog</a> which covers full background of AWS CloudFormation.</p>
<h2 id="heading-what-is-a-codecommit-repository">What is a CodeCommit repository?</h2>
<p><a target="_blank" href="https://aws.amazon.com/codecommit/">AWS CodeCommit</a> is a Git based storage service offered by AWS for all the developers who want to store their code on AWS.</p>
<p>This service is similar to the service provided by GitHub or GitLab.</p>
<p>Software Developers and Engineers can use this service if they want their code to integrate with other AWS components or services easily.</p>
<h2 id="heading-cloudformation-template">CloudFormation Template</h2>
<pre><code class="lang-json">{
  <span class="hljs-attr">"AWSTemplateFormatVersion"</span>: <span class="hljs-string">"2010-09-09"</span>,
  <span class="hljs-attr">"Description"</span>: <span class="hljs-string">"Template for creating a codecommit repository"</span>,
  <span class="hljs-attr">"Parameters"</span>: {
    <span class="hljs-attr">"RepositoryName"</span>: {
      <span class="hljs-attr">"Type"</span>: <span class="hljs-string">"String"</span>,
      <span class="hljs-attr">"Description"</span>: <span class="hljs-string">"Enter codecommit repository name"</span>
    },
    <span class="hljs-attr">"S3BucketName"</span>: {
      <span class="hljs-attr">"Type"</span>: <span class="hljs-string">"String"</span>,
      <span class="hljs-attr">"Description"</span>: <span class="hljs-string">"Enter the S3 bucket name where all the code files are stored"</span>
    },
    <span class="hljs-attr">"S3KeyForZipFile"</span>: {
      <span class="hljs-attr">"Type"</span>: <span class="hljs-string">"String"</span>,
      <span class="hljs-attr">"Description"</span>: <span class="hljs-string">"Enter the S3 file key for code zip files"</span>
    }
  },
  <span class="hljs-attr">"Resources"</span>: {
    <span class="hljs-attr">"CodeCommitRepo"</span>: {
      <span class="hljs-attr">"Type"</span>: <span class="hljs-string">"AWS::CodeCommit::Repository"</span>,
      <span class="hljs-attr">"Properties"</span>: {
        <span class="hljs-attr">"RepositoryName"</span>: { <span class="hljs-attr">"Ref"</span>: <span class="hljs-string">"RepositoryName"</span> },
        <span class="hljs-attr">"RepositoryDescription"</span>: { <span class="hljs-attr">"Ref"</span>: <span class="hljs-string">"RepositoryName"</span> },
        <span class="hljs-attr">"Code"</span>: {
          <span class="hljs-attr">"BranchName"</span>: <span class="hljs-string">"master"</span>,
          <span class="hljs-attr">"S3"</span>: {
            <span class="hljs-attr">"Bucket"</span>: { <span class="hljs-attr">"Ref"</span>: <span class="hljs-string">"S3BucketName"</span> },
            <span class="hljs-attr">"Key"</span>: {
              <span class="hljs-attr">"Ref"</span>: <span class="hljs-string">"S3KeyForZipFile"</span>
            }
          }
        },
        <span class="hljs-attr">"Tags"</span>: [
          {
            <span class="hljs-attr">"Key"</span>: <span class="hljs-string">"Purpose"</span>,
            <span class="hljs-attr">"Value"</span>: <span class="hljs-string">"Education"</span>
          }
        ]
      }
    }
  }
}
</code></pre>
<h2 id="heading-template-parameters-explanation">Template Parameters Explanation</h2>
<p>In the above template we have used 3 parameters.</p>
<ol>
<li><p>RepositoryName</p>
<p> Here you need to specify what should be your repository name.</p>
</li>
<li><p>S3BucketName</p>
<p> This parameter asks what is the name of the S3 bucket were the code files have been stored.</p>
</li>
<li><p>S3KeyForZipFile</p>
<p> Here you need to specify the key path for the zip file.</p>
</li>
</ol>
<p><strong><mark>Note</mark></strong><mark>: You need to first zip all your code files and then upload them in your S3 bucket. AWS CodeCommit service will automatically unzip files.</mark></p>
<h2 id="heading-template-input-example">Template Input Example</h2>
<p>Let's assume I am using a S3 bucket named "my-project-files". I have created a zip of my code files and uploaded in my S3 bucket. The name of the zip is <strong>code.zip.</strong></p>
<p>Considering the above assumptions this will how my inputs will look like when I run the template.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1718193992553/a785e3e8-9108-408c-b311-21269edc49ab.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>AWS CloudFormation provides a powerful way to automate the creation and management of AWS resources, including CodeCommit repositories.</p>
<p>By leveraging infrastructure as code (IaC), you can ensure consistent and efficient deployments. Understanding the basics of CloudFormation and CodeCommit is crucial, and with the provided JSON template and detailed explanations, you now have the tools to set up your own repositories seamlessly.</p>
<p>Embrace automation and take full advantage of AWS services to streamline your development and operations processes.</p>
]]></content:encoded></item><item><title><![CDATA[AWS Bedrock: Easy 3 Step Image Generation App Guide]]></title><description><![CDATA[Hello Everyone! Today we will learn how to create an image generation Ai based application in just 3 simple steps.
Today we will be taking help of AWS Bedrock, Langchain, StreamLit and python based AWS Boto3 library to create the application.
Follow ...]]></description><link>https://blog.cloudgags.in/aws-bedrock-easy-3-step-image-generation-app-guide</link><guid isPermaLink="true">https://blog.cloudgags.in/aws-bedrock-easy-3-step-image-generation-app-guide</guid><category><![CDATA[AWS]]></category><category><![CDATA[AWS Bedrock]]></category><category><![CDATA[streamlit]]></category><category><![CDATA[langchain]]></category><category><![CDATA[boto3]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 08 Jun 2024 18:45:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1717872111374/62dd7ce7-b7b2-4794-8309-426de94e1ba3.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello Everyone! Today we will learn how to create an image generation Ai based application in just 3 simple steps.</p>
<p>Today we will be taking help of <strong>AWS Bedrock, Langchain, StreamLit and python based AWS Boto3</strong> library to create the application.</p>
<p>Follow along and you will understand how easy to os to create such amazing application using the AWS Bedrock service.</p>
<p>So lets get started and learn some GenAI stuff.</p>
<h2 id="heading-prerequisite">Prerequisite</h2>
<p>In this example we will using the AWS Bedrock service. We will be using the <strong>models</strong> provided by the <strong>StabilityAI</strong> Before getting stated with the example you must request access for the <strong>StabilityAI</strong> models.</p>
<p>Also since the bedrock service is not available in all the AWS regions we will be using the <strong>us-east-1</strong> region for this example.</p>
<h3 id="heading-action-items">Action Items</h3>
<ol>
<li><p>Access for the <strong>StabilityAI</strong> models in the us-east-1 region.</p>
</li>
<li><p>Generate and configure access keys to execute the below python script. You will need the Bedrock service access.</p>
</li>
</ol>
<h2 id="heading-lets-create-the-application">Lets create the application</h2>
<h3 id="heading-step-1-create-the-backend">Step 1 - Create the Backend</h3>
<p>Create an <strong>image_lib.py</strong> and paste the below code in it.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> boto3 <span class="hljs-comment">#import aws sdk and supporting libraries</span>
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> base64
<span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> BytesIO

session = boto3.Session()

bedrock = session.client(service_name=<span class="hljs-string">'bedrock-runtime'</span>) <span class="hljs-comment">#creates a Bedrock client</span>

bedrock_model_id = <span class="hljs-string">"stability.stable-diffusion-xl-v1"</span> <span class="hljs-comment">#use the Stable Diffusion model</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_response_image_from_payload</span>(<span class="hljs-params">response</span>):</span> <span class="hljs-comment">#returns the image bytes from the model response payload</span>

    payload = json.loads(response.get(<span class="hljs-string">'body'</span>).read()) <span class="hljs-comment">#load the response body into a json object</span>
    images = payload.get(<span class="hljs-string">'artifacts'</span>) <span class="hljs-comment">#extract the image artifacts</span>
    image_data = base64.b64decode(images[<span class="hljs-number">0</span>].get(<span class="hljs-string">'base64'</span>)) <span class="hljs-comment">#decode image</span>

    <span class="hljs-keyword">return</span> BytesIO(image_data) <span class="hljs-comment">#return a BytesIO object for client app consumption</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_image_response</span>(<span class="hljs-params">prompt_content</span>):</span> <span class="hljs-comment">#text-to-text client function</span>

    request_body = json.dumps({<span class="hljs-string">"text_prompts"</span>: 
                               [ {<span class="hljs-string">"text"</span>: prompt_content } ], <span class="hljs-comment">#prompts to use</span>
                               <span class="hljs-string">"cfg_scale"</span>: <span class="hljs-number">9</span>, <span class="hljs-comment">#how closely the model tries to match the prompt</span>
                               <span class="hljs-string">"steps"</span>: <span class="hljs-number">50</span>, }) <span class="hljs-comment">#number of diffusion steps to perform</span>

    response = bedrock.invoke_model(body=request_body, modelId=bedrock_model_id) <span class="hljs-comment">#call the Bedrock endpoint</span>

    output = get_response_image_from_payload(response) <span class="hljs-comment">#convert the response payload to a BytesIO object for the client to consume</span>

    <span class="hljs-keyword">return</span> output
</code></pre>
<h3 id="heading-step-2-create-the-frontend">Step 2 - Create the Frontend</h3>
<p>We will be creating the UI of the application with the help of StreamLit library.</p>
<p>Create an <strong>image_ap</strong>Create an image generation app with AWS Bedrock using LangChain, streamlit with Boto3, and use Python for GenAI.<strong>p.py</strong> and paste the below code in it.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> streamlit <span class="hljs-keyword">as</span> st <span class="hljs-comment">#all streamlit commands will be available through the "st" alias</span>
<span class="hljs-keyword">import</span> image_lib <span class="hljs-keyword">as</span> glib <span class="hljs-comment">#reference to local lib script</span>

st.set_page_config(layout=<span class="hljs-string">"wide"</span>, page_title=<span class="hljs-string">"Image Generation"</span>) <span class="hljs-comment">#set the page width wider to accommodate columns</span>

st.title(<span class="hljs-string">"Image Generation"</span>) <span class="hljs-comment">#page title</span>

col1, col2 = st.columns(<span class="hljs-number">2</span>) <span class="hljs-comment">#create 2 columns</span>

<span class="hljs-keyword">with</span> col1: <span class="hljs-comment">#everything in this with block will be placed in column 1</span>
    st.subheader(<span class="hljs-string">"Image generation prompt"</span>) <span class="hljs-comment">#subhead for this column</span>

    prompt_text = st.text_area(<span class="hljs-string">"Prompt text"</span>, height=<span class="hljs-number">200</span>, label_visibility=<span class="hljs-string">"collapsed"</span>) <span class="hljs-comment">#display a multiline text box with no label</span>

    process_button = st.button(<span class="hljs-string">"Run"</span>, type=<span class="hljs-string">"primary"</span>) <span class="hljs-comment">#display a primary button</span>

<span class="hljs-keyword">with</span> col2: <span class="hljs-comment">#everything in this with block will be placed in column 2</span>
    st.subheader(<span class="hljs-string">"Result"</span>) <span class="hljs-comment">#subhead for this column</span>

    <span class="hljs-keyword">if</span> process_button: <span class="hljs-comment">#code in this if block will be run when the button is clicked</span>
        <span class="hljs-keyword">with</span> st.spinner(<span class="hljs-string">"Drawing..."</span>): <span class="hljs-comment">#show a spinner while the code in this with block runs</span>
            generated_image = glib.get_image_response(prompt_content=prompt_text) <span class="hljs-comment">#call the model through the supporting library</span>

        st.image(generated_image) <span class="hljs-comment">#display the generated image</span>
</code></pre>
<h3 id="heading-step-3-run-the-application">Step 3 - Run the application</h3>
<p>Open the terminal and paste the below command. A server will be running at port 8080. You can access the application by visiting the url - <strong>http://localhost:8080</strong></p>
<pre><code class="lang-bash">streamlit run image_app.py --server.port 8080
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, creating an image generation app with AWS Bedrock in just three simple steps is both efficient and powerful.</p>
<p>By integrating LangChain, streamlining processes with Boto3, and leveraging the flexibility of Python, you can harness the full potential of GenAI. This approach not only simplifies development but also ensures robust and scalable solutions for your image generation needs.</p>
<p>Start building today and experience the transformative power of these cutting-edge technologies.</p>
]]></content:encoded></item><item><title><![CDATA[AWS Bedrock: How to create a chat app using Boto3 Library?]]></title><description><![CDATA[Hello guys, in this blog we will learn how can we create a simple chat application using AWS Bedrock and the AWS Boto3 python library.
We will be creating a simple script which will take the prompt provided by the user as an input and will provide wi...]]></description><link>https://blog.cloudgags.in/aws-bedrock-how-to-create-a-chat-app-using-boto3-library</link><guid isPermaLink="true">https://blog.cloudgags.in/aws-bedrock-how-to-create-a-chat-app-using-boto3-library</guid><category><![CDATA[AWS]]></category><category><![CDATA[AWS Bedrock]]></category><category><![CDATA[genai]]></category><category><![CDATA[boto3]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 08 Jun 2024 13:14:02 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1717852105260/c1ebcec6-6e06-4f1c-acf9-1f0a03e8692b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello guys, in this blog we will learn how can we create a simple chat application using AWS Bedrock and the AWS Boto3 python library.</p>
<p>We will be creating a simple script which will take the prompt provided by the user as an input and will provide with relavant output.</p>
<p>By following along with the blog you will also learn how you can make a basic API call directly to the AWS Bedrock service.</p>
<h2 id="heading-prerequisite">Prerequisite</h2>
<p>In this example we will be using the AWS Bedrock service along with the <strong>models</strong> provided by the <strong>AI21 Labs</strong> So before getting started with the example you must request access for the <strong>AI21 Labs</strong> models.</p>
<p>Also since the bedrock service is not available in all the AWS regions we will be using the <strong>us-east-1</strong> region for this example.</p>
<h3 id="heading-action-items">Action Items</h3>
<ol>
<li><p>Access for the <strong>AI21 Labs</strong> models in the us-east-1 region.</p>
</li>
<li><p>Generate and configure access keys to execute the below python script. You will need the Bedrock service access.</p>
</li>
</ol>
<h2 id="heading-lets-get-the-python-script-ready">Lets get the python script ready</h2>
<ol>
<li>First install the AWS Boto3 library</li>
</ol>
<pre><code class="lang-python">pip3 install boto3
</code></pre>
<ol start="2">
<li><p>First we will add the import statement.</p>
<p> These statements allow us to use the AWS Boto3 library to call Amazon Bedrock.</p>
</li>
</ol>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> boto3
</code></pre>
<ol start="3">
<li>Initialize the Bedrock client library.</li>
</ol>
<pre><code class="lang-python">session = boto3.Session()
<span class="hljs-comment">#creates a Bedrock client</span>
bedrock = session.client(service_name=<span class="hljs-string">'bedrock-runtime'</span>)
</code></pre>
<ol start="4">
<li><p>Build the payload for the API call.</p>
<p> Here we are identifying the model to use, the prompt, and the inference parameters for the specified model.</p>
</li>
</ol>
<pre><code class="lang-python">bedrock_model_id = <span class="hljs-string">"ai21.j2-ultra-v1"</span> <span class="hljs-comment">#set the foundation model</span>

prompt = <span class="hljs-string">"Which is the largest city in India?"</span> <span class="hljs-comment">#the prompt to send to the model</span>

 <span class="hljs-comment">#build the request payload</span>
body = json.dumps({
    <span class="hljs-string">"prompt"</span>: prompt, <span class="hljs-comment">#AI21</span>
    <span class="hljs-string">"maxTokens"</span>: <span class="hljs-number">1024</span>, 
    <span class="hljs-string">"temperature"</span>: <span class="hljs-number">0</span>, 
    <span class="hljs-string">"topP"</span>: <span class="hljs-number">0.5</span>, 
    <span class="hljs-string">"stopSequences"</span>: [], 
    <span class="hljs-string">"countPenalty"</span>: {<span class="hljs-string">"scale"</span>: <span class="hljs-number">0</span> }, 
    <span class="hljs-string">"presencePenalty"</span>: {<span class="hljs-string">"scale"</span>: <span class="hljs-number">0</span> }, 
    <span class="hljs-string">"frequencyPenalty"</span>: {<span class="hljs-string">"scale"</span>: <span class="hljs-number">0</span> }
})
</code></pre>
<ol start="5">
<li><p>Call the Bedrock API.</p>
<p> We use Bedrock's invoke_model function to make the call.</p>
</li>
</ol>
<pre><code class="lang-python">response = bedrock.invoke_model(body=body, modelId=bedrock_model_id, accept=<span class="hljs-string">'application/json'</span>, contentType=<span class="hljs-string">'application/json'</span>) <span class="hljs-comment">#send the payload to Bedrock</span>
</code></pre>
<ol start="6">
<li><p>Display the response.</p>
<p> This extracts &amp; prints the returned text from the model's response JSON.</p>
</li>
</ol>
<pre><code class="lang-python">response_body = json.loads(response.get(<span class="hljs-string">'body'</span>).read()) <span class="hljs-comment"># read the response</span>

response_text = response_body.get(<span class="hljs-string">"completions"</span>)[<span class="hljs-number">0</span>].get(<span class="hljs-string">"data"</span>).get(<span class="hljs-string">"text"</span>) <span class="hljs-comment">#extract the text from the JSON response</span>

print(response_text)
</code></pre>
<ol start="7">
<li><p>Save the file. Great! Now you are ready to run the script!</p>
</li>
<li><p>Run the script using the below command</p>
</li>
</ol>
<pre><code class="lang-python">python3 bedrock_api.py
</code></pre>
<h2 id="heading-finally-here-is-the-code-for-complete-script">Finally Here is the code for complete script</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> boto3

session = boto3.Session()
bedrock = session.client(service_name=<span class="hljs-string">'bedrock-runtime'</span>) <span class="hljs-comment">#creates a Bedrock client</span>

bedrock_model_id = <span class="hljs-string">"ai21.j2-ultra-v1"</span> <span class="hljs-comment">#set the foundation model</span>

prompt = <span class="hljs-string">"What is the largest city in New Hampshire?"</span> <span class="hljs-comment">#the prompt to send to the model</span>

body = json.dumps({
    <span class="hljs-string">"prompt"</span>: prompt, <span class="hljs-comment">#AI21</span>
    <span class="hljs-string">"maxTokens"</span>: <span class="hljs-number">1024</span>, 
    <span class="hljs-string">"temperature"</span>: <span class="hljs-number">0</span>, 
    <span class="hljs-string">"topP"</span>: <span class="hljs-number">0.5</span>, 
    <span class="hljs-string">"stopSequences"</span>: [], 
    <span class="hljs-string">"countPenalty"</span>: {<span class="hljs-string">"scale"</span>: <span class="hljs-number">0</span> }, 
    <span class="hljs-string">"presencePenalty"</span>: {<span class="hljs-string">"scale"</span>: <span class="hljs-number">0</span> }, 
    <span class="hljs-string">"frequencyPenalty"</span>: {<span class="hljs-string">"scale"</span>: <span class="hljs-number">0</span> }
}) <span class="hljs-comment">#build the request payload</span>


response = bedrock.invoke_model(body=body, modelId=bedrock_model_id, accept=<span class="hljs-string">'application/json'</span>, contentType=<span class="hljs-string">'application/json'</span>) <span class="hljs-comment">#send the payload to Bedrock</span>

response_body = json.loads(response.get(<span class="hljs-string">'body'</span>).read()) <span class="hljs-comment"># read the response</span>

response_text = response_body.get(<span class="hljs-string">"completions"</span>)[<span class="hljs-number">0</span>].get(<span class="hljs-string">"data"</span>).get(<span class="hljs-string">"text"</span>) <span class="hljs-comment">#extract the text from the JSON response</span>

print(response_text)
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, creating a chat app using AWS Bedrock and the Boto3 library is a streamlined process that leverages the robust capabilities of AWS's AI and ML services.</p>
<p>By following the steps outlined in this blog, you can efficiently set up and deploy a chat application using Python that harnesses the power of GenAI for intelligent interactions. AWS Bedrock provides a flexible and scalable foundation, while Boto3 simplifies the integration and management of AWS services.</p>
<p>Whether you're a seasoned developer or new to cloud-based AI, this combination offers a powerful solution for building sophisticated chat applications. Explore further possibilities and continue innovating with AWS Bedrock, Boto3, Python, and GenAI to enhance your app's functionality and user experience.</p>
]]></content:encoded></item><item><title><![CDATA[Inserting Data into AWS DynamoDB using AWS Lambda and Boto3]]></title><description><![CDATA[Introduction
Are you looking to efficiently insert data into AWS DynamoDB? In this comprehensive blog post, we will guide you step-by-step on how to integrate AWS Lambda and Boto3 library to streamline data insertion processes. Discover essential tip...]]></description><link>https://blog.cloudgags.in/inserting-data-into-aws-dynamodb-using-aws-lambda-and-boto3</link><guid isPermaLink="true">https://blog.cloudgags.in/inserting-data-into-aws-dynamodb-using-aws-lambda-and-boto3</guid><category><![CDATA[AWS]]></category><category><![CDATA[aws lambda]]></category><category><![CDATA[AWS DynamoDB]]></category><category><![CDATA[DynamoDB]]></category><category><![CDATA[boto3]]></category><category><![CDATA[Python]]></category><category><![CDATA[Series]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Fri, 24 May 2024 08:35:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1716529760878/55aa380d-9ece-41ee-b685-1ee6d986d991.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>Are you looking to efficiently insert data into AWS DynamoDB? In this comprehensive blog post, we will guide you step-by-step on how to integrate AWS Lambda and Boto3 library to streamline data insertion processes. Discover essential tips and best practices for optimizing database management and leveraging cloud services for peak performance. Get ready to unlock the power of AWS DynamoDB and take your data management to the next level. So lets get started!</p>
<p><strong>NOTE: We will be using the ap-south-1 region for this tutorial, but you can use any region. Just make sure you create the lambda function and Dynamodb table in the same region.</strong></p>
<h2 id="heading-what-we-will-create">What we will create?</h2>
<p>To understand more clearly we will create a Todo list-taking application. For creating a to-do task we will save the <strong>id, name, and description</strong> of the task. We will be using "id" as the partition key, "name" as sort key while configuring the schema.</p>
<h2 id="heading-creating-lambda-function">Creating lambda function</h2>
<p>To create a lambda function</p>
<ol>
<li><p>Open the lambda service.</p>
</li>
<li><p>Click on the Create function button.</p>
</li>
<li><p>Provide a name for the lambda function.</p>
</li>
<li><p>Select the runtime for the function.</p>
</li>
<li><p>Select the architecture.</p>
</li>
<li><p>Select a role to provide permission to the function.</p>
</li>
<li><p>Leave the advanced settings as it is.</p>
</li>
<li><p>Click on "Create function" button.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1716457576454/28f98e06-88d5-4133-b620-baaa097755fc.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-permissions-to-the-lambda-function">Permissions to the lambda function</h2>
<p><strong>The role which you are attaching to the lambda function must contain the following policy. This policy allows the lambda function to perform certain operations on the DynamoDB table.</strong></p>
<ul>
<li><strong>AmazonDynamoDBFullAccess</strong></li>
</ul>
<h2 id="heading-creating-a-dynamodb-table">Creating a DynamoDB table</h2>
<p><strong>To create a DynamoDB table</strong></p>
<ol>
<li><p>Open the DynamoDB service.</p>
</li>
<li><p>From the available on the left click on "<strong>Tables</strong>".</p>
</li>
<li><p>Then click on the "<strong>Create Table</strong>" button on the right.</p>
</li>
<li><p>Provide a table name.</p>
</li>
<li><p>Enter the partition key as "<strong>id</strong>" with datatype as <strong>number</strong>.</p>
</li>
<li><p>Enter the sort key as "name" with datatype as <strong>string</strong>.</p>
</li>
<li><p>Select the <strong>customized settings</strong> option.</p>
</li>
<li><p>Leave the table class as the default.</p>
</li>
<li><p>Select the <strong>on-demand</strong> option for Read/write capacity settings.</p>
</li>
<li><p>Leave the rest of the settings as it is.</p>
</li>
<li><p>Click on the "<strong>Create Table</strong>" button.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1716461017065/d57b450f-4f4f-40e1-be52-4ba331e97fa2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1716461045396/34d1f7f9-0958-4505-b5fb-37bcaecc1fbf.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1716461068725/63bf6925-e36e-42a2-a2ae-c4b8be4b3bdc.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-writing-lambda-code">Writing Lambda code</h2>
<p>The following lambda code inserts an item in the specified DynamoDB table and return success message if the operation is successful.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> boto3

<span class="hljs-comment"># INPUT</span>
DB_REGION = <span class="hljs-string">'ap-south-1'</span>
DB_TABLE_NAME = <span class="hljs-string">'todo-app-db'</span>

ITEM = {
    <span class="hljs-string">'id'</span>: <span class="hljs-number">1</span>,  <span class="hljs-comment"># number</span>
    <span class="hljs-string">'name'</span>: <span class="hljs-string">'My First todo'</span>,  <span class="hljs-comment"># string</span>
    <span class="hljs-string">'description'</span>: <span class="hljs-string">'This is a sample item'</span>  <span class="hljs-comment"># string</span>
}

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_handler</span>(<span class="hljs-params">event, context</span>):</span>
    dynamodb = boto3.resource(<span class="hljs-string">'dynamodb'</span>, region_name=DB_REGION)
    table = dynamodb.Table(DB_TABLE_NAME)
    response = table.put_item(Item=ITEM)

    <span class="hljs-keyword">if</span> response[<span class="hljs-string">'ResponseMetadata'</span>][<span class="hljs-string">'HTTPStatusCode'</span>]==<span class="hljs-number">200</span>:
        <span class="hljs-keyword">return</span> {<span class="hljs-string">"message"</span>:<span class="hljs-string">"Item inserted successfully!"</span>}
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> {<span class="hljs-string">"message"</span>:<span class="hljs-string">"Operation Failes!"</span>}
</code></pre>
<h2 id="heading-key-points-to-keep-in-mind-when-working-with-dynamodb">Key points to keep in mind when working with Dynamodb</h2>
<ol>
<li><p>You can't skip the partition key when defining the DynamoDB table schema. Your partition key must contain unique values.</p>
</li>
<li><p>Always try to configure a sort key in the schema. Sort key helps to improve the data indexing which directly improves your data search in the table.</p>
</li>
<li><p>Turn on the deletion protect when creating a DynamoDB table.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, we have explored the seamless process of inserting data into AWS DynamoDB using AWS Lambda and Boto3. By following the step-by-step guide and leveraging best practices, you can efficiently manage your database and optimize performance.</p>
<p>The integration of AWS Lambda and Boto3 allows for streamlined data insertion processes, eliminating the need for manual operations and enhancing scalability. With the power of serverless computing and AWS cloud services, you can take full control of your data management, ensuring seamless operations and unlocking new possibilities for your applications. Keep experimenting and exploring the vast capabilities of AWS DynamoDB to harness the true potential of your data.</p>
<h2 id="heading-resource">Resource</h2>
<p>Follow the complete series to learn how to perform CRUD operations on AWS DynamoDB using AWS Lambda - <a target="_blank" href="https://blog.cloudgags.in/series/dynamodb-with-aws-lambda">Click Here</a></p>
]]></content:encoded></item><item><title><![CDATA[Introduction to DynamoDB and AWS Lambda]]></title><description><![CDATA[Introduction
In the rapidly evolving world of cloud computing, Amazon Web Services (AWS) offers a plethora of tools designed to help businesses scale and manage their applications efficiently. Two of these powerful tools are DynamoDB and AWS Lambda. ...]]></description><link>https://blog.cloudgags.in/introduction-to-dynamodb-and-aws-lambda</link><guid isPermaLink="true">https://blog.cloudgags.in/introduction-to-dynamodb-and-aws-lambda</guid><category><![CDATA[AWS]]></category><category><![CDATA[aws lambda]]></category><category><![CDATA[AWS DynamoDB]]></category><category><![CDATA[lambda]]></category><category><![CDATA[DynamoDB]]></category><category><![CDATA[Series]]></category><category><![CDATA[boto3]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Mon, 20 May 2024 17:24:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1716281048554/dbd81db7-b390-4f90-9b4b-9b4d1ca9ca1c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In the rapidly evolving world of cloud computing, Amazon Web Services (AWS) offers a plethora of tools designed to help businesses scale and manage their applications efficiently. Two of these powerful tools are <strong>DynamoDB</strong> and <strong>AWS Lambda</strong>. Whether you're new to cloud computing or looking to optimize your existing architecture, understanding these services is crucial.</p>
<p>This blog will provide a concise introduction to DynamoDB and AWS Lambda, explaining their core functionalities and how they can be integrated for seamless, serverless applications.</p>
<h2 id="heading-what-is-dynamodb">What is DynamoDB?</h2>
<p><strong>Amazon DynamoDB</strong> is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. Here are some key features.</p>
<ol>
<li><p><strong>Fully Managed</strong>: DynamoDB handles all the administrative tasks such as hardware provisioning, setup, configuration, replication, software patching, and cluster scaling.</p>
</li>
<li><p><strong>Performance</strong>: It delivers single-digit millisecond response times at any scale, making it ideal for high-traffic applications.</p>
</li>
<li><p><strong>Scalability</strong>: With DynamoDB, you can start small and scale up to millions of requests per second without any downtime.</p>
</li>
<li><p><strong>Flexible Data Model</strong>: It supports both key-value and document data models, allowing for a flexible schema design.</p>
</li>
</ol>
<h3 id="heading-benefits-of-using-dynamodb">Benefits of Using DynamoDB</h3>
<ol>
<li><p><strong>High Availability and Durability</strong>: Data is automatically replicated across multiple AWS regions.</p>
</li>
<li><p><strong>Security</strong>: Integrated with AWS Identity and Access Management (IAM) for fine-grained access control.</p>
</li>
<li><p><strong>Cost-Effective</strong>: Pay only for the throughput and storage you need.</p>
</li>
</ol>
<h2 id="heading-what-is-aws-lambda">What is AWS Lambda?</h2>
<p><strong>AWS Lambda</strong> is a serverless compute service that lets you run code without provisioning or managing servers. Here are some notable features.</p>
<ol>
<li><p><strong>Serverless</strong>: Automatically scales your application by running code in response to each trigger.</p>
</li>
<li><p><strong>Cost-Effective</strong>: You only pay for the compute time you consume – there’s no charge when your code isn’t running.</p>
</li>
<li><p><strong>Event-Driven</strong>: Easily integrates with other AWS services to trigger functions in response to events such as changes to data in an Amazon S3 bucket or an update in a DynamoDB table.</p>
</li>
</ol>
<h3 id="heading-benefits-of-using-aws-lambda">Benefits of Using AWS Lambda</h3>
<ol>
<li><p><strong>Automatic Scaling</strong>: Scales precisely with the size of the workload.</p>
</li>
<li><p><strong>Simplified Management</strong>: No need to manage the infrastructure; focus solely on your code.</p>
</li>
<li><p><strong>Flexible Invocation</strong>: Can be triggered by various AWS services, HTTP endpoints, or custom applications.</p>
</li>
</ol>
<h2 id="heading-integrating-dynamodb-with-aws-lambda">Integrating DynamoDB with AWS Lambda</h2>
<p>Integrating DynamoDB with AWS Lambda can create powerful serverless applications. Here’s a simple example of how they can work together:</p>
<ol>
<li><p><strong>Event Source</strong>: Configure DynamoDB Streams to capture item-level changes in a DynamoDB table.</p>
</li>
<li><p><strong>Trigger</strong>: Set up an AWS Lambda function to process these stream records. For instance, every time an item is added, updated, or deleted in the DynamoDB table, the Lambda function is triggered.</p>
</li>
<li><p><strong>Process Data</strong>: The Lambda function can then execute code to process the changes. This might include updating other databases, sending notifications, or performing analytics.</p>
</li>
</ol>
<h3 id="heading-example-use-case">Example Use Case</h3>
<p>Imagine you have an e-commerce application. When a new order is placed, it’s added to a DynamoDB table. DynamoDB Streams capture this event and trigger a Lambda function that processes the order – perhaps updating inventory, sending a confirmation email, and logging the transaction.</p>
<h2 id="heading-advantage-of-using-aws-lambda-with-dynamodb">Advantage of using AWS Lambda with DynamoDB</h2>
<ol>
<li><p>AWS Lambda seamlessly interacts and integrates with DynamoDB using the Boto3 library.</p>
</li>
<li><p>Using DynamoDB streams and AWS Lambda you can build out of the box solutions.</p>
</li>
<li><p>You can also perform database operations on AWS Cloudfront using lambda@edge functions.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Combining DynamoDB and AWS Lambda offers a robust, scalable, and cost-effective solution for modern application development. DynamoDB provides the reliable and high-performance database backend, while AWS Lambda enables you to run code in response to data changes without the overhead of managing servers.</p>
<p>By leveraging these AWS services, you can build responsive, event-driven applications that scale seamlessly with your business needs. Start exploring DynamoDB and AWS Lambda today, and unlock the full potential of serverless computing.</p>
<p>For more information, check out the official <a target="_blank" href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">DynamoDB documentation</a> and <a target="_blank" href="https://docs.aws.amazon.com/lambda/latest/dg/welcome.html">AWS Lambda documentation</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding CloudFront Functions: A Comprehensive Guide]]></title><description><![CDATA[Introduction
AWS CloudFront functions as a content delivery network, mitigating network latency between users and the origin server to efficiently serve media. However, users may have specific requirements, such as implementing business logic on requ...]]></description><link>https://blog.cloudgags.in/understanding-cloudfront-functions-a-comprehensive-guide</link><guid isPermaLink="true">https://blog.cloudgags.in/understanding-cloudfront-functions-a-comprehensive-guide</guid><category><![CDATA[AWS]]></category><category><![CDATA[aws cloudfront]]></category><category><![CDATA[CloudFront Functions]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Tue, 14 May 2024 08:16:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/gnyA8vd3Otc/upload/cb976acc598d8449cc4c162a5b2cb60d.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>AWS CloudFront functions as a content delivery network, mitigating network latency between users and the origin server to efficiently serve media. However, users may have specific requirements, such as implementing business logic on requests or redirecting certain requests to alternative locations. Is CloudFront equipped to handle these scenarios?</p>
<p>The answer is affirmative. AWS CloudFront incorporates advanced features, and one such capability is AWS CloudFront Functions.</p>
<p>CloudFront functions are designed to empower CloudFront users with the ability to apply customized business logic to the ongoing requests processed by AWS CloudFront. These lightweight functions are adept at executing small, efficient tasks at the edge locations during the processing of active requests. Okay, now we know what Cloudfront functions are but to understand more let's look at some use cases.</p>
<h2 id="heading-when-to-use-cloudfront-functions"><strong>When to use CloudFront functions?</strong></h2>
<p>Let's consider a scenario where I own a blogging website, and within my CloudFront distribution, there exists a behavior entry designated as "/aws." This configuration dictates that any request directed to CloudFront with a path matching "/aws" should be forwarded to the S3 bucket configured for hosting all blogs related to AWS. Now, suppose I encounter a situation where I need to redirect all incoming requests targeting the "/aws" path to a new origin, but, at the moment, I prefer not to modify the existing origin configured in CloudFront. How can I accomplish this task?</p>
<p>In this situation, leveraging the capabilities of CloudFront Functions proves beneficial. By creating a CloudFront function, I can establish a condition within the function specifying that whenever the requested URL path begins with "/aws," the function should generate a 302 redirection response. This approach ensures that users are seamlessly redirected to the new content without necessitating alterations to the existing origin configuration on CloudFront.</p>
<h2 id="heading-what-cloudfront-functions-can-do"><strong>What CloudFront Functions can do?</strong></h2>
<p><strong>1. Modify incoming HTTP request</strong></p>
<p>With CloudFront functions, you can perform request modifications like sending a 302 redirection or changing the origin based on certain logic you can execute modifications to requests, such as initiating a 302 redirection or adjusting the origin by specific logical criteria.</p>
<p><strong>2. Modify incoming HTTP response</strong></p>
<p>You also can alter the outgoing request by adjusting security headers or dispatching a customized response.</p>
<h2 id="heading-charges-for-using-the-cloudfront-function"><strong>Charges for using the CloudFront function?</strong></h2>
<p>As AWS adheres to the pay-as-you-go model, the cost structure for CloudFront Functions is likewise based on usage. Users incur charges by the number of function executions. Given the lightweight nature of CloudFront Functions in comparison to Lambda@Edge, the associated costs are correspondingly lower.</p>
<p>It is advisable to consult the official AWS documentation for a comprehensive understanding of the cost model, as these charges are governed by AWS.</p>
<p>Here are the AWS docs mentioning the CloudFront function charges - <a target="_blank" href="https://aws.amazon.com/cloudfront/pricing/">Docs</a></p>
<p>You can also calculate the tentative cost using the <a target="_blank" href="https://calculator.aws/#/">AWS calculator</a></p>
<h2 id="heading-limitations-of-cloudfront-function"><strong>Limitations of CloudFront function</strong></h2>
<p>So, now we know about CloudFront function but until and unless we don't know its limitations our information is incomplete. So below are the limitations of the CloudFront function.</p>
<p><strong>1. Language Support</strong></p>
<p>As of now, CloudFront functions exclusively support JavaScript. If you are familiar with Lambda@Edge, you may be aware of the language options it provides. However, it's essential to note that AWS CloudFront functions currently offer only JavaScript as the language option.</p>
<p><strong>2. No external library support</strong></p>
<p>CloudFront functions do not permit the inclusion of external libraries in your code. It is necessary to develop your code utilizing the libraries offered by AWS. While this design choice is intended to maintain the lightweight nature of the function, it can also be considered a limitation. If there is a requirement for importing external libraries, one would need to resort to using Lambda@Edge functions instead.</p>
<p><strong>3. Only two event association</strong></p>
<p>In contrast to Lambda@Edge, CloudFront functions are limited to association with two events: viewer request and viewer response. Lambda@Edge, on the other hand, offers broader association capabilities, allowing attachment to four events: viewer request, viewer response, origin request, and origin response.</p>
<p><strong>4. Lightweight</strong></p>
<p>While the lightweight nature of CloudFront functions is advantageous, it also imposes limitations. CloudFront functions are primarily suitable for simple operations. In contrast, utilizing Lambda@Edge provides the flexibility to execute more intricate logic, including database operations.</p>
<h2 id="heading-similar-topics">Similar Topics</h2>
<p>Learn how to perform redirections using cloudfront function - <a target="_blank" href="https://blog.cloudgags.in/3xx-redirection-using-cloudfront-function">Blog</a></p>
<p>Understand how AWS Cloudfront caches the content - <a target="_blank" href="https://blog.cloudgags.in/how-does-aws-cloudfront-cache-the-content">Blog</a></p>
]]></content:encoded></item><item><title><![CDATA[Free IPv4 on AWS: End of free tier]]></title><description><![CDATA[Introduction
Free IPv4 on AWS is no longer free. Yes, you heard it right. AWS has officially announced that starting from February 1, 2024, there will be charges for using IPv4 addresses on the AWS cloud. The charges are $0.005 per IP per hour for al...]]></description><link>https://blog.cloudgags.in/free-ipv4-on-aws-end-of-free-tier</link><guid isPermaLink="true">https://blog.cloudgags.in/free-ipv4-on-aws-end-of-free-tier</guid><category><![CDATA[aws news]]></category><category><![CDATA[AWS]]></category><category><![CDATA[aws billing]]></category><category><![CDATA[IPv4]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 11 May 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/SYTO3xs06fU/upload/1592c8a4d4ae6cacbc779e296d7131b6.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Free IPv4 on AWS is no longer free. Yes, you heard it right. AWS has officially announced that starting from February 1, 2024, there will be charges for using IPv4 addresses on the AWS cloud. The charges are $0.005 per IP per hour for all public IPv4 addresses. It is no longer necessary for the IPv4 address to be attached to any services. Whether in use or not, AWS will charge you for the use of any IPv4 address.</p>
<h2 id="heading-what-is-an-ipv4-address"><strong>What is an IPv4 address?</strong></h2>
<p>The IPv4 address serves as a unique identifier for devices connected to the extensive internet network. It facilitates smooth communication between devices by exchanging data packets. For example, a standard IPv4 address could be 192.168.1.1. Each time you communicate on the internet, such an address is assigned to every device.</p>
<h2 id="heading-reason-behind-the-change"><strong>Reason Behind the Change</strong></h2>
<p>To understand why AWS made this change, let's look at the problem they're dealing with. Back in the 1980s, when IPv4 addresses were first created, they didn't anticipate the internet's massive growth. Now, with so many devices online, there aren't enough IPv4 addresses to go around. This has led to the exhaustion of IPv4 addresses.</p>
<h2 id="heading-charges-defined-by-aws"><strong>Charges defined by AWS</strong></h2>
<p><a target="_blank" href="https://aws.amazon.com/blogs/aws/new-aws-public-ipv4-address-charge-public-ip-insights/"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715759400490/5b112cee-925c-4834-b4ba-737b1ff92815.png" alt class="image--center mx-auto" /></a></p>
<h2 id="heading-how-to-monitor-ip-use-on-aws">How to monitor IP use on AWS?</h2>
<p>It's difficult to track every IP address on the AWS cloud but due to the end of the IPv4 free tier, it has become a ne<a target="_blank" href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html">cessity. To overcome this problem AWS has l</a>aunched <strong>Public IP Insights</strong>. It's a new feature addon <a target="_blank" href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html">for the <strong>Amazon VPC IP Address Manager</strong> se</a>rvice. This add-on h<a target="_blank" href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html">elps the AWS users to manage</a> and track all the IP addresses they have been using on AWS. Right now this addon/feature is completely free and includes no additional costs.</p>
<h2 id="heading-summary">Summary</h2>
<p>As AWS transitions away from free IPv4 usage, it's essential to adapt to the new landscape. Embrace this change as an opportunity to optimize resource allocation and streamline your AWS operations. With a clearer understanding of IPv4 addresses and the reasons behind AWS's decision, you're better equipped to navigate this shift in the cloud computing landscape.</p>
<h2 id="heading-resources">Resources</h2>
<p>Official AWS announcement blog - <a target="_blank" href="https://aws.amazon.com/blogs/aws/new-aws-public-ipv4-address-charge-public-ip-insights/">Article</a></p>
]]></content:encoded></item><item><title><![CDATA[Easy Guide to Setting Up AWS Cloudfront Error Pages]]></title><description><![CDATA[Introduction
Imagine you visit a website delivered through AWS CloudFront. If something goes wrong on the server behind the scenes, you might see a confusing error message on your screen.
Such default error messages have the potential to impede user ...]]></description><link>https://blog.cloudgags.in/easy-guide-to-setting-up-cloudfront-error-pages</link><guid isPermaLink="true">https://blog.cloudgags.in/easy-guide-to-setting-up-cloudfront-error-pages</guid><category><![CDATA[cloudfront error pages]]></category><category><![CDATA[AWS]]></category><category><![CDATA[cloudfront]]></category><category><![CDATA[aws cloudfront]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Tue, 07 May 2024 06:03:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/sxiSod0tyYQ/upload/f0a31f37754eef556a2f129791c40293.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>Imagine you visit a website delivered through AWS CloudFront. If something goes wrong on the server behind the scenes, you might see a confusing error message on your screen.</p>
<p>Such default error messages have the potential to impede user experience, ultimately affecting business outcomes. Providing customized content in these scenarios would offer greater convenience.</p>
<p>Enter AWS CloudFront Error Pages! These allow you to craft personalized messages for users, elucidating the problem and possibly providing guidance. This ensures your visitors aren't left in a state of confusion. It's an effective method for enhancing your website's user experience!</p>
<h2 id="heading-how-do-cloudfront-error-pages-function"><strong>How do CloudFront Error Pages function?</strong></h2>
<p>To utilize AWS CloudFront Error Pages, you first need to determine which error code you want to trigger an error page. The CloudFront Error Pages operate based on error codes.</p>
<p>For instance:</p>
<p>Let's say a user is attempting to access certain content and receives a 404 error code as a response. In this case, we recognize that when a 404 error code is received, we should display a customized response to the user.</p>
<p>Therefore, we can configure the system so that when a 404 error occurs, a tailored response is presented to the user, ensuring the continuity of their experience and the integrity of the application.</p>
<h2 id="heading-benefits-of-cloudfront-error-pages"><strong>Benefits of CloudFront Error Pages</strong></h2>
<ol>
<li><strong>Improved User Experience</strong></li>
</ol>
<p>Custom error pages enhance the user's experience by delivering personalized responses when errors occur.</p>
<ol start="2">
<li><strong>Preserved Application Integrity</strong></li>
</ol>
<p>Custom responses shield users from the inner workings of the application, maintaining its integrity.</p>
<ol start="3">
<li><strong>Error Response Caching Capability</strong></li>
</ol>
<p>CloudFront error pages enable caching of error responses, reducing the burden on application servers.</p>
<h2 id="heading-how-to-configure-cloudfront-error-pages"><strong>How to configure Cloudfront Error Pages?</strong></h2>
<ol>
<li><p>Go to the error pages tab for the respective Cloudfront distribution.</p>
</li>
<li><p>Click on the "<strong>Create custom error response</strong>" button</p>
</li>
<li><p>Select the <strong>error code</strong> for which you want to send the custom response.</p>
</li>
<li><p>Set the <strong>caching period</strong>. Note that the period is in seconds</p>
</li>
<li><p>Select whether you want to send customized response or not by switching between "<strong>Yes</strong>" or "<strong>No</strong>".</p>
</li>
<li><p>If you opt for a customized response then provide the <strong>path</strong> of the page which should be served as the custom response.</p>
</li>
<li><p>Select the <strong>new status code</strong> you need to send along with this new request.</p>
</li>
<li><p>Lastly, click on the "<strong>Create custom error response</strong>" button</p>
</li>
</ol>
<p><strong>Example</strong></p>
<p>In the below screenshot, we have considered a case where we need to send a customized response for all the 404 errors. We have configured the caching period for 10 seconds and a 404.html response page.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715061315110/e6e466ea-dbc8-4178-90d6-407aefac4045.png" alt="AWS Cloudfront Error Pages Screenshot" class="image--center mx-auto" /></p>
<h2 id="heading-summary"><strong>Summary</strong></h2>
<p>CloudFront Error Pages are a crucial aspect of managing and optimizing web content delivery through AWS CloudFront. In this comprehensive guide, we delve into the fundamentals of CloudFront Error Pages, exploring what they are, how they work, and why they matter for website owners and developers.</p>
<p>Whether you're new to AWS CloudFront or looking to optimize your web delivery strategy, this guide will provide you with valuable insights into leveraging CloudFront Error Pages effectively.</p>
]]></content:encoded></item><item><title><![CDATA[Simplifying 3xx Redirection with CloudFront Function]]></title><description><![CDATA[Introduction
Hey there, hope you're doing well! Ready to dive into today's blog about pulling off a 3xx redirection using a CloudFront function? Before we jump in, let's get on the same page about what exactly a 3xx redirection involves.
Quick heads ...]]></description><link>https://blog.cloudgags.in/3xx-redirection-using-cloudfront-function</link><guid isPermaLink="true">https://blog.cloudgags.in/3xx-redirection-using-cloudfront-function</guid><category><![CDATA[AWS]]></category><category><![CDATA[cloudfront]]></category><category><![CDATA[CloudFront Functions]]></category><category><![CDATA[aws cloudfront]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sun, 05 May 2024 14:58:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1714922489607/d52a7bca-cad0-4f08-adc2-9bc21e90b6fd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Hey there, hope you're doing well! Ready to dive into today's blog about pulling off a 3xx redirection using a CloudFront function? Before we jump in, let's get on the same page about what exactly a 3xx redirection involves.</p>
<p>Quick heads up: To get the most out of this blog, it's important to be familiar with CloudFront functions. If you're not up to speed on them, no worries – check out our blog dedicated to CloudFront functions first, and then come back to supercharge your understanding.</p>
<h2 id="heading-what-are-cloudfront-functions">What are Cloudfront functions?</h2>
<p>CloudFront functions are designed to empower CloudFront users to apply customized business logic to the ongoing requests processed by AWS CloudFront. These lightweight functions are adept at executing small, efficient tasks at the edge locations during the processing of active requests.</p>
<h2 id="heading-what-is-a-3xx-redirection">What is a 3xx redirection?</h2>
<p>Alright, let's break down 3xx redirects! When your browser gets a 3xx response, it's like a friendly note saying, "Hey, what you're looking for has a new address, go check it out over there!" These codes range from 300 to 308, and each number signifies a unique redirection type. Keep an eye out for 301 and 302 – they're the rockstars of daily redirection use! They're the ones you'll encounter the most in your web adventures.</p>
<h2 id="heading-different-types-of-3xx-redirects">Different types of 3xx redirects</h2>
<p><strong>1. 300 - Multiple Choices</strong></p>
<p>The web server can generate various responses to a request, as the requested resource is accessible in different formats.</p>
<p><strong>2. 301 - Moved Permanently</strong></p>
<p>The requested resource is no longer located at URL A; it has been permanently relocated to URL B. Moving forward, the resource can be accessed exclusively at URL B, rendering URL A obsolete. For comprehensive details on the 301 redirect, please refer to the provided information.</p>
<p><strong>3. 302 - Found (Temporarily displaced)</strong></p>
<p>The requested resource is present on the web server; however, it is temporarily inaccessible at URL A but can be accessed through URL B. The URL A remains valid during this temporary unavailability.</p>
<p><strong>4. 303 - See Other (open other storage location)</strong></p>
<p>The web server notifies the querying client that a distinct GET request must be sent to an alternative storage location to obtain the desired resource.</p>
<p><strong>5. 304 - Not Modified</strong></p>
<p>Since the most recent request from the client to the web server, the resource remains unchanged, and as a result, it will not be transmitted again.</p>
<p><strong>6. 305 - Use Proxy</strong></p>
<p>The web server notifies the client that access to the requested resource is only available through a proxy.</p>
<p><strong>7. 306 - (reserved)</strong></p>
<p>Previously, the directive was to modify the proxy. Currently, the status code is not actively employed but is reserved.</p>
<p><strong>8. 307 - Temporary Redirect</strong></p>
<p>(Comparable to 302/303) The requested resource is present on the web server but is temporarily inaccessible at URL A, instead located at URL B. The URL A remains valid. However, the browser should proceed using the same method as the original request (meaning a POST request is followed by another POST request). For in-depth details, refer to the information on the 307 redirect.</p>
<h2 id="heading-why-to-perform-3xx-redirection">Why to perform 3xx redirection?</h2>
<p>Imagine you're managing a bustling blog website with fresh content pouring in daily. Picture a scenario: you have a blog dedicated to AWS EC2 at the URL "www.somedummywebsite.com/ec2." Suddenly, a decision is made to revamp things, and the new address becomes "www.somedummywebsite.com/aws/ec2."</p>
<p>Now, think about the users who are accustomed to the old URL. If they try to access the blog using the previous address, they'd hit a dead end since the old URL is no longer valid. This is where the magic of redirects comes in.</p>
<p>To ensure a smooth transition and spare users from frustrating errors, the blog site employs a 3xx redirection on the old URL. Here's how it works: when users attempt to reach the blog through the outdated URL, their browser automatically redirects them to the updated address, "www.somedummywebsite.com/aws/ec2." The result? A seamless user experience, all thanks to this intelligent redirection strategy.</p>
<h2 id="heading-where-to-perform-redirection">Where to perform redirection?</h2>
<p>Great, let's dive into how we can set up a redirection using CloudFront functions. Now, for this to work, you'll need a CloudFront distribution in place. If you've explored CloudFront a bit, you'll know that it provides two slots where we can plug in our CloudFront function: one for the viewer request and another for the viewer response.</p>
<p>Let's break it down:</p>
<p><strong>1. Viewer Request</strong></p>
<p>If you associate the function with the viewer request, you'll be working with the incoming request data. This means you can inspect and manipulate the request before it reaches the origin server.</p>
<p>Your CloudFront function here will take the request data as input, and based on certain conditions, you can craft a custom response. This is handy if you want to perform logic before the request hits your server.</p>
<p><strong>2. Viewer Response</strong></p>
<p>Now, if you decide to associate the function with the viewer response, you're dealing with the outgoing response data. This is after CloudFront has received the response from your origin server.</p>
<p>In this scenario, your CloudFront function takes the response data as input. Again, you can apply custom logic, maybe modifying headers or response content, before it reaches the end user.</p>
<p>So, it's like choosing between tweaking the request before it heads to the server or fine-tuning the response before it reaches your audience. Depending on your use case, you'll pick either the viewer request or viewer response slot for your CloudFront function. Exciting, right?</p>
<h2 id="heading-redirection-code">Redirection Code</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handler</span>(<span class="hljs-params">event</span>)
</span>{
    <span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> This example function is for a viewer request event trigger.</span>
    <span class="hljs-comment">// Choose viewer request for event trigger when you associate this function with a distribution.</span>
    <span class="hljs-keyword">var</span> request = event.request;

    <span class="hljs-comment">// Set the desired destination URL for the 302 redirection</span>
    <span class="hljs-keyword">var</span> redirectUrl = <span class="hljs-string">"https://www.google.com/"</span>;

    <span class="hljs-comment">// Create a response with the 302 status code and the new location</span>
    <span class="hljs-keyword">var</span> response = {
            <span class="hljs-attr">statusCode</span>: <span class="hljs-number">301</span>,
            <span class="hljs-attr">statusDescription</span>: <span class="hljs-string">'Moved Permanently'</span>,
            <span class="hljs-attr">headers</span>:
                { <span class="hljs-string">"location"</span>: { <span class="hljs-string">"value"</span>: redirectUrl } }
            }



    <span class="hljs-comment">// Return the response to trigger the 302 redirection</span>
    <span class="hljs-keyword">return</span> response;

}
</code></pre>
<h2 id="heading-summary">Summary</h2>
<p>In this blog, we delved into the realm of CloudFront functions, exploring their significance. We unpacked the concept of redirections—understanding not just the "what" but also the "how" and "when" behind executing 3xx redirections. To bring these insights to life, we even walked through a practical demonstration.</p>
]]></content:encoded></item><item><title><![CDATA[Mastering AWS CloudFormation: A Comprehensive Overview]]></title><description><![CDATA[Introduction
AWS Cloudformation is an infrastructure as a code(IAAC) service provided by Amazon Web Service(AWS). It allows its users to provision infrastructure on AWS in an automated fashion by writing code for the infrastructure in a JSON/YAML fil...]]></description><link>https://blog.cloudgags.in/mastering-aws-cloudformation-a-comprehensive-overview</link><guid isPermaLink="true">https://blog.cloudgags.in/mastering-aws-cloudformation-a-comprehensive-overview</guid><category><![CDATA[AWS]]></category><category><![CDATA[cloudformation]]></category><category><![CDATA[AWS CloudFormation]]></category><category><![CDATA[#IaC]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sun, 05 May 2024 14:52:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1714920678286/aae79691-9035-49f0-a86f-64765fc556c1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>AWS Cloudformation is an infrastructure as a code(IAAC) service provided by Amazon Web Service(AWS). It allows its users to provision infrastructure on AWS in an automated fashion by writing code for the infrastructure in a JSON/YAML file.</p>
<h2 id="heading-is-aws-cloudformation-free">Is AWS Cloudformation free?</h2>
<p>Yes, AWS Cloudformation is a completely free service. But if cloudformation is a free service then how does the user get charged? The answer is, that the user gets charged for the usage of infrastructure/resources, created with the help of cloudformation.</p>
<p>Example</p>
<p>Let's say you have written a template and this template creates an ec2 instance. Now since you have created this instance using the cloudformation service, you will not get charged for using the cloudformation service but you will get charged for using the EC2 service. So in this way, AWS Cloudformation service is free.</p>
<h2 id="heading-why-should-i-use-aws-cloudformation">Why should I use AWS CloudFormation?</h2>
<p>So, AWS Cloudformation is a service used for creating infrastructure/resources on AWS in an automated way. To understand why to use cloudformation service let's take an example.</p>
<p>Example</p>
<p>Let's say you are working for a company and we have one customer. Every month the customer requests to launch an EC2 instance. As per the requirement, you go and launch the instance manually. The customer uses the instance for 10-12 days and then deletes/terminates the instance.</p>
<p>A few months later the customer requested that now instead of one instance he needs 100 instances. Now let's discuss the problems that you will face if you decide to do this work manually all on your own.</p>
<h2 id="heading-issues-when-not-using-aws-cloudformation">Issues when not using AWS Cloudformation</h2>
<ol>
<li><strong>Human Error</strong></li>
</ol>
<p>The very first problem that you will face is the chance of increased human errors. Since now the requirement is increased, it is possible that you can misconfigure the instances.</p>
<ol start="2">
<li><strong>Time-Consuming</strong></li>
</ol>
<p>Since now the requirement has increased you will need more time to fulfill the requirement.</p>
<ol start="3">
<li><strong>Inconsistency Across Environments</strong></li>
</ol>
<p>Deploying the infrastructure manually can result in inconsistency across different environments like dev, test, and prod.</p>
<ol start="4">
<li><strong>Lack of Version Control</strong></li>
</ol>
<p>When you deploy infrastructure manually it becomes very difficult to track the changes done to the infrastructure as you move ahead.</p>
<p>Now let's assume you are using cloudformation for deploying the 100 EC2 instances, as requested by the customer</p>
<h2 id="heading-advantages-of-using-aws-cloudformation">Advantages of using AWS Cloudformation</h2>
<ol>
<li><strong>Reduces human errors</strong></li>
</ol>
<p>Now since you are using AWS Cloudformation, you have already created a template with the required configurations and you just need to deploy the template. In this way, you just need to interact with the console once which reduces the chances of human errors.</p>
<ol start="2">
<li><strong>Time-saving</strong></li>
</ol>
<p>Since now you are creating the infrastructure using the preconfigured template, you can focus on your other work and let the template create the infrastructure/resources in your account as needed.</p>
<ol start="3">
<li><strong>Reusable Templates</strong></li>
</ol>
<p>You just need to create the cloudformation template once and then you can reuse the same template multiple times as you want, reducing the efforts required for creating the resources from scratch.</p>
<ol start="4">
<li><strong>Easy Rollback</strong></li>
</ol>
<p>If there is some configuration mismatch which updating the infrastructure, cloudformation will roll back to the previous stable state maintaining the availability of the infrastructure.</p>
<h2 id="heading-how-to-validate-the-aws-cloudformation-template">How to validate the AWS Cloudformation template?</h2>
<p>So you have created the template but validating the template before deployment is important. To validate the template there are two ways by which you can do the validation</p>
<ol>
<li><strong>The CLI option</strong></li>
</ol>
<p>The AWS CLI provides a command by which you can validate your cloudformation template. The process is very simple. You need to provide the template path which is where your template is stored on your machine to the following command</p>
<p><code>aws cloudformation validate-template --template-body file:///home/local/test/sampletemplate.json</code></p>
<ol start="2">
<li><strong>By using an external linter library</strong></li>
</ol>
<p>Another way is to use an external library. There is a library named "cfn-lint". This is a Python-linter library that helps in validating the cloudformation templates. This library supports both JSON as well as YAML-based templates.</p>
<p><code>cfn-lint -t template.yaml</code></p>
<h2 id="heading-what-is-aws-cloudformation-designer">What is AWS Cloudformation Designer?</h2>
<p>AWS Cloudformation Designer is a designer tool provided by AWS, which helps users with zero coding knowledge to create cloudformation templates in a seamless experience. So how this works is there is one designer screen with multiple icons of various services provided by AWS. You just need to drag the icon of the service you want to use on the designer screen and configure the service based on the requirement. If you have multiple services just drag the service and connect the services accordingly. But if want this to work, you should have at least basic AWS services knowledge, so that you can understand what's going on.</p>
<h2 id="heading-what-is-aws-cloudformation-drift-detection">What is AWS Cloudformation drift detection?</h2>
<p>Drift detection is a feature that is used to check whether there have been any changes made to your infrastructure using the AWS management console or the AWS SDK. If there had been any changes made via the management console or the SDK, the drift detection system notifies the changes by highlighting what was the previous state and what is the new state.</p>
<h2 id="heading-alternatives-to-aws-cloudformation">Alternatives to AWS CloudFormation</h2>
<p>So, AWS Cloudformation is a service provided by AWS in the IAAC(Infrastructure as a code) category. But what if I don't want to use cloudformation and I want to use another tool serving the same functionality? The answer to your query is Terraform by HashiCorp. Terraform is another tool that creates and manages infrastructure on AWS as well as other cloud platforms like Azure, GCP, etc.</p>
<p>Terraform uses a declarative configuration language known as HashiCorp Configuration Language (HCL) which helps Terraform deploy infrastructure based on the code provided. The advantage of deploying infrastructure on multiple clouds using the same language/syntax structure makes Terraform a very tough competitor for AWS. Just imagine using a different service when using AWS, a different one on Azure, and a different one on GCP.</p>
<p>But if you are totally on AWS or Azure or GCP I will prefer to use the native IaaC service/solution provided. Like if you are on AWS then use cloudformation. The reason is the level of support, validation, and service cloudformation will be able to provide on AWS, it is possible that the same will not provided by Terraform.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding AWS CloudFront Content Caching]]></title><description><![CDATA[Introduction
Amazon CloudFront is a CDN service from Amazon Web Services that helps make websites faster by storing copies of website content in different places around the world. This way, when you visit a website, you get the information from a loc...]]></description><link>https://blog.cloudgags.in/how-does-aws-cloudfront-cache-the-content</link><guid isPermaLink="true">https://blog.cloudgags.in/how-does-aws-cloudfront-cache-the-content</guid><category><![CDATA[AWS]]></category><category><![CDATA[cloudfront]]></category><dc:creator><![CDATA[CloudGags]]></dc:creator><pubDate>Sat, 04 May 2024 10:19:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1714818733332/fba48012-9325-4350-8410-964715e1e593.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p><strong>Amazon CloudFront</strong> is a <strong>CDN service from Amazon Web Services</strong> that helps make websites faster by storing copies of website content in different places around the world. This way, when you visit a website, you get the information from a <strong>location nearby</strong>, making it load faster.</p>
<h2 id="heading-but-what-are-these-nearby-locations">But what are these nearby locations?</h2>
<p>These nearby locations are called <strong>Edge Locations.</strong> These edge locations are spread globally to cover most of the area on the globe. <strong>As per the AWS, there are over 410 PoPs, including more than 400 Edge Locations, and 13 regional mid-tier caches in over 90 cities across 48 countries.</strong></p>
<h2 id="heading-but-how-do-edge-locations-function">But how do edge locations function?</h2>
<p>The edge locations consist of various <strong>nodes</strong>. You can consider nodes like some entity/virtual servers to hold the data in the form of a cache. <strong>Each node caches the data for a certain amount of time based on certain parameters.</strong></p>
<p>Whenever content is requested, the request is landed on the nearest edge location closest to the location from where the request originated. Post landing Cloudfront checks whether there is any cache present on the edge location for the requested content. If the cache is present the content is served from the cache.</p>
<p>But if the content is not present the request is forwarded to the actual servers where the content is present. Once the response is sent from the origin servers the response is cached on the edge location and then the response is sent back to the users. Now when the user next time requests for the same content the cached content from the last iteration is served to the user. This cache mechanism also helps in reducing the latency for the user.</p>
<h2 id="heading-how-long-content-is-cached-on-the-edge-locations">How long content is cached on the edge locations?</h2>
<p>AWS CloudFront caches content on different parameters mentioned below.</p>
<ol>
<li><p><strong>Cache policy</strong></p>
<p> AWS CloudFront has a concept of cache policy. A cache policy is a document that contains the amount of time the content needs to be cached on the edge location.</p>
<p> Whenever we need to cache the content, we need to provide the <strong>default, minimum, and maximum TTL(Time to live)</strong>. These TTL values define for how much duration the content will be cached on the edge locations.</p>
</li>
<li><p><strong>No of requests</strong></p>
<p> This is a parameter that is not discussed more often but plays an important role. Let's assume we have configured a cache policy for our Cloudfront distribution which caches the content for 1 hour. However, the traffic on the CloudFront distribution is very low, with hardly 10 requests. This is where this parameter comes into the picture.</p>
<p> If the number of requests is very low, AWS Cloudfront does not cache the content even though a cache policy is set.</p>
</li>
</ol>
<h2 id="heading-how-to-control-cache-on-edge-locations">How to control cache on edge locations?</h2>
<p>Usually, the cache expires on the edge location based on the configurations done in the CloudFront distribution cache policy. But there are some times when we need to customize the cache expiration time according to the requirement.</p>
<p>We can achieve this functionality using the <strong>max-age and s-maxage header.</strong> These two headers are responsible for cache expiration.</p>
<p><strong>1. max-age</strong></p>
<p>This header works on the browser-level cache. Whenever you need to control the cache on the browser level you can configure this header under the <strong>Cache-Control</strong> header and return it while returning the response.</p>
<p><code>Cache-Control:"max-age=30"</code></p>
<p><strong>2. s-maxage</strong></p>
<p>You can use this header when you need to control the cache expiration time on the edge location/CDN level. This header works with all CDN providers like Cloudfront, Akamai, etc.</p>
<p><code>Cache-Control:"s-maxage=30"</code></p>
<p><strong><mark>Note: When you return only max-age the edge location as well as the browser both use the value as the expiration time, while the s-maxage only works with the edge location. It will not work on the browser level.</mark></strong></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Amazon CloudFront's efficient caching mechanism optimizes content delivery by strategically storing data at edge locations worldwide, significantly reducing latency and enhancing user experience across the globe.</p>
]]></content:encoded></item></channel></rss>