Deploying Your First Lambda Function With Command Line
Tags: aws, python, serverless, beginners
Published: 2020-04-15 15:27:06

As a developer, you will often need to create APIs to interact with various systems and integrations. Traditionally, this required a lot of work as far as developing the infrastructure and deploying the code on either an on-premise or cloud server like EC2.

However, this method is slow and costly.

Thus, the concept of serverless computing has gained popularity in the past few years.

An example of a serverless service is [AWS Lambda](https://aws.amazon.com/lambda/). Lambda works similarly to an API end-point in the sense that you can call a single function as long as you have access to said function.

This service executes code only when it is needed, and scales the requests per day as per requirement automatically. It comes in handy when combined with API gateways to develop one optimal solution.

[AWS API Gateway](https://aws.amazon.com/api-gateway/) itself is a fully-managed service that allows developers to create, publish, monitor, and maintain their APIs at any scale.

When you combined it with a Lambda function, you now have what is known as a FaaS (Function as a Service). This allows developers to replace the slow, traditional back-end development process with a cost-efficient, serverless solution.

* * * * *

Benefits of Using Lambda
========================

There are several benefits of working with serverless technologies.

At the development level, Lambda makes it easier to manage systematic complexity and carry out integration testing. Simply put, Lambda enables engineers to focus entirely on business logic since it handles scaling, logging, error tracking, and authorization.

Some of the most essential benefits of Lambda are as follows:

Built-in logging
----------------

Have you ever developed a piece of infrastructure and realized you needed to devote a decent amount of time to include logging and error tracking?

Well, Lambda does all that for you. You can see all your logs in the AWS management console.


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

Logging for our test function

Scalability
-----------

Another interesting feature is that AWS Lambda scales your application automatically.

It runs your code in response to each request and then processes each request individually, parallel to the running code. This way, the triggers are scaled exactly with the size of the entire workload.

* * * * *

Developing Your First Lambda Function With API Gateway
======================================================

With all the benefits out of the way, let's discuss how to set up an AWS Lambda function.

Let's start by looking at the function below. This is a standard "Hello World" style function. You will also notice we are using Python, which is important when it comes to configuring your setup later on.

We would like to push this basic function onto AWS Lambda. Unlike your standard "Hello World" function, where you might only pass in a single variable, this one will take a parameter that references an "event". This will contain the data we will be passing in our HTTP request.

This data will then be parsed and will pull out any parameters in that body request and sets them to whatever we need. In this case, there is only the "name" variable.

Which we will use to send back "Hello" + name, and a status code 200. This will represent the function returning successfully.

Step 1: Python code
-------------------
```python
import json
def test_function(event, context):
    body={'message':'OK'}
    params = event['eventStringParameters']

    name =str(params['name'])
    helloString = "Hello " + name
    body['helloString'] = helloString

    response={
        'StatusCode':200,
        'body':json.dumps(body),
    }

    return response
```

Step 2: Serverless.yml file
---------------------------


To easily push this function to Lambda using the command line, we will also create a `serverless.yml` file.

This file will provide the parameters such as which programming language to use when run, the region, your credentials, as well as the actual function and the parameters required.

This helps configure the Lambda function so it is easy to push to the cloud with the command line.

The file below is what we will be using.

```yml
service: lambda
provider:
  name: aws
  stage: dev
  runtime: python3.6
  region: us-east-2
  credentials:
    accessKeyId: YOUR KEY HERE 
    secretAccessKey: YOUR SECRET HERE

functions:
  test_function:
    handler: handler.test_function
    memorySize: 512
    timeout: 60
    events:
      - http:
          path: get-hello
          method: get
          request:
            parameters:
              queryStrings:
                name: true


plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: true
    slim: true
```

Step 3: Install serverless
--------------------------

Now that we have our basic function and a `serverless.yml` file, the next step in deploying this function to Lambda is to [npm install serverless](https://www.npmjs.com/package/serverless-lambda?activeTab=readme), which will let you access the `sls` command.

The commands below will help you install this service.

```
npm install -g serverless
npm i -D serverless-dotenv-plugin
sls plugin install -n serverless-python-requirements
```

Step 4: Deploy the function
---------------------------

With serverless installed, you now have access to the `sls` command. This will allow you to deploy your serverless function with one command.

First, you do need to make sure you are in the folder where your `serverless.yml` and `test_function.py` exist. Make sure these are the only things in the folder.

This is important because, when you run the deploy command, your entire file will be deployed. You don't want to accidentally deploy 500GB of some random file.


![](https://miro.medium.com/max/1508/1*9D_FuNKXtfgXmtcz-k_dEg.png)

Once you have ensured that only the files you want to deploy are in your folder, you can run the below command in your terminal.

```
sudo sls deploy
```

After this, `sls` will deploy your code to AWS. Eventually, you should be able to find it automatically in your AWS management console.

![](https://miro.medium.com/max/60/1*s9rMXbUebcx934KV0GAAnA.png?q=20)

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

Congrats! You have launched your first serverless function.

Step 5: Testing your function
-----------------------------

To test this function, we are going to use a JSON file to pass the parameters. This will represent a basic HTTP request.

Below is a file we are calling `event.json` that you will need. Notice that it only has a "name" parameter, but in the future, you could make your serverless functions much more complex and, in turn, have more parameters.

Once you have this JSON file set up, then you can use the command below to test the function in the command line.

```
sudo sls invoke --- function predict-price --- path event.json
```

After running this function, you will get an output like the one below.

![](https://miro.medium.com/max/60/1*d_qO-0i-TYPJmqtj-4tUgA.png?q=20)

![](https://miro.medium.com/max/2484/1*d_qO-0i-TYPJmqtj-4tUgA.png)

This is the response you would get back if you were to implement this serverless function in your infrastructure.

There you go, you have launched and tested your first Lambda function using the command line.

* * * * *

Final Words
===========

AWS Lambda is a highly scalable, event-driven, serverless service that lets developers quickly develop functions. It can be used by software developers to create end-points and has also been used heavily by data engineers to develop data pipelines between AWS components like [S3](https://aws.amazon.com/s3/), [Kinesis](https://aws.amazon.com/kinesis/), and [Redshift](https://aws.amazon.com/redshift/).

Lambda's ability to reduce the time required to develop infrastructure is making usage of Lambda more popular every day. Not to mention, with all those expensive EC2 costs, the ability to only pay per call of a function makes Lambda a very enticing solution.

All in all, this FaaS (Function as a Service) is great for developers and can help streamline your development process.

If you would like to read more about data science, cloud computing and technology, check out the articles below!

[Data Engineering 101: Writing Your First Pipeline](https://www.theseattledataguy.com/data-engineering-101-writing-your-first-pipeline/)

[5 Great Libraries To Manage Big Data](https://www.theseattledataguy.com/5-great-libraries-manage-big-data-with-python/)

[What Are The Different Kinds Of Cloud Computing](https://logitanalytics.com/what-are-the-different-kinds-of-cloud-computing-services/)

[4 Simple Python Ideas To Automate Your Workflow](https://www.theseattledataguy.com/4-simple-python-ideas-to-automate-your-workflow/)

[4 Must Have Skills For Data Scientists](https://www.theseattledataguy.com/4-skills-data-scientist-must-have/)

[SQL Best Practices --- Designing An ETL Video](http://www.acheronanalytics.com/acheron-blog/sql-best-practices-designing-an-etl-video)

[5 Great Libraries To Manage Big Data With Python](https://www.theseattledataguy.com/5-great-libraries-manage-big-data-with-python/)

[Joining Data in DynamoDB and S3 for Live Ad Hoc Analysis](https://medium.com/better-programming/joining-data-in-dynamodb-and-s3-for-live-ad-hoc-analysis-c4cdd24da082?source=friends_link&sk=2f1830676db3d9b4900ce8d0e20c2723)