News
BackImports and Data Sources in Terraform
"Infrastructure as code" with Terraform normally means that you first define the infrastructure you require in config files and that Terraform then uses your specifications to create the infrastructure in practice. However, thanks to "import" functionality, you can also include existing resources in your Terraform setup for further management. In addition, "data sources" can be utilized to read and use further values that relate, for example, to resources in separate Terraform setups.
Many projects "evolve" rather than following a plan
IT projects frequently start out small. This approach is often intentional in order to validate certain hypotheses quickly using a process that is as lean as possible and to continuously adapt the project in short iterations. And sometimes supposedly short-term experiments establish themselves and over time grow into an important product or tool. At cloudscale.ch, we make it particularly easy for you: within the shortest amount of time and with just a few clicks of your mouse, your first cloud servers are ready and can be changed just as quickly and easily when required.
It goes without saying that we also support the creation and management of your cloud infrastructure with Terraform. However, the things that provide consistency and efficiency in an established project would often represent nothing more than unnecessary overheads if applied in the initial phase. If the project then grows, you may at some point realize that a tool like Terraform would actually have been worth it. Given that in many cases you will not want to start from scratch again, the import function for existing resources means that, fortunately, you do not have to do so.
Import existing resources into Terraform
The starting point for Terraform is your configuration, which is where you define all the resources, such as the cloud servers that you need for your project along with their properties. Once these resources have been created, Terraform keeps a "state" in order to remember which resources in the real world belong to which definitions. If your specifications or reality change, Terraform can detect this and recreate the desired state during the next terraform apply
.
To include an existing resource in your Terraform setup at cloudscale.ch, simply define it as usual in your Terraform config. Instead of then having it newly created with terraform apply
, just use terraform import
to link your definition to the existing resource, which you specify by means of its unique ID. You will find the details required for each resource in the documentation for our Terraform provider. By then running terraform plan
, you can check whether Terraform still detects differences between the definition and the actual resource. Keep adapting the definition, if necessary, until Terraform no longer detects a need for change.
Things are special with regard to SSH keys, which are provided to a newly created cloud server by means of a metadata server and config drive and incorporated by cloud-init during the first boot up. As the handling of SSH keys is limited to this initial configuration, Terraform cannot read them from our API and store them in its state at a later point in time. With regard to importing servers, this means that you must not specify SSH keys; otherwise Terraform would detect a need for change every time, which would result in the server being deleted and newly created.
Additional values with data sources
At cloudscale.ch, you can import almost all your existing resources into your Terraform setup as described above, with the only current exception being "custom images". You may, however, not want an import, for example if the resources in question are already part of another Terraform setup. In order to still be able to access attributes of such resources and to use them e.g. in the definition of further resources, you can use data sources.
Data sources make all the important properties of your cloud resources available in Terraform. This means that servers managed by Terraform can, for example, adopt the flavor of a server outside your Terraform setup or be defined to have an interface in a manually managed private network.
Various "arguments" are available in order for you to access data sources. Use one or several of them to filter for the resource you are looking for. The values that can be used as an argument and other "attributes" of the resource are then available to you for further use in your Terraform setup. You will also find all the important information relating to the use of data sources in our documentation.
Terraform differs from conventional configuration management systems in that it allows an automated setup of your infrastructure as a "greenfield project" without you having to prepare servers or networks. In practice, however, many projects do not develop and evolve in the way that hindsight deems best. At cloudscale.ch, you can now, with the help of imports and data sources, integrate "legacy" resources of this kind into your Terraform setup as if they had always been there. Avoid having to start from scratch again when "tidying up" evolved setups and instead use your energy to advance your project.
Create order without the waste,
Your cloudscale.ch team