Jumping Into Serverless
Tags: aws, serverless, javascript, beginners
Published: 2024-05-10 01:41:14

Every wonder what it takes to build a serverless application on AWS?  Me too. After recently passing my AWS Solution Architect certification I thought I would take some time to dive into the world of server less and build my first functioning application.  After all, according to my classes and the internet server less is fast and fairly easy.  You hear things like “ just put your business logic in a lambda and deploy” all over.  Is it really that easy?

## What I thought I was going to build

I set out to build a simple application using some AWS serverless technology, I mean I did just get AWS certified.  I thought a nice simple application that delivers a weather forecast to me from the NWS API, this will be simple.  In just a few hours with some intense googling I should have something that resembles a well architected enterprise application as pictured below. This should be an easy project to accomplish with a few hours a day in my spare time, right? ![Imagination Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmwzxzgmbfhvqho0qw8b.png)

My original thought was a couple of lambdas, a SQS queue and I got this.  I was envisioning being able to build something within 4-8 hour timeframe.  After all everyone says “Lambdas are easy”, plus I did not feel like I was just trying to swing for the fences, just create what I felt was the basics.  I wanted to create one lambda that would query the NWS API and return a forecast, narrow it down to the next 2 periods (24 hours); and then ship the forecast off to a SNS topic that would email me.  Reading the NWS API Guide, I seen that I would only need to make 2 calls and be able to handle the JSON and get a forecast out.  So I set off to the console.  

## What did I build

The first evening I went to the admin console, clicked over to Lambda, and hit that create button.
![Lambda Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kjhy2xj7uqo7z3bdhh3.png) 

![Lambda Consle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vczgnhsraq3lo48a97sv.png)I started a local project on my laptop to quickly spit out some javascript(btw, I am not a JS developer, rather a novice) that I thought would accomplish the job.  I pushed the functions that I wrote to the lambda console via a copy and paste; I was too excited to be bothered with zipping and uploading.  I then spent five minutes researching how to test a lambda and quickly wrote out the test parameters and hit test. With in three seconds(the default lambda timeout) I was disappointed; I was staring at a test run that returned not only a failure, but a whole bunch of stuff that I did not understand.

What to do now?  With my limited experience I went back to my laptop and put the javascript application to the test on a local live server.  I found a couple of typos and one misplaced bracket, but I had it working in about five minutes.  Sweet! 

```
async function getEndpoint(latitude, longitude) {
    try  {
        const response = await fetch(
            `https://api.weather.gov/points/${latitude},${longitude}`
        )
        if(!response.ok){
            throw new Error('Fetching endpoint failed');
        }
        //await the response and store data
        const data = await response.json();
        //parse json and grab important parts
        const office = data.properties.cwa;
        const gridx = data.properties.gridX;
        const gridy = data.properties.gridY;
        const forecastUrl = data.properties.forecast
        //call forecast function passing items needed to complete url
        //console.log(forecastUrl);
        getForecast(forecastUrl);
    }
    catch(error) {
        console.error('Error: ', error);
    }
};

async function getForecast(url) {
    try {
        const forecast = await fetch(
            url
        )
        if(!forecast.ok){
            throw new Error('Fetching forecast failed');
        }
        //store response data
        const forecastData = await forecast.json();
        const period = forecastData.properties.periods
        //Make output more readable.
        clearData = JSON.stringify(forecastData, null, 2);
        console.log(url);
        //console.log(forecastData);
        //console.log(clearData);
        postPeriod(period[0]);
        
    }
    catch(error){
        console.error('Error: ', error);
    }
    
};
```

I copied my code that was working locally back to the lambda console, confidently grabbed a soda and prepared for a successful test.  I pulled up my test check the parameters and hit “test”.  Again, three seconds passed, FAILURE!  A test with a null response and messages I didn’t understand.  I started googling the errors the log was printing and following what was sure to be hot leads, but nothing helped.  Edit after edit on the function I was greeted with alternating invocation errors, and null responses.

## Immediate Response

I thought this was supposed to be easy!  I have have working “business logic” code base! Why?  What is so different about this?  These are the things I kept saying to myself over the next couple of days and nights as I continued to work on the function trying to get some semblance of working code.  I ready the AWS documentation over and over, reading blog posts, and even stack overflow posts; nothing seemed to yield a working solution.  I began to think that this just wasn’t meant to be.
Deep breaths and renewed mind

I finally found the correct AWS documentation and learned about the event handler and what it was.  This finally got me to a point where I could at least get back to returning a simple text string from the function to do something with.  So from here I broke my code up, and went with just trying to get the first API call to work.  This function takes a gps location and returns the forecast url with NWS grid and office for the API to get the actual forecast.  After some days I was able to get this function working and returning a URL I could do something with.  

I then tried to chain my next function in the code that would actually get the forecast from NWS.  This is where the renewed mind checked out.  Immediately I was back to nondescript errors, words I did not understand, and results that made no since to me.  It works locally just fine, put in in the lambda and it is like its not even the same langue anymore.  What is happening?  Slog on I will, but I WILL figure this out!   

## What I learned

So far I have learned several valuable lessons, and even a trick or two as this journey has grown in scope from a 4-8 hour adventure to well into its second week.  

First, serverless in not as point and click as I thought it was.  There are certainly gaps between the classroom and reality.   I have also been reminded here that practical application of theoretical knowledge often leads to early failure, but the knowledge gained is hard earned and hard to forget.  I feel that in the technology world you really have to try and use things for you to understand them.

Second, lambdas are the own animal.  This one is obvious to experienced developers, but to beginners like myself it just looks like another place to put some code and run it.  Before using lambda you really need to take the time to understand at a minimum; event handlers, message formats, json parsing.  These are key to getting data in and out of your lamas, no matter the language/runtime you choose.  I believe it would also help to choose a language that you are really comfortable with, unlike me; this was my first endeavor with javascript outside of some basic website actions.

Thirdly, tutorials and Youtube videos don’t always have all the answers for the problem you are facing.  I originally chose to use javascript for this because I though with it being the “official” runtime there would be many tutorials, code examples, and Youtube for me to go by; but as it turns out once you get past the event handler every thing is “custom” code and even if someone has done the same thing as you, they did it differently and what they have may not make any sense to you; especially when you a frustrated.  Be prepared to be searching more for concepts than direct answers to your problem.  If you are not getting the expect return from a nested function; you should be searching “how javascript nested functions work”, not “why does my function not return an answer”.

## Where am I going from here

I am going to continue this journey.  Though I have at times been frustrated, I am enjoying the process, even more so I a gain knowledge and confidence.  I have written down what my MVP is, and started laying out the steps to get there.  Turns out I am not that far away, just need to keep working.  In real life I am much further along, but I have decided I want to write about my experiences and bring others along, so this series will continue and I will keep advancing this project, if for nothing else personal satisfaction of completing something.  Please join me as I work to complete my MVP pictured below.

![MVP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vr9cgb2qqy8utckvc2xg.png)