Jina How To

Jina ❤️ Terraform

Easily define your Jina infrastructure via code

Susana G
5 min readNov 5, 2020

Hi everyone!

Recently I’ve been working with Terraform and tinkering with it to see how we can integrate it with Jina, so I thought to share with you all that I got.

First thing was to see what Terraform is and why everyone is so hyped about it. According to their website:

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

So that’s a lot of fancy words to say it’s a way for you to define your infrastructure via code. No need to go to AWS console and define every part of the infrastructure there. This comes in very handy since it’s easier to debug when you have everything in one script so you can see all the details in one single place.

Cool! Nice, sold, what now?

Let’s tweak our South Park example and instead of running our Docker image locally and then checking the results with Curl, let’s set everything on an AWS instance and use that URL. And of course, because otherwise this post wouldn't make sense and I could be napping instead, let’s set up the infrastructure with Terraform.

What Will You Need?

  1. Terraform
  2. An AWS account
  3. The South Park Docker image

The first thing is to install Terraform, and the installation will depend on your OS. I’m using macOS and I installed it via Homebrew:

brew install terraform

Once you have it installed, you need to configure your AWS, so on your console the first command you’ll run is:

aws configure

This will ask for the credentials of your AWS account. Once you set this, you won’t need to specify keys through code, which is good because that’s a bad practice and you will end up with your account in quarantine…this might have or not have happened to me.

Ok! we have all the setup ready and we can start to write our Terraform file.

What’s Happening? What’s Going to Happen?

  1. Create an image repository in AWS and push our South Park docker image
  2. Create an ECS Cluster & Task
  3. Create an ECS service & load balancer

Create an Image Repository in AWS and Push Our South Park Docker Image

We already have a Docker image with the South Park example so we don’t need to create a new one.

So the first thing is to create our Terraform script. It can be called whatever you want but with a .tf extension:

The first part is to create a provider: With Terraform you can use AWS, Azure, Google Cloud Platform or many others, or you can even write your own if you feel like life is too boring and you are an expert in Go. Whatever floats your boat. We’ll use AWS for this example. The second part is to create our repository and we just need to set the name for it. The tags are always optional.

This is the simplest Terraform file you can have. If you run this, Terraform will create an AWS repository for you, so let’s do that.

You can run:

terraform plan

And this will print a plan with all the resources that will be created, modified, or deleted. In this case, we are only creating a resource so it will show that. This command is always nice to do for a sanity check, so you see what will happen before it happens, and if we agree with this we run the script with:

terraform apply

After we run this, if we go to our AWS console, we’ll see that an sp-repo has been created:

Great, we have our repository ready. But at the moment it’s empty, so we need to push our South Park Docker image into it. To do that, click on the repository name, and then in the upper right corner you’ll see a button to “View Push commands”:

We’ll use that information to push our Docker image:

Follow the steps that are shown there except step 2: We don’t need that since we already have our Docker image. After you’ve done that, you should have the South Park image in your repository. Yay.

Create a Cluster and Task

The next step is to create a Cluster:

Ok, for the task we need a few more details:

For this, we will set the image URL of our Docker image and set the ports to 45678, the same that the South Park example is using.

If you run terraform apply you should see a new cluster with a task attached to it.

Create a Service and Load Balancer

Ok, now we want to create a service that will use the task we just created as a blueprint. Here we’ll set the name of the service, a reference to our cluster, and to the task we just made. We have only one container so we set desired_count to 1

We need a load balancer as the access point to our container. The load balancer needs a security group, so we need to create that too:

The idea is that all traffic will be received by our load_balancer_security_group (ALB), and the service_security_group will receive only from the ALB.

Just for debugging, we also have an output to show us the URL to access our South Park example:

If you run terraform apply you’ll have now a cluster with one task and one service running on it:

Also in your terminal, you’ll see the output with the URL to check our results with CURL as in the South Park example.

That’s it! But just before you go and nap, do a terraform destroy if you won’t need it anymore, so you release all the resources and don't get charged extra in AWS if you’re using the free version.

--

--