<?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>DEV Community: Arankarl</title>
    <description>The latest articles on DEV Community by Arankarl (@dumgrammer).</description>
    <link>https://dev.to/dumgrammer</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%2F1513231%2Fa109e11b-c232-4577-9f41-ddf4aca05bdb.jpeg</url>
      <title>DEV Community: Arankarl</title>
      <link>https://dev.to/dumgrammer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dumgrammer"/>
    <language>en</language>
    <item>
      <title>Building and Triggering an AWS Lambda Function for Beginners!</title>
      <dc:creator>Arankarl</dc:creator>
      <pubDate>Fri, 20 Dec 2024 12:23:52 +0000</pubDate>
      <link>https://dev.to/dumgrammer/building-and-triggering-an-aws-lambda-function-for-beginners-252c</link>
      <guid>https://dev.to/dumgrammer/building-and-triggering-an-aws-lambda-function-for-beginners-252c</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenbxxveoi3hc3q0a7cau.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenbxxveoi3hc3q0a7cau.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is AWS Lambda?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lambda allows us to run our code on a high-availability compute infrastructure without provisioning or managing servers. It is a &lt;strong&gt;serverless&lt;/strong&gt; compute service that is highly scalable, cost-efficient and ideal for event-driven architecture&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehmbs4svjkh7n48wsqp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehmbs4svjkh7n48wsqp1.png" alt="Asynchronous Function" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lambda can be used in many things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data Processing&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;  Process real-time streaming data from services like Amazon Kinesis or Amazon SQS.&lt;/li&gt;
&lt;li&gt;  Transform and store data in databases or data lakes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2 Web Applications&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Build and deploy RESTful APIs with AWS API Gateway.&lt;/li&gt;
&lt;li&gt;  Serve as backend logic for lightweight web apps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3 Event-Driven Tasks&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Trigger tasks such as image resizing, sending notifications, or cleaning logs when files are uploaded to S3&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In this guide, we'll explore AWS Lambda, create a basic function, test it, and finally implement a useful notification simulation.
&lt;/h2&gt;

&lt;p&gt;Now we're going to test the AWS Lambda and to do this we need to set up a few things. Follow these steps:&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create a Lambda Function
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Log in to the AWS Management Console&lt;br&gt;
Go to &lt;a href=""&gt;aws.amazon.com&lt;/a&gt; to sign up if you don't have an account&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to the AWS Lambda service.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note: You can easily navigate it by searching it on the search bar [Alt + S]&lt;/em&gt;&lt;br&gt;
3 Click 'Create a Function'&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqyh1nx8bsfl6ofppus1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqyh1nx8bsfl6ofppus1.png" alt="Use cases" width="390" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4 In the Basic Information pane let's name it 'myLambdaFunction' &lt;br&gt;
  &lt;em&gt;Note: You can name it the way you want&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;5 For Runtime, choose either Node.js 22.x or Python 3.13.&lt;br&gt;
  &lt;em&gt;In this case I'll be using Node.js&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After setting up that should look like this&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78w8qcss262aisjui37j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78w8qcss262aisjui37j.png" alt="Image description" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;6 Click 'Create Function'&lt;br&gt;
  After doing so it should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d4bpl1ciohkvtf0isfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d4bpl1ciohkvtf0isfg.png" alt="Image description" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Create a simple testing code
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;This snippet came from the documentation as a way to test waters but later, I will show some application&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const handler = async (event, context) =&amp;gt; {

  const length = event.length;

  const width = event.width;
  let area = calculateArea(length, width);
  console.log(`The area is ${area}`);

  console.log('CloudWatch log group: ', context.logGroupName);

  let data = {
    "area": area,
  };
    return JSON.stringify(data);

  function calculateArea(length, width) {
    return length * width;
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Deploy the code by clicking &lt;strong&gt;Deploy&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjx5c2wzhdbexttmi8eb8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjx5c2wzhdbexttmi8eb8.png" alt="Image description" width="264" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function is named handler with two arguments event and context&lt;/li&gt;
&lt;li&gt;An event in Lambda is JSON (JavaScript Object Notation) that bears our data&lt;/li&gt;
&lt;li&gt;In this case we'll create an event in the console by entering a JSON formatted document with two key-value pairs. &lt;strong&gt;'length'&lt;/strong&gt; and &lt;strong&gt;'width'&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The second argument we take is context which contains information about the function invocation and execution environment. In this sample it uses the &lt;strong&gt;'.logGroupName'&lt;/strong&gt; to output the name of its CloudWatch&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 3: Create a Test Event
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In the &lt;strong&gt;Test Event&lt;/strong&gt; section you can choose &lt;strong&gt;Create test event&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7bw1s0v3kdzyhm1zhog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7bw1s0v3kdzyhm1zhog.png" alt="Image description" width="265" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2 After clicking that you should see this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fci17pmteltjn5tu30tpy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fci17pmteltjn5tu30tpy.png" alt="Image description" width="460" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3 Lets name it 'myTestEvent' &lt;br&gt;
4 In the &lt;strong&gt;event JSON&lt;/strong&gt; section, replace the default JSON with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "length": 6,
  "width": 7
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;So as you can see it's all coming together right since this is what we're chaining in the event argument&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Choose &lt;strong&gt;Save&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To test this code just click the run icon&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fso60otc1ppx9xg7144fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fso60otc1ppx9xg7144fc.png" alt="Image description" width="373" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and voila you have tested your first lambda function!&lt;/p&gt;

&lt;h2&gt;
  
  
  Now for the last part we're going to create something useful but still faithful to this structure
&lt;/h2&gt;

&lt;p&gt;Now let's change the code with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const handler = async (event, context) =&amp;gt; {

  const { email, password } = event;

  if (!email || !password) {
      return {
          statusCode: 400,
          body: JSON.stringify({ message: "Email and password are required." }),
      };
  }

  const notiMessage = `Notification sent to: ${email}`;
  console.log(notiMessage);
  console.log('CloudWatch log group: ', context.logGroupName);
  return {
      statusCode: 200,
      body: JSON.stringify({
          message: "Notification triggered successfully.",
          details: notiMessage,
      }),
  };
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what this function does.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So first instead of repeatedly typing the event we declared right away all the things we're expecting to come &lt;strong&gt;'email'&lt;/strong&gt; &amp;amp; &lt;strong&gt;'password'&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Then create a simple if validation to check if the &lt;strong&gt;'email'&lt;/strong&gt; or &lt;strong&gt;'password'&lt;/strong&gt; has a value&lt;/li&gt;
&lt;li&gt;We still use the &lt;strong&gt;CloudWatch&lt;/strong&gt; to monitor the logs&lt;/li&gt;
&lt;li&gt;And if all goes as planned the body would return a success message that has a binded value of static string and the the details with the binded value of the &lt;strong&gt;const notiMessage&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now for the test event lets just change the keys and values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "email": "bastianlacap55@gmail.com",
  "password": "7hi$isAT3stSimUl5t0n"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we run it and it should ouput something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F307z79eakeg75m8hax9x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F307z79eakeg75m8hax9x.png" alt="Image description" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to advance you can use &lt;strong&gt;Nodemailer&lt;/strong&gt; and to actually send an actual email message as well as implementation of asynchronous programming. For documentations just follow the links here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.tourl"&gt;https://docs.aws.amazon.com/lambda/latest/dg/welcome.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.tourl"&gt;https://nodemailer.com/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.tourl"&gt;https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Async_JS&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;br&gt;
That's it for our: &lt;br&gt;
&lt;strong&gt;Building and Triggering an AWS Lambda Function for Beginners!&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>cloudcomputing</category>
    </item>
  </channel>
</rss>
