Setting up AWS Infrastructure Using Terraform for Beginners
Published: 2023-01-09 20:00:35

---
title: Setting up AWS Infrastructure Using Terraform for Beginners
published: true
date: 2022-12-18 21:30:00 UTC
tags: 
canonical_url: https://dataroots.io/research/contributions/tips-and-tricks-on-how-to-setup-an-aws-infrastructure-with-terraform-for-beginners
---

_After one month of training at dataroots, some starters work on the internal project. The project of the Rootsacademy 2022 Q3 class consists of making an end-to-end solution for inferring information from traffic images. It goes without saying that this end-to-end solution requires infrastructure. In this post, we go through the infrastructure along with some tips and tricks to deploy AWS infrastructure using Terraform. Our aim is to explain things at a high level, such that you, the reader, can get a better understanding of how to achieve the same result, even without prior knowledge of AWS and Terraform._

## Crash Course on AWS Services in Architecture

![](https://dataroots.ghost.io/content/images/2023/01/smart_mobility_infra-1.png)
_(1) The final architecture of Rootsacademy Q3 project_

The above image displays final architecture. We need several services in AWS to realize the solution. In this section we give a short explanation of the used AWS services and how we used them in our architecture.

### Region, VPC and Subnets

AWS services are deployed inside a region, the region can be seen as the physical location where services are running. AWS gives us the possibility to create private networks using VPCs. These VPCs can be split in several subnets, which allows us to control the resilience of deployed resources inside this VPC.

For our project we deployed everything inside a single subnet, but ideally you would maximize the resilience of your architecture.

### Amazon S3

S3 is the object storage service within AWS and introduces the concept of buckets, on which objects are stored on.

We have two buckets running inside this architecture. The first bucket mimics a street camera that takes images of its surroundings, and the other one saves the annotated images that are made by the model.

### Amazon Sagemaker Notebook and Model

Sagemaker is the service on AWS that contains all your machine learning needs. It has a lot of features ranging from training models to deploying endpoints to it.

We use Sagemaker Notebook for feeding images to the API Gateway in order to get an inference. The Sagemaker Model (deployed as image on Amazon ECR) annotates the images and gives it back to the Lambda.

### Amazon API Gateway

API Gateway allows for creation and managing APIs. API Gateway is situated between applications using the API and services that implement the functionality of the API.

In our case, the API Gateway implements a REST API POST method. The Sagemaker Notebook calls the POST method with an image and triggers the Lambda, and after successful inference, the method returns a body containing the annotated image.

### AWS Lambda

Lambda is a FaaS service within AWS for running code using a specific runtime.

Our Lambda code (deployed as image on Amazon ECR) connects several components with the following functionality:

- Inquiries Sagemaker for prediction
- Writing annotated image to the s3 bucket
- Writing metadata and annotated image to RDS
- Passing the annotated image back to API Gateway

### Amazon RDS

The relation database of Amazon is Amazon RDS.

Our use for RDS is to save the metadata of the input images and their Sagemaker inferences.

## Infrastructure as Code (IaC), What's in a Name? 

Readers familiar with AWS Cloudformation might have heard of IaC. Terraform's functionality is somewhat similar to Cloudformation, but Cloudformation is only for AWS. Terraform is cloud agnostic and can be used to provision infrastructure on other major cloud platforms.

All of the above AWS services and their configurations can be specified in Terraform as code. Instead of creating all those components manually using the AWS Console, Terraform eases the process of deploying infrastructure on AWS by writing declarative code which represents resources and their configurations. It also links those resources to physical resources on AWS (state file), so it can detect changes in code and adjust the physical resource accordingly.

```
resource "aws_sagemaker_notebook_instance" "notebook_instance" {
  name = "smart-mobility-notebook-instance-${terraform.workspace}"
  role_arn = var.iam_role_arn
  instance_type = "ml.t2.medium"
  subnet_id = var.subnet_ids[0]
  security_groups = var.security_group_ids
}
```

_(2) Example of Terraform code to deploy an AWS Sagemaker notebook instance_

When working with multiple people on a Terraform project, it is recommended to perform what is called a bootstrap. The bootstrap consists of two operations:

- Saving the state file in a s3 bucket, so that the state file can be accessed by your teammates.
- Saving a lock in a DynamoDB table. The lock ensures that only one person can modify the resources listed in the state file to prevent inconsistencies. 

It is important to check if the partition key for the DynamoDB table is called _ **LockID** _. This specific partition key is required for Terraform for recognizing the table.

## Points of Attention

We finish this blog post with some points to pay attention to when setting up a similar infrastructure.

- **API Gateway**

For deploying the _aws\_api\_gateway\_integration_ resource, the uri of the Lambda needs to be in the following format:

```
"arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${lambda_arn}/invocations/"
```

_(3) Specific required format for linking a Lambda to the API gateway_

- **IAM Policies**

Working with IAM policies is tricky. You need to be aware that some services might not have permission to do certain things. In AWS, the default rule is implicit deny. Resources are inaccessible for all kinds of actions performed on it unless explicitly stated by policies. If an action times out, there is a high probability that a policy needs to be set.

- **Terraform workspaces**

Terraform workspaces are useful when working with multiple environments (e.g. development and production). Each workspace has its own state file, making it possible to deploy the same resources for different environments.

- **Github Actions and Github secrets**

It is possible to create a CI/CD workflow using Github Actions that validates the changes in the Terraform code and apply those changes to AWS. Be aware that Github needs AWS credentials to do that, but do not place your credentials on the repository. Github secrets offers a secure way to save your AWS credentials such that it can be used in CI/CD workflows.

- **AWS Console**

It might be easier to create a first version of a resource using the AWS Console. Here you can get a high level overview and idea of the possible configuration parameters. If you can make a resource with your desired configuration, it should not be that difficult to translate it to Terraform code.

## Conclusion 

The cloud & infrastructure side of the Rootsacademy project was an interesting experience to have. It gave us a bit of more experience before starting working with clients. We would like to thank Virginie for organizing this project and our senior colleagues Richard and Emre for their time reviewing our PRs and giving us guidance.

This blog is mainly concerned about cloud & infrastructure, but if you would like to know more about the other elements of this project, we would highly recommend reading the blog posts of the data team and machine learning team.

## You might also like 

[

Tokyo Drift : detecting drift in images with NannyML and Whylogs - Warre Dreesen, Martial Van den Broeck

Detecting drift in your data is very important when deploying models inproduction. It ensures that the performance of your model does not decrease dueto the nature of the input data changing. There are a lot of tools out there formonitoring your data and detecting drift such as Great expectation…

 ![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH5QMTDQUuJijI6QAAAAFvck5UAc+id5oAAANWSURBVFjD7dZZiJVlGAfw33GGaWJomMzItEXbbHGGZoyiRW0RLxLypgutbiuIFiJbQCKKiGihneqiBW+CoGIIuqgkEJexxZRogbLQjJrKaLFi1Jm6eN7Pec93vnOcAaOb+cOB873b83+2//syhf8ZtUNyyuCDreeX3/UfEqg3PhPz8AWGJ0KmOYGyV1Ve1K+Zg2dwCb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NDYrMDA6MDCyFISGAAAAAElFTkSuQmCC)dataroots

 ![](https://dataroots.io/static/0f56f59e505301c5daae0bb424b5edbd/47498/tokyo-header-1.jpg)
](https://dataroots.io/research/contributions/tokyo-drift-detecting-drift-in-images-with-nannyml-and-whylogs)