Terraform
Installing Terraform
On a Ubuntu machine run the following command
# register key
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
# add repo
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
# install with apt
sudo apt install terraform
Setting up provider
Create a directory with any name and
cd
to the directoryCreate a file named
providers.tf
with the following content. This will enable the OpenStack providerproviders.tf
# Define required providers terraform { required_version = ">= 0.14.0" required_providers { openstack = { source = "terraform-provider-openstack/openstack" version = "~> 1.35.0" } } } provider "openstack" {}
Declare the cloud information. It is recommended that you refer to Using the OpenStack Command Line Interface . You can also declare it in the
providers.tf
however, you will have to declare your login details in plain text which is not secure.Run
terraform init
$ terraform init Initializing the backend... Initializing provider plugins... - Reusing previous version of terraform-provider-openstack/openstack from the dependency lock file - Using previously-installed terraform-provider-openstack/openstack v1.35.0 Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
Applying config via terraform
To create a configuration you need the deploy.tf
for your configuration and variable.tf
for the variables in the provider folder
I have create an example that will create a set of instances with a volume attached to it.
deploy.tf
variables.tf
To change the variable you can either change the default in variable.tf
or by passing the -var "<variable-name>=<value>"
flag during terraform plan
or terraform apply
.
On thing to note is that you should create ssh keys via the web interface or OpenStack clients and reference it in terraform since terraform will store configuration in plain text which will expose your ssh key.
To save the plan to a file you can use the -out=<path>
flag. This will make sure when you apply the plan, terraform uses the correct plan
After you have edited the files, you can run
terraform plan
to preview the changes
e.g.
terrafor apply plan
to apply the changes
e.g.
Reference
You should refer to Terraform Registry for details on how to use the providers
Here are some of the common providers
Provider | Function |
---|---|
openstack_compute_instance_v2 | Create instance |
openstack_networking_secgroup_v2 | Create security group |
openstack_networking_secgroup_rule_v2 | Create security group rule |
openstack_networking_network_v2 | Create network |
openstack_networking_floatingip_v2 | Get a floating IP from allocated pool |
openstack_compute_floatingip_associate_v2 | Associate floating IP to instances |
openstack_containerinfra_clustertemplate_v1 | Create magnum cluster template |
openstack_containerinfra_cluster_v1 | Create magnum cluster |
openstack_blockstorage_volume_v2 | Create a new volume |
openstack_compute_volume_attach_v2 | Attach volume to instances |
openstack_lb_loadbalancer_v2 | Create Load balancer |
openstack_lb_listener_v2 | Create Load balancer listener |
openstack_lb_pool_v2 | Set method for load balance charge between instance (e.g. round robin) |
openstack_lb_member_v2 | Add instance to load balancer |
openstack_lb_monitor_v2 | Create health monitor for load balancer |