So, in the last tutorial, we managed to get a simple CI/CD pipeline up and running so that whenever we commit anything to our github repo, it automatically builds and deploys these changes to our “production” environment.
In this tutorial, we are going to continue to develop our blog and get started building up the first of our Lambda functions that we’ll need to make our blog work.
Our First Lambda Function
We’ll start by creating a
hello world style lambda function that will later return a paginated list of blog posts. Once we’ve managed to successfully deploy this version 1 of the endpoint, we can then start working on integrating it with our DynamoDB table that we’ll be creating later on in the series.
Let’s break down the above code.
- line 1 - We import the
aws-sdkso that we can interact with DynamoDB later on
- line 3 - We define the the entry point of our Lambda function, this function takes in the following:
- event - Allows us to access the event data
- context - The context under which the Lambda will execute, this includes things like the runtime information.
- callback - The callback we will call when our function is ready to return
- line 5 - We call our
callbackfunction which returns a
statusCode, any response
Now that we have a simple Lambda function, let’s attempt to deploy this using the serverless command line interface. Create a new
serverless.yml file within a new
functions/ directory in the root of your project. You will then want to populate it with the following
Let’s break this configuration down so that we know what’s going on:
- line 1 - We define the name of the service that we will be deploying, in this case
- line 3 - We pin the serverless cli version we wish to use
- line 5 - We specify that we want to deploy to AWS and the
eu-west-1region within AWS
- line 9 - We specify what the array of functions we wish to deploy, the first of which is our
- line 11 - This is the runtime we wish our Lambda function to execute within
- line 12 - This is the handler of our function, this is a combination of
indexfunction that we defined within said file
- line 13 - We define how this Lambda is triggered, in this case through
httpand through a
Now that we’ve got this all set up for us, we can now deploy our lambda by doing the following:
This will then package everything up for us, and handle the deployment of our functions up to AWS. It will also set up any API Gateways required in order to interact with our Lambda functions. Upon running this, you should see something like this:
You should notice that in the list of
endpoints this deployment returns, we have a
GET request and the url for that request. If we now hit that API endpoint, we should be able to see
Hello World displaying within our browser!
Debugging your Lambda Functions
If you see any issues with this endpoint, it’s worthwhile looking into how you can accurately debug what has gone wrong with the code you have written and deployed. In order to do that, we need to look at AWS CloudWatch which hosts all of the logs of our Lambda functions with no additional setup needed. In order to view these logs, you can either navigate to CloudWatch directly, or you can open up the Lambda console and select your newly deployed function, you can then click the
monitoring tab at the top of the page and it will show you graphs that tell you things like invocation count, duration and any errors.
You should notice on each of these graph boxes, there is a link to
Jump to Logs at the top right. If you click on this, you will be taken to the CloudWatch logs and you can dive in and see things like stack traces if things have gone wrong:
We have now managed to write and deploy our first Lambda function! In the next few tutorials, we will be expanding our Lambda collection and start integrating with services such as DynamoDB and eventually GraphQL.
One question you may be asking is
Why Serverless?. Well, this one function we’ve deployed in this tutorial is highly scalable, incredibly resilient and ready for production. If you are interested, you should check out my article on How Serverless Computing will Change the World in 2018 for more one why you should use technology such as AWS Lambda within your production systems.
Under Construction: This course is currently under construction and new tutorials will be getting released in the coming days and weeks! Stay Tuned!