Creating a lambda function with terraform

April 27, 2019    development   serverless   aws

Introduction

Here we will see how to use terraform to manage lambda functions, it will be a simple hello world in node.js, available as a gist here, note that I did not create this example but it’s really close to the official documentation but shorter, you can see another example with python here.

Before you start make sure you already have your account configured for awscli and terraform installed.

Configuration files

First of all we need to get our terraform file or files (in a normal case scenario, but since this is a hello world it is easier to have everything in the same file), I have added some comments of what each part does as you can see.

The code itself

Then we need the code that we need or want to run there.

Initialize terraform

First of all we will need to initialize terraform like in the gist below, this will download the necessary plugins that we used in the code, otherwise it won’t be able to run.

Apply the changes

The next step would be to apply the changes, you can also plan to an outfile and then apply from that file, but also apply works, this command will take care of doing everything that we defined, it will archive the code, the IAM role and the function itself.

Running the function

Then the last step would be to run our function to see if it actually works, in this case we’re using the awscli but you can use the AWS console as well, the result will be the same.

Clean up

Remember to clean up before leaving.

I don’t know about you, but I’m going to keep using the serverless framework for now, but it’s good to see that we have alternatives and with some effort can give us the same functionality.

Errata

If you spot any error or have any suggestion, please send me a message so it gets fixed.

Also, you can check the source code and changes in the generated code and the sources here



comments powered by Disqus