<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ifezouani ilyass on Medium]]></title>
        <description><![CDATA[Stories by ifezouani ilyass on Medium]]></description>
        <link>https://medium.com/@ifezouanii?source=rss-9648496fb814------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XFpb2O9KquF5k-ixSVrCFQ.jpeg</url>
            <title>Stories by ifezouani ilyass on Medium</title>
            <link>https://medium.com/@ifezouanii?source=rss-9648496fb814------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 23:37:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ifezouanii/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Advanced AWS Lambda — An Obscure Feature You Must Absolutely Use]]></title>
            <link>https://awstip.com/advanced-aws-lambda-an-obscure-feature-you-must-absolutely-use-2d03110d563f?source=rss-9648496fb814------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d03110d563f</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[ifezouani ilyass]]></dc:creator>
            <pubDate>Thu, 18 Apr 2024 09:19:10 GMT</pubDate>
            <atom:updated>2024-04-24T14:42:58.798Z</atom:updated>
            <content:encoded><![CDATA[<h3>Advanced AWS Lambda — An Obscure Feature You Must Absolutely Use</h3><h3>Introduction</h3><p>If you have ever worked as a Cloud or DevOps engineer, you might have created an AWS Lambda function once or twice before. Most probably you developed this function in a natively supported runtime like Python or NodeJS, and its aim was to execute a simple process or integrate with some other AWS services like AWS Cognito or API Gateway to do some identity verification. This implementation has led you to familiarize yourself with basic AWS Lambda operations, like choosing the runtime, uploading your code, setting and using environment variables, and attaching a role with basic execution permissions.</p><p>But what if your code was written in a non-natively supported runtime, like a Shell or Go script. Your first instinct might tell you to translate that code to Python, or better still, asking GenAI to do the translation for you. This might work if your code is a simple and short function, or/and your company’s security &amp; confidentiality policy allows you to send your code to third party GenAI; But in most cases, if you work in a big company that have strict policies and requirements, you will not be able to do this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*HLkvF3q4nyCWnwjiUiH5qA.gif" /></figure><p>This post will explain how you can easily and simply create a Docker image that offers the custom runtime needed for your function to run, and all you need is a basic understanding of Docker and AWS Lambda concepts.</p><h3>AWS Lambda under the hood</h3><p>Before we dive deep into how to configure a custom runtime, let’s take a step back and try to demystify AWS Lambda’s magical way of executing your function in a serverless mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xOUdtfOzeVA11M3Z.png" /></figure><p>AWS Lambda serverless execution is made possible thanks to three components :</p><ol><li><strong>Lambda Service:</strong> It takes care of provisioning, scaling, monitoring, and logging. It manages the lifecycle of the function execution including deploying code and invoking functions.</li><li><strong>Execution Runtime:</strong> This is the environment in which your code runs. AWS Lambda supports multiple runtimes such as Node.js, Python, Java, etc., and also allows the use of custom runtimes for other programming languages or specific runtime versions.</li><li><strong>Runtime API:</strong> This interface sits between the Lambda service and the execution runtime. It is used for the communication between AWS Lambda’s infrastructure and the function’s execution environment.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bko4RGBDZXOixCUZoH_G8Q.png" /></figure><p>We’ll focus mostly on the Blue rectangle which consists of the runtime and our function code. This is the part we need to modify to make our Lambda function work. Usually, supported AWS Lambda runtimes have an event loop that continuously polls the Runtime API for new events, loads the function handler, executes it once an event is received, then sends the response back to the Runtime API, which in turn returns this response to the client (Lambda service in this case).</p><p>AWS offers us what’s called an OS-only runtime, which provides the language support, additional settings such as environment variables and certificates needed to execute our script, and configures all the necessary APIs to interact with Lambda Service in one hand, and our process on the other hand <a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-provided.html"><strong>[1]</strong></a>. All we have to do is add a small script, bundle it with our code, build a new docker image based on AWS OS-only runtime, upload this image to ECR, and finally use it as an image for our AWS Lambda. Easy and simple !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*Fs-d5kxj5Aa3LL0Jm0MF-Q.gif" /></figure><h3>Custom runtime implementation</h3><p>In this section I will give you the necessary scripts and code needed to build the Docker image for our AWS Lambda custom runtime. Get ready to geek out — grab your coffee, because we’re venturing deep into “The Code Jungle”!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*-AMtT2H3qw0N26uRiqTVwQ.gif" /></figure><blockquote>bootstrap</blockquote><pre>#!/bin/sh<br><br>set -euo pipefail<br><br># export<br># Initialization - load function handler<br>source $LAMBDA_TASK_ROOT/&quot;$(echo $HANDLER | cut -d. -f1).sh&quot;<br><br># Processing<br>while true<br>do<br>  HEADERS=&quot;$(mktemp)&quot;<br>  # Get an event. The HTTP request will block until one is received<br>  EVENT_DATA=$(curl -sS -LD &quot;$HEADERS&quot; &quot;http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next&quot;)<br><br>  # Extract request ID by scraping response headers received above<br>  REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id &quot;$HEADERS&quot; | tr -d &#39;[:space:]&#39; | cut -d: -f2)<br><br>  # Run the handler function from the script<br>  RESPONSE=$($(echo &quot;$HANDLER&quot; | cut -d. -f2) &quot;$EVENT_DATA&quot;)<br><br>  # Send the response<br>  curl &quot;http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response&quot;  -d &quot;$RESPONSE&quot;<br>done</pre><p>The first line <strong>“set -euo pipefail”</strong> is to configure the shell to handle errors robustly and to exit immediately if any command within the script fails.</p><p>This script starts by loading the function handler through the source command.</p><p>Then it starts a loop to probe the Runtime API for any new events by calling the <strong>invocation/next </strong>API.</p><p>It then extracts the request ID to help identify the Lambda invocation.</p><p>After that, it executes our main shell script with the payload data received from the Lambda service.</p><p>And finally, it sends the response back to the Runtime API by calling the <strong>invocation/$Request_ID/response</strong></p><blockquote>Dockerfile</blockquote><pre>FROM public.ecr.aws/lambda/provided:al2023<br><br>ENV HANDLER=&quot;function.handler&quot;<br><br>COPY ./bootstrap  ${LAMBDA_TASK_ROOT}/bootstrap<br>COPY ./function.sh  ${LAMBDA_TASK_ROOT}/function.sh<br><br>RUN chmod +x ${LAMBDA_TASK_ROOT}/bootstrap<br>RUN chmod +x ${LAMBDA_TASK_ROOT}/function.sh<br><br>ENTRYPOINT [&quot;/var/task/bootstrap&quot;]</pre><p>This Dockerfile creates a Docker image based on the AWS public Lambda image : <strong>lambda/provided:al2023 </strong><a href="https://aws.amazon.com/fr/blogs/compute/introducing-the-amazon-linux-2023-runtime-for-aws-lambda/"><strong>[3]</strong></a><strong>. </strong>This image provides the language support, and additional settings such as environment variables and certificates needed to execute our script.</p><p>We set the environment variable to the <strong>handler </strong>of our shell script. It should be in the format &lt;<strong>script name&gt;.&lt;function name&gt;</strong></p><p>We then copy the previous <strong>bootstrap</strong> script and our shell script to <strong>$LAMBDA_TASK_ROOT </strong>(a defined Lambda runtime environment variable referencing the function code directory <strong>/var/task</strong>) <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime"><strong>[4]</strong></a></p><p>We make our scripts executable, and finally set the entrypoint to our <strong>bootstrap </strong>script which will loop over the Runtime API awaiting a new event.</p><blockquote>function.sh</blockquote><pre>function handler () {<br>  EVENT_DATA=$1<br>  echo &quot;$EVENT_DATA&quot; 1&gt;&amp;2;<br>  RESPONSE=&quot;Echoing request: &#39;$EVENT_DATA&#39;&quot;<br><br>  echo $RESPONSE<br>}</pre><p>This is a sample shell script that take <strong>EVENT_DATA </strong>as an argument and prints it out.</p><blockquote>Directory Structure</blockquote><pre>runtime-tutorial<br>├ bootstrap<br>├ Dockerfile<br>└ function.sh</pre><h3>Test it for yourself</h3><p>If you’ve made it so far, congratulations, you have now understood one of the most complex concepts about AWS Lambda functions. And as we all know, to make your knowledge stick, you must practice it !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*3JqMenMdO5NIFvKGb0Tylg.gif" /></figure><p>That’s why I have made a Terraform module that contains the previous scripts, and creates the necessary AWS resources to create a custom runtime Lambda and test its invocation for yourself.</p><p>This the Terraform script you need to write and execute if you wish to deploy and explore your Lambda function with its custom runtime :</p><pre>data &quot;aws_caller_identity&quot; &quot;current&quot; {}<br><br>data &quot;aws_region&quot; &quot;current&quot; {}<br><br>locals {<br>  current_account_number = data.aws_caller_identity.current.account_id<br>  region = data.aws_region.current.name<br>}<br><br>module &quot;custom-lambda&quot; {<br>  source = &quot;github.com/Ilyassxx99/lambda-custom-runtime.git&quot;<br>  custom_bash_function_name = &quot;custom-bash-function&quot;<br>  custom_bash_ecr_repository_name = &quot;custom-lambda&quot;<br>  current_account_number = local.current_account_number<br>  docker_image_custom_bash_uri = &quot;custom-lambda:latest&quot;<br>  lambda_image_custom_bash_version    = &quot;3.12.2&quot;<br>  lambda_image_arch              = &quot;amd64&quot;<br>  region = local.region<br>  tags = {<br>    Terraform-module = &quot;custom-bash-runtime&quot;<br>  }<br>}</pre><h3>Conclusion</h3><p>In this first post of my blog series <strong>Advanced AWS Lambda</strong>, we have seen how you can customize your Lambda runtime to support any programming language you might have used to write your script. This will be very useful when you are working with AOT compiled languages like Go or Rust, where execution performance of your script is crucial.</p><p>I hope you found this blog useful, if you have any feedback or any questions about this blog’s topic, or about another technical subject you want me to discuss in the future, please contact me on my <strong>Linkedin</strong> : <a href="https://www.linkedin.com/in/ifezouaniilyass/">https://www.linkedin.com/in/ifezouaniilyass/</a></p><p>Thank you for your time, and see you in the next blog. Cheers !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*WIjcsTL49wdYazQX2VS95Q.gif" /></figure><h4><strong>Notes :</strong></h4><ol><li>AWS supports OS-only <strong>provided.al2023</strong> runtime <a href="https://aws.amazon.com/fr/blogs/compute/introducing-the-amazon-linux-2023-runtime-for-aws-lambda/"><strong>[3]</strong></a> that will allow you to import zip directly instead of Docker image. I chose to use a Docker image in this article to cover the case where you need to include other dependencies for your code. You can directly “and more easily install packages/modules and package it inside a Docker image.</li><li>For AOT compiled languages like Go or Rust, AWS offers libraries that implement the runtime, with functions you can use to wrap your main process. In this article we will suppose you want to deploy a Shell script; In this case you will need to write a script that does the different Runtime API calls and process execution.</li></ol><h4>References :</h4><p><strong>[1]</strong> <a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-provided.html">https://docs.aws.amazon.com/lambda/latest/dg/runtimes-provided.html</a></p><p><strong>[2]</strong> <a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html">https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html</a></p><p><strong>[3]</strong> <a href="https://aws.amazon.com/fr/blogs/compute/introducing-the-amazon-linux-2023-runtime-for-aws-lambda/">https://aws.amazon.com/fr/blogs/compute/introducing-the-amazon-linux-2023-runtime-for-aws-lambda/</a></p><p><strong>[4]</strong> <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime">https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime</a></p><p><strong>[5]</strong> <a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html">https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d03110d563f" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/advanced-aws-lambda-an-obscure-feature-you-must-absolutely-use-2d03110d563f">Advanced AWS Lambda — An Obscure Feature You Must Absolutely Use</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>