Intro to Infrastructure as code (Serverless framework)
Tags: serverless, aws, beginners, javascript
Published: 2022-11-18 14:16:05

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.


## What is infrastructure as code?


![IAC concept](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jj61dv63oh5f2yrf1py3.png)



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

## Install AWS CLI

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](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)

- Now check if the AWS-CLI was installed properly by running the following command in your terminal `aws` and you should get a output like the image below.

![Aws-cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4em4yesr4983nin5pehq.png)

## Add new IAM user 
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.

![Add Iam user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t86hklsz0x5o9v4edmvc.png)

- Select add users 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e91lwechasnz0w5n2obo.png)


- give the user a name and provide programmatic access as we will be creating resources through code and hit next.

![IAM user permission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rl3fm01egeps2w0mkcq8.png)

- 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktxzzrir85067byoxcpt.png)

- 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1alnhfssx7s4km5tmlh.png)

- 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.

```
aws configure
```

## Setup serverless framework

- 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.


## Setup your lambda function with code

- Run `serverless` in 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.

![project templates serverless](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7z3mjc5cwf5497acho7s.png)

- select no for all the following options and continue.

- now cd into your serverless project directory and you should see the following:

![serverless framework folder dir](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmqd7c2y329o0cy8w208.png)

There are 2 main files which needs to be explained
 
1. The `handler.js` file.
2. The `serverless.yml` file.

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.

![serverless iac code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddcvc3ypzynytnlk5tyv.png)



YAML file store key value pair data just like JSON.

- In the `service` key 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 "-".)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlptzj9pa611fui9jw5q.png)

- The `frameworkVersion` specifies which version of serverless framework we are using.

- The `provider` key is used to configure your cloud provider 
   - `name` here specifies which cloud provider you're using.
   - `runtime` specifies which programming language you're using.

(**FYI:** the provider block might look different for other cloud providers you look it up [here](https://www.serverless.com/framework/docs/providers))

- Under the `functions` block you are telling serverless framework where to find your functions and you're linking an API gateway to your function.
  - `handler` tells you where to find your function
  - `events` accepts 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.

## Let's deploy the function

In order to deploy this function all, you have to do is run the command down below.

```
serverless deploy
```

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.