{"id":28878,"date":"2024-04-09T16:50:28","date_gmt":"2024-04-09T15:50:28","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/desarrollo-serverless-simplificado-aws-lambda-y-python\/"},"modified":"2024-06-03T17:43:04","modified_gmt":"2024-06-03T16:43:04","slug":"desarrollo-serverless-simplificado-aws-lambda-y-python","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/","title":{"rendered":"Serverless Development Simplified: AWS Lambda and Python"},"content":{"rendered":"<p>Serverless computing has changed the way developers build and deploy applications in the cloud. AWS Lambda is one of the most popular services that allows you to run code without needing to manage servers. In this tutorial, I will guide you through how you can create and deploy functions in AWS Lambda using Python, one of the most loved programming languages for its simplicity and versatility.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#%C2%BFQue_es_AWS_Lambda\" >What is AWS Lambda?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Preparando_el_entorno_de_desarrollo\" >Preparing the development environment<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Instalacion_de_AWS_CLI_y_Boto3\" >Installing AWS CLI and Boto3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Configuracion_AWS_CLI\" >AWS CLI Configuration<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Creando_tu_primera_funcion_Lambda_con_Python\" >Creating your first Lambda function with Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Paso_1_Escribir_el_codigo_de_la_funcion\" >Step 1: Write the function code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Paso_2_Empaquetar_y_desplegar\" >Step 2: Pack and Deploy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Paso_3_Configurar_el_API_Gateway\" >Step 3: Configure the API Gateway<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Consideraciones_de_seguridad_y_mejores_practicas\" >Security Considerations and Best Practices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/simplified-serverless-development-aws-lambda-and-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_AWS_Lambda\"><\/span>What is AWS Lambda?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>AWS Lambda is an event-driven computing service that allows you to execute code in response to specific events in AWS and easily integrate different AWS services. With Lambda, you only pay for the computing time you consume, making it an efficient and scalable option for many applications.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preparando_el_entorno_de_desarrollo\"><\/span>Preparing the development environment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we dive into creating Lambda functions, it&#039;s important to set up your local development environment. You will need the following:<\/p>\n<ul>\n<li><strong>AWS CLI:<\/strong> Tool that allows you to interact with AWS services from the command line.<\/li>\n<li><strong>AWS SDK for Python (Boto3):<\/strong> Allows your Python script to interact with AWS services.<\/li>\n<li><strong>Python 3:<\/strong> Lambda supports several versions of Python, make sure you have the most appropriate one installed.<\/li>\n<li><strong>A code editor of your choice:<\/strong> It can be VSCode, PyCharm, or any other that is comfortable for you.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Instalacion_de_AWS_CLI_y_Boto3\"><\/span>Installing AWS CLI and Boto3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To install AWS CLI you can follow the instructions in the <a href=\"https:\/\/aws.amazon.com\/cli\/\" rel=\"nofollow noopener\" target=\"_blank\">official AWS documentation<\/a>. Next, install Boto3 using pip, the Python package manager:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">pip install boto3<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Configuracion_AWS_CLI\"><\/span>AWS CLI Configuration<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Once the AWS CLI is installed, run the following command to configure your credentials (you will need to have your access key ID and secret access key):<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">aws configure<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Creando_tu_primera_funcion_Lambda_con_Python\"><\/span>Creating your first Lambda function with Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Paso_1_Escribir_el_codigo_de_la_funcion\"><\/span>Step 1: Write the function code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Let&#039;s start with a simple function that will be triggered via an HTTP event created by API Gateway. The following Python code receives an event and returns a welcome message with the name passed as a parameter in the body of the event.<\/p>\n<pre><code class=\"&quot;language-python&quot;\">import json def lambda_handler(event, context): # Parse the incoming JSON body = json.loads(event[&#039;body&#039;]) name = body[&#039;name&#039;] # Create a response object response = { &quot;statusCode&quot;: 200, &quot;headers&quot;: { &quot;Content-Type&quot;: &quot;application\/json&quot; }, &quot;body&quot;: json.dumps({ &quot;message&quot;: &quot;Hello, {}!&quot;.format(name) }) } return response<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Paso_2_Empaquetar_y_desplegar\"><\/span>Step 2: Pack and Deploy<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To deploy this code to AWS Lambda, we must package it along with any dependencies that are not available in the default AWS Lambda runtime.<\/p>\n<ol>\n<li><strong>Create a file called <code>requirements.txt<\/code><\/strong> if your function depends on external libraries. For example:\n<pre><code class=\"&quot;language-bash&quot;\">echo &quot;requests&quot; &gt; requirements.txt<\/code><\/pre>\n<p>Then install the dependencies in a folder called <code>packages<\/code>:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">pip install -r requirements.txt -t package\/<\/code><\/pre>\n<\/li>\n<li><strong>Package your code<\/strong> including the dependencies directory:\n<pre><code class=\"&quot;language-bash&quot;\">cd package zip -r ..\/my-deployment-package.zip . cd .. zip -g my-deployment-package.zip lambda_function.py<\/code><\/pre>\n<\/li>\n<li><strong>Deploy your function using AWS CLI<\/strong>:\n<pre><code class=\"&quot;language-bash&quot;\">aws lambda create-function --function-name myFunction --runtime python3.8 --role arn:aws:iam::123456789012:role\/lambda-role --handler lambda_function.lambda_handler --zip-file fileb:\/\/my -deployment-package.zip<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"Paso_3_Configurar_el_API_Gateway\"><\/span>Step 3: Configure the API Gateway<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To make your Lambda function accessible over HTTP, you need to configure AWS API Gateway:<\/p>\n<ol>\n<li><strong>Create a new API<\/strong> using the AWS console or AWS CLI.<\/li>\n<li><strong>Set up a new resource and method<\/strong> (for example, GET or POST) that points to your Lambda function.<\/li>\n<li><strong>Deploy your API<\/strong> in a new or existing stage and note the generated URL.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Consideraciones_de_seguridad_y_mejores_practicas\"><\/span>Security Considerations and Best Practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When deploying Lambda functions, it is crucial to consider code security and optimization:<\/p>\n<ul>\n<li><strong>Use appropriate IAM roles<\/strong> that limit permissions to what your function strictly needs.<\/li>\n<li><strong>Handle errors appropriately<\/strong> so that you do not expose sensitive information.<\/li>\n<li><strong>Monitor and record appropriately<\/strong> the behavior of your functions to detect possible improvements or abuses.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>AWS Lambda and Python form a powerful combination for creating efficient and scalable serverless applications. This tutorial has walked you through the steps necessary to configure, develop, and deploy a basic feature, but the possibilities are vast. I encourage you to explore more about AWS Lambda and other AWS services that can elevate your projects to new heights. If you have questions or need help, don&#039;t hesitate to <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact me<\/a>.<\/p>\n<p>Happy coding!  <\/p>","protected":false},"excerpt":{"rendered":"<p>Serverless computing has changed the way developers build and deploy cloud applications. AWS Lambda is one of the most popular services that allows you to run code without having to manage servers. In this tutorial, I will walk you through how you can build and deploy functions on AWS Lambda.<\/p>","protected":false},"author":1,"featured_media":28879,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1901],"tags":[1993,205,211,1994,627,1008,515,1996,1995],"class_list":["post-28878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-pruebas-de-proyecto","tag-aws","tag-blog","tag-desarrollo","tag-lambda","tag-proyecto","tag-pruebas","tag-python","tag-serverless","tag-simplificado"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28878"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28878\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28879"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}