Deploying Your First Serverless Lambda Application: A Beginner's Tutorial
Tags: serverless, lambda, tutorial, devops
Published: 2023-05-02 05:03:19

Welcome to this tutorial on how to deploy a serverless Lambda application! Serverless architecture has become increasingly popular over the years due to its many benefits such as cost-effectiveness, scalability, and reduced infrastructure management. [Amazon Web Services (AWS) Lambda](https://aws.amazon.com/lambda/) is a popular serverless platform that allows you to run code without provisioning or managing servers. In this tutorial, you will learn how to deploy a serverless Lambda application step-by-step.

Let's get started!

## Prerequisites:
- Free [Harness CD](https://app.harness.io/auth/#/signup/?module=cd&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_serverless_article&utm_term=get-started) module to deploy the serverless application
- Fork this [serverless Node.js repo](https://github.com/pavanbelagatti/serverless-nodejs-tutorial) example, this is what we are going to deploy.
- A Kubernetes cluster access. Let's use [Minikube](https://minikube.sigs.k8s.io/docs/start/) in this tutorial.
- Harness Delegate with Serverless installed: the Harness Delegate is a worker process that performs all deployment tasks. Don't worry about this, I'll show you how to do it.
- [AWS User account](https://aws.amazon.com/) with required policy: Serverless deployments require an AWS User with specific AWS permissions, as described in AWS Credentials from Serverless.com. 

**To create the AWS User, do the following:**

> Log into your AWS account and go to the Identity & Access Management (IAM) page.
> Click Users, and then Add user. Enter a name. Enable Programmatic access by clicking the checkbox. Click Next to go to the Permissions page. 
> Full Admin Access: click on Attach existing policies directly. Search for and select AdministratorAccess then click Next: Review. Check to make sure everything looks good and click Create user.
> View and copy the API Key and Secret to a temporary place. You'll need them later in the tutorial.

## Tutorial:
Login to the Harness CD module. Harness is a modern continuous delivery platform with AI/ML capabilities that helps to deploy applications faster.

Create a project
![create a project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72301wo2ru1iz99pc8r8.png)

Invite collaborators in you want to your project
![Invite people](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogd4q45cf7d6udezjdj1.png)

From the side bar, click on pipelines and start creating your CD pipeline.
![create pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60qodj7qh572sbjajudz.png)

![creating pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfzmnkl3p0qkufpyxvpa.png)

Add details to your pipeline
![pipeline details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8udbml9ro0qb171wtip.png)

Next select the deploy stage and add the details
![deploy stage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/217eov5yq6ikryronjgl.png)

Select the serverless lambda deployment type from the deployment options available.
![serverless deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cvlo1pp5uhktwd56qt8.png)

Next, create a service to deploy.
![create service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j90ke0vb3fw6hia6lnu7.png)

Add the required details
![service details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffymrdtgpusnfxv96j7l.png)

Add the serverless manifest details
![manifest details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6l1fmvv4iwky41lcgkv.png)

Since our serverless manifest file is stored in GitHub let's add these details.
![serverless manifest details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xug0n5u71dflwm2xkmq8.png)

![git details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nrgdwyy0ux7m6bjqnye.png)

The details we are specifying for our [serverless application repo](https://github.com/pavanbelagatti/serverless-nodejs-tutorial.git) you forked above.
![GitHub details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6eyynhf2it7nvzch9h4.png)

Add your GitHub username and password. Add password through a secret manager.
![secret manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31x8rb80k7g7hqjnjshp.png)

Now, it is time to connect our GitHub repo to the Harness delegate.
![select delegate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv276ndbzurppywa4zw9.png)

![delegate not ready](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6im6e4t345inodmhef3.png)

But we don't have any delegate yet ready, let's create one.
Create install new delegate and you will land to this next page where you will be guided to install your delegate.
Select Kubernetes manifest and basic from the options.
![Install the delegate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmwxlm3jtkqwx6lmxkgq.png)

Download the delegate yml file and edit it, why? Because we need to make sure it handles our serverless commands. 

Open your delegate yml file just downloaded and edit it and add the following command in the place of `init_script` 

```
- name: INIT_SCRIPT
          value: |-
              microdnf install -y nodejs
              npm install -g serverless@2.50.0
```

See the screenshot for more clear understanding.
![serverless script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2c4a7dblopp38uecieq8.png)

Save the yml file.

Make sure a cluster is running on your local to install the delegate. We can use Minikube to create a single node cluster and install our delegate.

Start the minikube with the following command

```
minikube start
```
![minikube start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d1m0mf3i2rqh2inwfkc.png)

Apply the following kubectl command for the yml.

```
kubectl apply -f harness-delegate.yml
```

Once you apply the above command, you can see a bunch of configurations getting added.
![kubectl apply command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik46e95ygsvzxnb0ntv1.png)

You can verify the delegate installation
![delegate install verify](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99lfqxdwn4a4ap5gytz8.png)

Once you successfully install the delegate, you will get back to the delegate connection step. Now, you will see the newly added valid delegate in the list to connect our manifest.
![delegate connection step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgml3gmu4c1hpxqkj855.png)

Make sure the connection is successful.
![successful connection of delegate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz5db2w277zshx33n5zy.png)

Continue adding your manifest details.
![man details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwu1y3wh0c1t2oza5b5m.png)

![git details bew](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6mhrz4r7bdjow3dzorl.png)

Now you can see your added manifest details in the service details.
![service details mention](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjdun47g2g6ue57ojw7a.png)

Next, create a pre-prod environment and connect AWS infrastructure. 
![connectr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo514epvws95c3hptvn3.png)

Connect AWS in the infrastructure, and make sure you have the required details from your AWS account.
![AWS connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ggfp38d4xohmjaizgh9.png)

You need to connect your AWS connector with Harness delegate.

In the deployment step, you will select the rolling deployment step.
![deployment step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26hwtn0zz7uq8kom3yn8.png)

Save everything and run the pipeline.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3ogxnoqnbnkfud7yav3.png)

You will see a successful lambda deployment.

For more understanding, follow this official [Harness developer hub guide](https://developer.harness.io/docs/continuous-delivery/onboard-cd/cd-quickstarts/serverless-lambda-cd-quickstart/).