DEV Community

Cover image for Create a Node and Express AWS Lambda function
Debopriya Dey
Debopriya Dey

Posted on

Create a Node and Express AWS Lambda function

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 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

Connecting with AWS console

Firstly we need to install the AWS CLI on our machine. AWS CLI 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
Enter fullscreen mode Exit fullscreen mode

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
  2. Give a username and and check the programmatic access option and click next. Add User
  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
  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

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
Enter fullscreen mode Exit fullscreen mode

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

  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

  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

  5. Check your api by hitting on the provided endpoint from your browser or Postman.
    Test

Top comments (0)