Everyone keeps talking about âInfrastructure As Codeâ but when you read about it, you feel stuck and frustrated. A long time ago a lot of your cloud infrastructure was created manually and itâs now all a bit of a mess.
Infrastructure As Code feels like a million miles away for youâŚ
If that sounds too real for you, fear not. Because there is a way to get a state-of-the-art Infrastructure As Code workflow applied onto your existing infrastructure, and itâs easy. Let me show you how.
With Terraform itâs possible to bring existing infrastructure under code management in a safe, and incremental way. And today weâre going to go through the three steps youâll need to take if you want to apply Terraform Infrastructure As Code to your existing infrastructure.
By the end of this article youâll understand the 3 steps to get started with Terraform Infrastructure As Code on existing infrastructure.
Before we jump in, if youâre not familiar with Infrastructure As Code, check out this article first: Infrastructure As Code: A Quick And Simple Explanation. Or if you want a birds eye view of Terraform first, check out: Learn The 6 Fundamentals Of Terraform â In Less Than 20 Minutes.
Terraform in under 200 words
And before we get to the step for migrating manually created infrastructure, itâs important weâre on the same page about Terraform. Because Terraform is the tool weâll use today to bring our existing infrastructure into management by code.
Terraform is an open source declarative Infrastructure As Code tool. Itâs a third party tool, so it operates on all cloud providers: AWS, GCP and a whole host of other cloud tools. Itâs also a CLI, so you simply install it on your machine or a remote build server in order to use it. And lastly, Terraform operates with a concept of state. State is your representation of the cloud resources you want to manage with Terraform.
Because Terraform can be installed on any machine, Iâm pretty sure you can use it. And because it works on all cloud providers â even your own data centers â there simply should not be many reasons why you canât implement Terraform. And thatâs why Iâve chosen it as the tool to help you refactor your existing infrastructure.
Okay, and with that out of the way, letâs get to it! Here are the three stepsâŚ
Step 1: Begin To Understand Terraform
Terraform Up & Running Book
Firstly youâll need to clear the mist of uncertainty. Weâll need to know what Terraform is, and how itâs capabilities help us manage our existing manually created infrastructure. At this point weâre trying to move from âI donât know what I donât knowâ to âI know what I donât knowâ. Weâre not trying to be an expert, but we should first get a lay-of-the-land by familiarising ourselves with the concepts of Terraform.
At a bare minimum, Iâd suggest that you become familiar with: state, the language of Terraform (HCL), how Terraform manages resource dependencies, the plan and apply commands, file structure and providers. If youâre keen to jump into this straight away, I have already written these concepts up in the article: Learn The 6 Fundamentals Of Terraform â In Less Than 20 Minutes.
But whilst the article gives you a birds-eye view, thereâs only so much I can teach you in a single article. For a more comprehensive view Iâd suggest you grab yourself a copy of Terraform Up & Running. The book was written by Yevgeniy Brikman, founder of Gruntwork and itâs a genuinely very good walk through of all the Terraform concepts. You donât need to read it cover to cover, but at least start to familiarise yourself with the terms and concepts.
Note: If youâre into books, learning and all that jazz, be sure to check out: The Big List Of Cloud Native Engineering Resources.
And once youâve started to wrap your head around those topics youâll be in a great place to move to step twoâŚ
Step 2: Practice Terraform On Greenfield
Planning a Terraform resource
âGive me six hours to chop down a tree and I will spend the first four sharpening the ax.â â Abraham Lincoln
I know at this point itâll be really, really tempting to simply go ahead and apply what you learned in step one straight away and try to get it working with your existing infrastructure. But, I do recommend at this point you apply a little more patience, and instead start practicing on greenfield infrastructure.
Why? Because learning most technology can be tricky, and itâs important that you start to understand the nuances, including how to un-pick any situations you get yourself into. Thatâs why I suggest you start by building out on greenfield first, just to get a feel for how Terraform works.
To do this, pick some infrastructure that you already have manually configured, and start trying to provision new resources using Terraform. But Iâm not simply suggesting you have a play, youâll want to pay specific attention to the following areas, as theyâre whatâs important when it comes to refactoring your existing infrastructure later.
Understand: How, When & Why Of State
State is how Terraform knows what resources you want to manage. Terraform will convert code into state when you apply your changes. The concept of state is the most important area youâll need to know for refactoring your existing infrastructure. So take note of when Terraform alters state and why.
Understand: The Terraform Syntax
As youâre building out your project, pay attention to the Terraform syntax. Pay special attention to how the resource blocks are configured, and try to notice the similarities in how different resources are configured. Start to imagine how the syntax will apply to your infrastructure.
Understand: Resource Dependencies
And the last big thing is: youâll want to get comfortable with is how Terraform handles dependency resolution. Try to create at least a few resources that link to each another. For instance, create a load balancer that references a server, or a domain name that references an asset. Again, start to imagine the dependencies your current manually created infrastructure has.
Generally speaking, at this point you want to make some mistakes. Youâll want to push boundaries and see what the repercussions are. Be curious. Try, for instance to cancel a Terraform apply mid way through and see what happens to your state file. You want to be building confidence that you can get yourself out of sticky situations. Which will be important when we come onto applying these ideas to your existing infrastructure.
And by the end of step two you should understand the main concepts and be pretty confident with using Terraform to create basic infrastructure. Now itâs time to move onto the part that youâve been waiting for, and thatâs bringing your existing manually created infrastructure under management with Terraform.
Step 3: Import Terraform State
Terraform Docs For Importing An S3 Resource
Terraform manages infrastructure through state. If resources are recorded in state, Terraform assumes you want to manage them, if theyâre not included in state Terraform assumes you donât care about them. When youâre starting out you wonât have any resources managed under Terraform state. So the first step with migrating infrastructure into Terraform is to âimportâ them.
How To Import Terraform State
If you head to the Terraform docs for your favourite resource (honestly, at this point I feel me writing about creating S3 buckets will be a memeâŚ). Youâll see that at the bottom of the document it shows you how to import that specific resource.
Importing is simply a command that is made to your resource, to fetch down the resource metadata and store it in state. The state that would have been created if youâd provisioned with Terraform in the first place. All you need is the resource name, and itâs unique identifier. The identifier will change though dependent on what cloud youâre operating on, for instance.
The Two Requirements To Import State
But wait, there is something I forgot to tell you. Importing state requires two things before you get started (the keen eyed among you might have spotted the two arguments in the above command image). And those two things are:
- A resource block â A definition of the resource youâre importing
- State â The state of the resource
Before you can begin importing state, you must create a resource block.
Why? Because your resource block tells terraform which piece of your code to assign to that state. Imagine you have two servers in Terraform, it wonât know which server is the one for which youâre importing state, so youâll have to tell the import command explicitly.
For instance, if you were importing a static S3 bucket, called mywebsite youâd need to have your terraform config setup with a resource corresponding to the name mywebsite which youâre importing. Having your named resource allows Terraform knows to assign the imported state to that resource.
Protip: Donât worry about writing the resource and itâs attributes perfectly. Just create the resource block. For instance, if youâre importing a server, just add a server resource block. Donât bother configuring size, tags, etc. Because you can run terraform plan after importing your state, which will tell you the differences. And you can simply copy any differences from the output into your code. Trying to guess the attributes up front is hard, time consuming and inefficient.
The Process Of Importing State
I appreciate that might be a lot of new concepts to digest. So in summary the process of importing state looks like thisâŚ
- Write a rough resource configuration block for the resource (in code)
- Import your resource with the import
terraform importcommand - Use
terraform planto check the differences between your code and state - Make changes to align your resource attributes with the existing resource
Protip: This little tool, called Terraforming lists out all your resource configuration blocks, which can save you time!
Terraform All The Infrastructure!
And thatâs it, those are your three steps you for moving your existing infrastructure under management by Terraform. Youâll start by understanding Terraform, then using Terraform on greenfield before finally using Terraform to import state. Follow these three steps and youâll be up and running with Terraform in no time.
I hope that helped put you on the right track and gets you on the road to implementing Infrastructure As Code. When you get the right setup it becomes far easier to manage your infrastructure this way. As soon as you start, youâll wish you did it sooner â I bet you.
Speak soon Cloud Native friend!
The post 3 Steps To Migrate Manually Created Infrastructure To Terraform appeared first on The Dev Coach.
Lou is the editor of The Cloud Native Software Engineering Newsletter a Newsletter dedicated to making Cloud Software Engineering easier, every 2 weeks you get news and articles that cover the fundamental topics of Cloud Native in your inbox.




Top comments (0)