Deploying Your First Lambda Function With Command Line
SeattleDataGuy
Posted on April 15, 2020
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. 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 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.
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
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.
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, 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.
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.
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.
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, Kinesis, and 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
5 Great Libraries To Manage Big Data
What Are The Different Kinds Of Cloud Computing
4 Simple Python Ideas To Automate Your Workflow
4 Must Have Skills For Data Scientists
SQL Best Practices --- Designing An ETL Video
Posted on April 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.