Deploying Your First Serverless Lambda Application: A Beginner's Tutorial

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 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 module to deploy the serverless application
  • Fork this serverless Node.js repo example, this is what we are going to deploy.
  • A Kubernetes cluster access. Let's use Minikube 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 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

Invite collaborators in you want to your project
Invite people

From the side bar, click on pipelines and start creating your CD pipeline.
create pipeline

creating pipeline

Add details to your pipeline
pipeline details

Next select the deploy stage and add the details
deploy stage

Select the serverless lambda deployment type from the deployment options available.
serverless deployment

Next, create a service to deploy.
create service

Add the required details
service details

Add the serverless manifest details
manifest details

Since our serverless manifest file is stored in GitHub let's add these details.
serverless manifest details

git details

The details we are specifying for our serverless application repo you forked above.
GitHub details

Add your GitHub username and password. Add password through a secret manager.
secret manager

Now, it is time to connect our GitHub repo to the Harness delegate.
select delegate

delegate not ready

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

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

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

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

You can verify the delegate installation
delegate install verify

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

Make sure the connection is successful.
successful connection of delegate

Continue adding your manifest details.
man details

git details bew

Now you can see your added manifest details in the service details.
service details mention

Next, create a pre-prod environment and connect AWS infrastructure.
connectr

Connect AWS in the infrastructure, and make sure you have the required details from your AWS account.
AWS connection

You need to connect your AWS connector with Harness delegate.

In the deployment step, you will select the rolling deployment step.
deployment step

Save everything and run the pipeline.
run pipeline

You will see a successful lambda deployment.

For more understanding, follow this official Harness developer hub guide.

No comments:

Post a Comment