Simply: AWS lambda
Tags: lambda, aws, javascript, beginners
Published: 2020-03-02 15:07:52


![](https://miro.medium.com/max/360/1*qg4GEY91S1IHZ1nXfPCVSg.png)

# λ  What is AWS lambda? 
With AWS lambda we can write code and execute it without caring about configuring servers. 

# λ Why should I use it? 
It enables you to quickly develop business relevant code and deliver value for your customers and stakeholders

# λ How do I start?
First you're gonna need an AWS account, [follow this guide](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/). 

## 🛠️Creating our first lambda
From the AWS console head to **Services** and search for **Lambda** select the first option
![Imgur](https://i.imgur.com/x14g9ec.png)
Click **Create Function**
![Imgur](https://i.imgur.com/vMg1Vd5.png)
Enter your name for the lambda and select runtime (i'm going with Node.js) Leave everything else default
![Imgur](https://i.imgur.com/lxDK53H.png)

## ⌨️ Writing code
When your lambda is created you'll be taken to that lambdas page where you can see and setup lots of information and options about your lambda, let's not worry to much about that right now and just scroll down to "Function Code"
![Imgur](https://i.imgur.com/GoeMhar.png)
Using the inline editor (you are of course able to write code with any IDE you want and deploy it to AWS but i'll cover that in another post) let's enter some code, this is what I used

```javascript
exports.handler = async (event) => {
    console.log('event', event);
    
    // initiate animals array
    const animals = ['cat', 'dog', 'tardigrade'];
    
    // get input
    const input = JSON.parse(event.body).input;
    
    // concatinate animals with input
    concatAnimalsInput(animals, input)
    
    // create a response object and return it
    const response = {
        statusCode: 200,
        body: JSON.stringify(animals),
    };
    return response;
};

const concatAnimalsInput = (animals, input) => {
    if(typeof input === 'string') {
        animals.push(input);
    } else {
        animals = animals.concat(input);
    }
}
```
![Imgur](https://i.imgur.com/1ymDaDW.png)

## 🐦 Testing our code
At the top of the screen click **configure test event** and create an event to execute the function with
![Imgur](https://i.imgur.com/f1vBFSz.png)
The event in JSON format
![Imgur](https://i.imgur.com/Ch3tD3o.png)
Hit **Create** and finally click the "Test" button
![Imgur](https://i.imgur.com/AV90cma.png)
After its execution you'll see the result and the output by clicking **Details** in the green result box, you can also click **(logs)** to enter CloudWatch Logs and get a better look into all executions of your lambda
![Imgur](https://i.imgur.com/T9FgYEZ.png)

# 🎉 Good Job!
You've just created a lambda and the possibilities with it are endless, in [future posts](https://dev.to/tqfipe/simply-aws-dynamodb-1kb8) I'll discuss how we can connect an API to our lambda via [API Gateway](https://aws.amazon.com/api-gateway/) and how we can store our data in the NoSQL database [DynamoDB](https://aws.amazon.com/dynamodb/)




# λ Discussion

## 💰What about the price? 
With Lambda the first **million** requests each month are alway free after that you pay $0.20 per 1M requests and $0.0000166667 for every GB-second, read more [here](https://aws.amazon.com/lambda/pricing/). Lambda is usually used together with other AWS services that might also incur cost such as Cloudwatch logs which we touched upon in this post, Cloudwatch logs also offer a [free tier](https://aws.amazon.com/cloudwatch/pricing/), 5GB of Log Data Ingestion and 5GB of Log Data Archive, which means nothing we did in this post will result in any cost even if you do no cleanup. 
Read more about the economics of cloud here ["Cloud is expensive"](https://tiqqe.com/cloud-is-expensive/)

## I don't want to use the inline code editor!
Great, me neither, I suggest as a first step either looking into exporting your code to zip and uploading to the lambda 
![Imgur](https://i.imgur.com/l4JdB5O.png)
or exploring the [Serverless framework](https://serverless.com/), a tool that makes it easy to deploy serverless application such as Lambda!

If you liked this guide please consider reading my latest one about DynamoDb, written in the same way as this one, you can find it [here](https://dev.to/tqfipe/simply-aws-dynamodb-1kb8)

## 👨‍💻 Contact me
Questions? Thoughts? 
Twitter: [@tqfipe](https://twitter.com/tqfipe)
Linkedin: [Filip Pettersson](https://www.linkedin.com/in/filip-pettersson-08747bb7/)