Terraform question - regarding pre-existing infrastructure

I’m looking to try out Terraform and I keep seeing a lot of basic examples, but I’m concerned about what’s already in place. The most basic terraform plans seem to just add new resources where nothing ever existed. I’m scared that if I start small, some of my existing servers, scripts, etc. will be deleted. Can Terraform be used like CloudFormation? Like add what you need for specific projects and manage it per project or task? My rationale at the moment is we have a few servers, etc. that can’t just be replaced overnight with code. They’re custom and our current deployment is a mess. I’m trying to start somewhere on our road to complete automation.

TLDR; I want to start integrating and automating via terraform, but I’m scared it may overwrite existing infrastructure. I want to add on demand.


Related discussions:

Terraform will fail with an error if you try to create new resources with conflicting names/IDs. You have to import existing resources into terraform manually to be able to manage or destroy them. You should be okay, but I’m not an expert by any definition.

hey so there are a couple things that would really help you out with confidence here. You really just need to have a better understanding of how terraform works.

Look into what the terraform state file is and what it does

Have an understanding of what is happening at plan and apply phases and how that relates to the state file

once you understand these you can start to think about how to split your resources into more than one state file in a meaningful way

  1. Yes, Terraform can be used for specific projects or tasks. Just because you’re using Terraform for some things in a cloud environment doesn’t mean you have to use it for everything. You could spin up some servers using Terraform without affecting the servers you spun up outside of Terraform. Though once you get started, you’ll find it in your best interests to eventually get everything into Terraform, but don’t let that stop you from starting.

  2. Look into terraform import. It’s let’s you import existing infrastructure into Terraform management without recreating it. That VPC that someone created manually two years ago? Import it, and it’ll be as if it was created by Terraform. You can do this with your legacy pet servers too.

So it sounds like getting the current state is somewhat of a first step, correct? Assuming that the state is current and not superseded, I should be able to just add-on or change and we should be fine. Also, are there controls that do a check on what is being changed (altered, deleted, or otherwise) so I can back out if necessary before pulling the trigger on the ‘apply’?

Sorry for the super-noob questions, I’m literally just getting started.

I’ve just been learning about import. Is it correct that I have to manually put the resources definitions into my configuration?

So it sounds like getting the current state is somewhat of a first step, correct? Assuming that the state is current and not superseded, I should be able to just add-on or change and we should be fine.

This is definitely a valid first step. See the import section at the bottom of most terraform resource pages. Technically speaking, all import does is add entries to your state file by making read only calls to the AWS api based off the identifying information (usually a name or ARN) you supply. Note that import only changes the state file – it doesn’t author the configuration for you.

Also, are there controls that do a check on what is being changed (altered, deleted, or otherwise) so I can back out if necessary before pulling the trigger on the ‘apply’?

Yep! You can use terraform plan with the -out parameter to create a plan file that you can thoroughly review. Then when you go to pull the trigger, you would use this plan file.

Hope this helps

Yes, if you want to import them and take over managing them as code you do.

You can import existing infrastructure with the Terraform import command. Make sure your Terraform code defines the resources exactly as they exist today. In some cases, it may be more practical to rebuild the resources from scratch in Terraform and migrate data (eg to correct architectural mistakes in your hand crafted infrastructure). Terraform will not blow up resources that it doesn’t know about.

There is also an open source project called terraforming that can generate TF source files for your existing infrastructure, but it is limited (not all AWS services are supported). Also, the code it generates will not have variables and modules. It’s a good way to see what your code should (sort of) look like though.

It’s up to you how you structure the Terraform code. You can have one set of TF code for the whole account, or you can have separate TF code for separate projects within an account. If you keep the code independent you’ll want to make sure they don’t step on each other (eg they both try to create the same resource). Personally, I would use separate AWS accounts and one set of TF code for each account. If there are any common designs you need between the accounts, use TF modules and include those modules as needed.

I HIGHLY recommend you figure out the Terraform state file before you go too far. The state file is something that newbies tend to not to take seriously and then get bitten later.

If there will be multiple users, or multiple computers (e.g. your workstation and a build server) running Terraform, you need to get remote state figured out. Terraform lets you put the state file in an S3 bucket with DynamoDB for locking. It’s not too hard when you figure it out, and it the best solution for most use cases on AWS.

If you are not doing those things yet (eg you are only running Terraform from your workstation), at least make sure the state file is checked into version control. As soon as a second person or computer is involved, get remote state figured out.

https://github.com/dtan4/terraforming

‘terraforming’ is a pretty nice project that can help with writing your files from your existing resources though.

https://github.com/dtan4/terraforming

Terraform will raise an error if there’s a conflict between a new resource and an existing resource. So, it’s pretty safe to run it in an environment where you already have infrastructure.

Basically, terraform only cares about it’s inventory. If a resource is not in it’s inventory, the resource needs to be created even if the resource already exists on AWS. The create will fail in any case where Amazon doesn’t allow a conflict.

To manage existing resources, you need to import them. Importing can be a bit risky though, since Terraform can try to destroy the resource if it is not defined in the existing configs. It’s very important to plan changes before applying them after an import.

You can reference existing resources using data sources. In these cases, you can get the properties of the resource, but Terraform will not actually manage it.

Yes, importing them via the terraform import command only imports the resource(s) into the Terraform State file.

It does not automatically generate or populate the .tf file based on the resource.

I would recommend against importing all your existing resources into terraform as a first step, especially if they are as you say custom and not easy to unravel into code. Importing over time is probably a better way to go. You can refer to existing resources using “data” sections of the AWS provider and terraform will not try to manage the lifecycle of these (e.g. if you want to refer to an existing security group, you can bring it in to your terraform configs by name and then use the sg ID in contexts that require that without having to hard code the ID).

As you get the hang of the tool you will find that the process of editing config files and applying them can be highly iterative, almost conversational. You might import and existing resource and the see that terraform is planning to destroy it because the config is implying that an immutable value needs to be changed (like the AMI of an existing instance)…no problem, just adjust the config and recheck the plan. In this way you will slowly replicate the details of that resource in the code of the config.

Just FYI… Terraforming was really nice, but it is getting cobwebs… https://github.com/dtan4/terraforming/issues/423

Just FYI… Terraforming was really nice, but it is getting cobwebs… https://github.com/dtan4/terraforming/issues/423

Perchance did you try it? I (very briefly) tried the docker container, but had one small task after another. Had to create a quay account to get the container, had some struggle with file mapping between container and host (my bad, forgot about this). I think I may have gotten it to start but didn’t get far (again, my bad..ran out of time).

Anyway, I would feel better about trying it again if someone else had a good experience with it…

Oh this is such helpful advice. Makes me want to go give it a try, just one or two resources at a time. Thanks

I never used the docker container. I just have terraform installed locally (with the excellent terragrunt wrapper). I don’t really need the levels of indirection on my local machine that docker provides for this sort of thing.

This pattern is what I do now, and using data resource is a nicer, less baroque way of accessing existing resources. Yes, importing them later is still a good idea, but you don’t have to get stuck in so many chicken-and-egg dependency hells with this pattern. :slight_smile: