Skip to content

Instantly share code, notes, and snippets.

@auryn31

auryn31/blog.md Secret

Created September 21, 2019 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auryn31/bfe0bcd6a6df0244b2c9f11f9250d321 to your computer and use it in GitHub Desktop.
Save auryn31/bfe0bcd6a6df0244b2c9f11f9250d321 to your computer and use it in GitHub Desktop.

HowTo: Getting started with AWS Lambda

AWS Lambda has long been a topic I would like to deal with. Now I have watched and started several tutorials. But most of them were either too long or too short. They all showed how to upload the function, but not how to test it locally.

Now I have my first functions online and want to show you how easy it is to get started.

In general, AWS Lambda is very easy to use and allows an easy start in Serverless Computing with the free Contignent. What AWS Lambda is and what you use it for can be found here and at Google.

Getting started

First you need the tool serverless. The tool allows you to deploy and use the lambda functions on the Amazon servers (and many more like Azure, Alibaba Cloud, etc). You can find the Amazon payment model here.

You can install serverless with the command npm install -g serverless. If it was installed correctly, test it with serverless --version. The output should look like this:

https://gist.github.com/ebe33aba55c9c327e124cc5106e07d3c

Now you create a folder in which you want to create your function. For this example I will name the folder first_serverless_function. If you change now with the terminal into the folder you can start here with the first lambda function. For the tutorial I use Node.js, where the tool serverless also provides boilerplate code for Python.

To start with the first AWS Lambda function, you can use the serverless boilerplate code:

https://gist.github.com/f513c87db21c6e73fd0abecc5975e298

In the terminal the output should look like this:

https://gist.github.com/e69556c00ead3126e845d39c7ada12ff

Now two files have been created. One handler.js and one serverless.yml. The handler.js contains the function and the serverless.yml describes it for AWS. You can test this function locally or directly to AWS Deployen.

Lambda lokal Testen

To execute the function you just created locally, it doesn't take much.

In the serverless.yml it is described that our function is called hello and uses the handler hello which is described in the handler.js. You can also simulate the function call with the serverless tool. For this you have to call the function as follows:

https://gist.github.com/3f8feabf77a302152541ad531ae4d9e8

The answer should look like this:

https://gist.github.com/107bc5c2d658fcefbb7e8d442d1529a6

To create a simple Hello World!, you can rewrite the function like this:

https://gist.github.com/5aa27b7fe692d081a2268a88629e8846

Now the function expects a parameter hello, which must be given. If you call the function as described above, this will lead to an error, because you did not specify a parameter. So the local testing has to change so that the parameter is given, or the function intercepts the error. It is best to do both. The function that catches the error directly looks like this:

https://gist.github.com/77eb7111f7a8ca9efe5f13c7f99f5f47

Now the call of just (serverless invoke local --function hello) should not be an error anymore and your response should look like this:

https://gist.github.com/e5bfb07adb48bf031b47761548653357

Now give the function a name (serverless invoke local --function hello --data '{"queryStringParameters":{"hello": "Peter"}}'). The response changes to :

https://gist.github.com/1da2fee57ae7d7da69992a43f7a29a7c

Now you have created a function that returns Hello NAME!. You will deploy it in the next step.

Deployment

To deploy the function you need an AWS account. You can easily create it here.

Once you have created an account, you can retrieve the login data to allow serverless deployment. You can find them here. Then create the credentials file for serverless with serverless config credentials --provider aws --key KEY --secret SECRET. You can check if this worked with cat ~/.aws/credentials. Both the key and the secret should be output.

Now you have to define the endpoint. This happens in the serverless.yml in lines 66-69. Here you change the endpoint like this:

https://gist.github.com/941f585a27bc20ed525cf3f3d8d9e567

And already you are ready to deploy your function to AWS. You do this with serverless deploy.

The deployment takes about a minute. After that the output in the console looks like this:

https://gist.github.com/2818c3d34246e6048e557a21ee8a912f

Now you can access the function you just created under the URL listed above. Without parameters you should simply return a Hello World!. If you now call the URL with the parameter hello(URL?hello=Peter), the function should answer Hello Peter! correctly.

Summary

In the tutorial you created your first AWS Lambda function, tested it locally and then deployed and executed it on the Amazon server. You are now ready to create more complex functions and run serverless. Besides, you only pay what you need and have a high scalability. And all in under 30 minutes.

I'm excited about AWS Lambda and hope to use it in production soon. Did you like the little introduction, let me applaud, do you have a question or a suggestion, please comment the article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment