If you have followed the previous posts about creating lambda functions, you would have realized that it's a pain in the ass to configure the infrastructure manually using the AWS console, you always wonder if there's an easy way of setting up your infrastructure and honestly there is, and it's called infrastructure as code.
Let me give you an example to understand infrastructure as code
If you have used an ORM (like prisma, typeORM or DjangoORM) in your project to connect to your database, you know that it generally is a twostep process
where you first describe how your tables are going to look and then you run a migration which converts your code into SQL tables.
Similarly in infrastructure as code you describe your infrastructure in something called a YAML file (similar to JSON) and then you run a migration script which will setup your infrastructure according to the description provided.
In this post we are going to use a framework called serverless which specifically is used for you guessed it serverless infrastructure setup on various platforms like AWS, Cloudflare and etc...
But if you're interested in diving deep checkout Terraform or Ansible.
So, let's start
Serverless framework uses the aws cli to perform migrations so follow the below steps to setup AWS-CLI on windows.
First, download and install the AWS-CLI
Now check if the AWS-CLI was installed properly by running the following command in your terminal
awsand you should get a output like the image below.
This is the only manual step to start with, The serverless framework is going to use the aws cli to execute tasks on your aws account so you need to provision your aws-cli with the creds to access your aws account
- Search for IAM in the search bar and select users.
- Select add users
- give the user a name and provide programmatic access as we will be creating resources through code and hit next.
- For the purpose of this post provide the user administrative access but do not do it in a live project and hit next and create the user.
- Now you must see access code and secret key which we will just use in a second and make sure you don't share this with anyone.
- Now in the terminal run the command below and it will ask for your access key enter that followed by the secret key and hit enter until the process exits.
- First, we have to install the serverless framework globally by running
npm install -g serverless
- now that you have installed the serverless framework checkout to the location of your choice.
serverlessin the terminal you'll see that it gives you a variety of options to choose from choose AWS - Node.js - HTTP API and hit enter and select a project name and hit enter.
select no for all the following options and continue.
now cd into your serverless project directory and you should see the following:
There are 2 main files which needs to be explained
if you first open the handler.js file you'll notice that it's the code for your lambda function.
and the serverless.yml file is where you describe your infrastructure let me explain the instructions line by line.
YAML file store key value pair data just like JSON.
- In the
servicekey we pass in the service name that will be displayed on your aws lambda console like in the image below. (FYI the naming in the console has 3 properties service name, stage name and the function name all separated by "-".)
frameworkVersionspecifies which version of serverless framework we are using.
providerkey is used to configure your cloud provider
namehere specifies which cloud provider you're using.
runtimespecifies which programming language you're using.
(FYI: the provider block might look different for other cloud providers you look it up here)
- Under the
functionsblock you are telling serverless framework where to find your functions and you're linking an API gateway to your function.
handlertells you where to find your function
eventsaccepts an array of values where you can specify how you want to trigger the function call, here we are using the
httpApi(API Gateway) to trigger the function which in turn asks you to specify the API path and method to invoke the function.
In order to deploy this function all, you have to do is run the command down below.
now if you go to your lambda console you should see that your function 's deployed.
Will share how to create a DynamoDB and S3 instance in the next post.
Thanks for your time.