<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Harshad sai Nanduri</title>
    <description>The latest articles on Forem by Harshad sai Nanduri (@harshadsainanduri).</description>
    <link>https://forem.com/harshadsainanduri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1223335%2F12fdb203-2bc6-4704-b52b-ba487666c754.png</url>
      <title>Forem: Harshad sai Nanduri</title>
      <link>https://forem.com/harshadsainanduri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/harshadsainanduri"/>
    <language>en</language>
    <item>
      <title>Running Docker Container on AWS Lambda to manage AWS Services</title>
      <dc:creator>Harshad sai Nanduri</dc:creator>
      <pubDate>Fri, 01 Dec 2023 09:25:18 +0000</pubDate>
      <link>https://forem.com/epam_india_python/running-docker-container-on-aws-lambda-to-manage-aws-services-4ajj</link>
      <guid>https://forem.com/epam_india_python/running-docker-container-on-aws-lambda-to-manage-aws-services-4ajj</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--30OB6ZPI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mknc3f2r6ejitm3ak4iy.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--30OB6ZPI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mknc3f2r6ejitm3ak4iy.PNG" alt="Image description" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to manage AWS service EC2 using AWS Lambda Containers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; We have used python language to manage aws service using lambda container.&lt;/li&gt;
&lt;li&gt; We have official documentation of aws boto3 to manage all aws service using python3.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Pre-requisites:&lt;/p&gt;

&lt;p&gt;• AWS CLI&lt;br&gt;
• AWS Services (LAMBDA, ECR Registry and EC2)&lt;br&gt;
• Docker&lt;br&gt;
• Python&lt;/p&gt;

&lt;p&gt;We have a python script to describe list of ec2 instances in AWS&lt;br&gt;
&lt;strong&gt;Python Script&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iJ3I3c7x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qv60qxlw0gbof2s0g9ks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iJ3I3c7x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qv60qxlw0gbof2s0g9ks.png" alt="Image description" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Docker file&lt;br&gt;
We have taken a Docker file from AWS official documentation which is supported to AWS lambda service python image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w8Yt6hgj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zowf34rqfey6j69taf2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w8Yt6hgj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zowf34rqfey6j69taf2u.png" alt="Image description" width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To create a container image from an AWS base image for Python&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a directory for the project, and then switch to that directory and copy Docker file and lambda_function.py file into this directory.&lt;/li&gt;
&lt;li&gt; For example, here's what your requirements.txt should look like if your function uses the AWS SDK for Python (Boto3).&lt;/li&gt;
&lt;li&gt; To generate Docker image from Docker file run the following command
                 docker build  -t lambda-image:latest .&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To upload the image to Amazon ECR and create the Lambda function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Run the get-login-password command to authenticate the Docker CLI to your Amazon ECR registry.&lt;/li&gt;
&lt;li&gt; Create a New ECR registry to store Docker images.&lt;/li&gt;
&lt;li&gt; Let’s Tag the Docker image with registry name and push the Image to  ECR registry by using aws cli command.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Start Deploying Docker image into AWS Lambda !&lt;br&gt;
let’s create a EC2 machine with required installed services like, Docker, Python and AWS(aws-cli).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m9mTdZ3p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx4p7ycvv102mho5jyos.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m9mTdZ3p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx4p7ycvv102mho5jyos.png" alt="Image description" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a directory for the project, and then switch to that directory and copy Docker file and lambda_function.py file into this directory.&lt;/li&gt;
&lt;li&gt; For example, here's what your requirements.txt should look like if your function uses the AWS SDK for Python (Boto3).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qSX_Q2CW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u5okgjdp68jgr7zub7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qSX_Q2CW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u5okgjdp68jgr7zub7o.png" alt="Image description" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.To generate Docker image from Docker file run the following command&lt;br&gt;&lt;br&gt;
                     docker build  -t lambda-image:latest .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kzK7g_Ac--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orq9hhigrbp68o0t47uj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kzK7g_Ac--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orq9hhigrbp68o0t47uj.png" alt="Image description" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4.Create a New ECR registry to store Docker images.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BKykKb3---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mgtpvv5p588t1grxwjg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BKykKb3---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mgtpvv5p588t1grxwjg.png" alt="Image description" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5.Let’s Tag the Docker image with registry name and push the Image to  ECR registry by using aws cli command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7yipi5rA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u06t3z6yyx51tdudkz8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7yipi5rA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u06t3z6yyx51tdudkz8u.png" alt="Image description" width="800" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;6.Now create a lambda function and provide url of the docker image from ECR and add required IAM role permissions to access EC2 services using lambda.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--39N6atzI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmdutpdbykwr3y4s47l5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--39N6atzI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmdutpdbykwr3y4s47l5.png" alt="Image description" width="800" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s run the lambda funtion to test it. Observe the instance I’d in the Log output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iuxTgmo5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcd34yndm2btokudbgot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iuxTgmo5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcd34yndm2btokudbgot.png" alt="Image description" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
