So you’ve heard about this serverless thing and feel like getting your hands dirty and learning some of it. Good, many people agree on serverless being the future of computing and even if we’re not going to get rid of servers for a while and containers are a massive topic as well, the serverless model is gaining a lot of traction. So here’s a step by step guide on how to get your hands dirty fairly painlessly.
First thing, you need Docker. In case you’ve been living under a rock, Docker is the best way out there to run containers and SAM uses Docker to host sam local and provide local testing capabilities to your function.
Docker community edition should do the trick. Once you’ve installed it, create a user on the Docker Hub and sign in to your Docker user. That’s Docker done and dusted, yep, it’s that easy.
Install Python and pip
This is where the tricky bit comes in. If you are on Mac OS, then you should have Python 2.7 by default, which should do the trick. If you are on Windows, there’s a whole different ball game. You can get the Python install and then have a read on here to make sure you have all the kinks ironed out. You will have to add Python and the scripts folder to your path. The main thing you need to make sure is that you need to be using python 2.x. Sam will not work with Python 3.x
Make sure you have a working python install by typing in your command line:
Anything over 2.7 will work. Once you have installed python, it’s time to install pip. First of all make sure you’re not one of those lucky souls who have it and check on the command prompt:
If you can’t access it, do not panic, you’re on the 99% of people! download the get pip script. Save it to your drive and execute it with python:
Once it’s done you should have pip on your command line and the pip command for the version should be working. If you’re on Windows, make sure your scripts folder inside the python bin is on your path!
Install AWS SAM
Finally! This is getting easier now. You should by all means read the full install and introduction guide. Make sure if you played with SAM local before and installed it using the npm version that you remove it, the npm version of the sam cli stopped being updated on 0.2.11 and even if you download the newer ones they won’t show up.
Once you’ve cleaned up, all you need to do is run the following command (make sure you sudo if you’re on a Mac):
pip install --user aws-sam-cli
Once pip is done installing your SAM, you should be able to run
You’re nearly there! Make sure you have version 0.3 (released 8th May) and you can use the shiny new init command we have on the cli now. Ok time to get some code going, with the help of the cli new command mentioned before we can create the boilerplate for a new function. Because nodejs is the default (of course it is) you’ll have to specify you want .net for it, we can do this with the command:
sam init -r dotnetcore2.0 -n look-ma-no-server
Clearly -n is the function name! So this has created our very own first serverless app. Time to build it and give it a go locally, on your app’s folder, run the following:
cd look-ma-no-server dotnet publish src/HelloWorld sam local start-api
Point your browser at your url (most likely http://127.0.0.1:3000/) and voila, your very own local testing!
In this article I’m not going to cover a lot more as the read me of your brand new project has a lot of good instructions on how to follow up. If you want to get a look of how the generated code looks like, this is a good starting point: