[ad_1]
Let’s quilt an API use case with Terraform HCL & Python

These days, many of the infrastructure code is finished thru Terraform. It’s been there for relatively some time, has a robust neighborhood, and it’s multicloud. On the other hand, issues begin to be tough in the case of checking out Terraform code. Whilst Terraform makes use of its personal language (HCL), its backend is written in Golang.
A excellent trend for Terraform modules exams is terratest, however as you might have guessed, it is very important write those in Golang. Right here we’re going to show off how we will be able to use simple Python with an impressive but easy library tftest on current Terraform HCL code.
Tftest is a small Python library from Google. It allows you to do Terraform movements (plan|deploy|wreck) programmatically and retrieve the execution plan, output variables, and many others.
The ability of tftest lies with the possible combo with pytest
. But even so, Python has in point of fact excellent SDK make stronger at the other cloud suppliers, which makes it great for checking out cloud infrastructure.
Our setup will contain a easy Cloud Run API (serverless container runtime), however you’ll observe the above manner for any infrastructure you deploy!
Easy infrastructure check
Within the first instance, we will be able to do a easy check to get our fingers round tftest
. Let’s check out the next
- Create a
plan
fixture - Assert that the output identify of our container symbol is what we anticipated
- Test the output variables are anticipated
The plan fixture merely appears at which terraform module/listing we wish to glance up (and carry out the observe
command) and make the output variable to be had as an object.
As you’ll see, you’ll simply retrieve any outputs and/or variables to accomplish any exams that you desire to!
Complicated e2e infrastructure check
We’re going now to do an e2e check that might do the next:
- Deploy the API on Cloud Run
- Get the URL of the deployed provider
- Generate an Auth consultation able for request
- Carry out the request and assert the reaction
- Wreck the API
The primary 3 issues can also be once more installed a fixture to stay our checking out serve as on the minimal and so that you can reuse this one for different requests.
This time, our fixture will do an observe
and a wreck
on the finish of the check.
It is going to additionally generate an auth consultation in line with the deployed URL to be allowed to accomplish a request. We can wrap up this auth consultation thru a request_wrapper
serve as.
Let’s put this fixture in conftest.py
:
And right here’s our check report that might carry out the request and assert the reaction:
This report is stored at a minimal, and the fixture can also be simply reused for different endpoints checking out.
This setup works nice for any serverless parts (like Cloud Run) that don’t have an extended chilly get started. Some Cloud services and products can from time to time take as much as 15–20 mins to be able, making it tedious to incorporate them as a part of a CI pipeline.
The setup offered right here works nice on an current terraform codebase. On the other hand, if you’re beginning a brand new challenge, there are extra suitable answers.
Terraform already has in beta Terraform CDK, which lets you without delay use Python (or every other programming languages supported) to claim your infrastructure, which makes checking out a lot more uncomplicated.
Pulumi could also be a really perfect candidate, and it’s extra mature at the CDK aspect.
If you wish to paintings with AWS handiest, you’ll additionally use AWS CDK, however you lose some great benefits of making an investment wisdom in an IAC framework that isn’t vendor-locked.
In any respect, it’s great to in any case be capable of use a typical programming language to additionally set up infrastructure as you’ll without delay leverage all of the checking out toolkits incorporated!
Satisfied checking out.
Hyperlink to the entire demo GitHub repository right here.
Need to Attach?
Observe me on YouTube or LinkedIn
[ad_2]