Create a Node and Express AWS Lambda function
Tags: node, aws, serverless, cloud
Published: 2022-10-18 18:02:55

## What is AWS Lambda

Before deploying your first AWS lambda function lets talk a little bit about Serverless and AWS Lambda. If you already know much about this you can skip to the next part. 

At first serverless sounds like there is no server involved but that not the case. It simply means that you're not responsible for managing or provisioning of the server and as a developer you will just focus on writing the logic in your code.

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. Once you upload your code to lambda the service handle all the capacity, scaling pathing and administration of the infrastructure to run your code.

Now lets get started to create and deploy your first lambda function.  

## Creating AWS accounts

Firstly you need to create a AWS account for which you need to verify your credit or debit card. The process is very simple.

1. Go to [AWS website](https://aws.amazon.com/) and click on Create an AWS Account then a webpage will appear to verify your email. Fill up your email and verify the OTP.
2. After creating your password you will be asked for your contact details
3. After this you will be asked to verify the payment method. Here you have to add your Card details after which 2 rupee will be credited from your account for verification and then it will be refunded. 
4. After few more steps you will be logged in your AWS console.

![AWS Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03un0fhxt7iwph9plss0.png)


## Connecting with AWS console

Firstly we need to install the AWS CLI on our machine. [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) go to this link and download the version supported by your OS. After the download is complete run the installation process and now to check your installation open your command prompt and enter 
```
C:\> aws --version
aws-cli/2.7.24 Python/3.8.8 Darwin/18.7.0 botocore/2.4.5
```

Now in order to enable the serverless framework to communicate with our AWS account we need to create a IAM account. This is basically a service to manage who get access to what resources of our AWS account.

1. Search for IAM service on the search bar and navigate to same. There you need to create a user by clicking on Add user.
![IAM Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8xnj3850f7vaeneo3sm.png)
2. Give a username and and check the programmatic access option and click next. 
![Add User](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jm0s0etnww1mrap7mpm.png)
3. Now you need to provide the permission to the users of what resources they can use. For now you can give AdministratorAccess (They have access to all the resources).
![Set Permission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0miqau9oq6rl3g66rdof.png)
4. For the next two steps you can leave the column as it is and click on  Create User. You can copy the credential or Download the csv file. Later you need these to connect your machine to the AWS account.
![Credential](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5ok8kr9q8ouchckllte.png)

Now we will finally do the setup. For that we need to open our command prompt and `aws configure` and then provide the access key and the secret key of the recently created user

```
C:\>aws configure
AWS Access Key ID [****************TEUZ]: AK********3X
AWS Secret Access Key [****************Rh5l]: VVL******xKv9
```
Now we are all set to create and deploy our lambda function from our Command Prompt.

## Create Lambda function and Deploy

Now we need to setup [Serverless](https://www.serverless.com/) 

1. Just do a npm install to setup the serverless `npm install -g serverless`
2. Enter serverless and you will be given a list of templates. Select `AWS - Node.js - Express API`. Your template will get downloaded 
![template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u570qkks10ofmv050xty.png)

3. Open the folder in VS code and check your template.
4. Now we just have to deploy the code which might take few minutes. For that we need to open our project dir 
![deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhtu1lvatnxx6xu3m0ky.png)

5. Check your api by hitting on the provided endpoint from your browser or Postman.
![Test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9f251a32hr1up2g9ybs1.png)