Cuando trabajas en grandes entornos gestionados con terraform, es muy probable que tengas que trabajar a su vez con diferentes versiones de terraform, según requieran cada unos de los proyectos.
En lugar de descargar las diferentes versiones que necesitemos, renombrando los ejecutables, moviéndolos a algún directorio dentro del PATH y jugando con enlaces simbólicos, podemos símplemente usar el maravilloso tfenv.
Descarga e instalación
Para empezar a usar tfenv, lo único que necesitaremos será clonar el repositorio (por ejemplo en nuestra home) y crear un enlace simbólico para que tfenv esté disponible en el PATH.
1 2 3 4 5 6 7 8 9 |
[adri@adri ~]$ git clone https://github.com/kamatama41/tfenv.git ~/.tfenv Cloning into '/home/adri/.tfenv'... remote: Counting objects: 716, done. remote: Total 716 (delta 0), reused 0 (delta 0), pack-reused 716 Receiving objects: 100% (716/716), 102.65 KiB | 0 bytes/s, done. Resolving deltas: 100% (449/449), done. Checking connectivity... done. [adri@adri ~]$ sudo ln -s ~/.tfenv/bin/* /usr/local/bin |
Trabajar con diferentes versiones de terraform
Para trabajar con diferentes versiones de terraform, primero deberemos instalarlas con tfenv. Esto es tan rápido y fácil como ejecutar tfenv install y la versión deseada.
1 2 3 4 5 6 7 8 9 10 11 |
[adri@adri ~]$ tfenv install 0.9.2 [INFO] Installing Terraform v0.9.2 [INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.9.2/terraform_0.9.2_linux_amd64.zip ######################################################################## 100.0% [INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.9.2/terraform_0.9.2_SHA256SUMS tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification Archive: tfenv_download.og79q6/terraform_0.9.2_linux_amd64.zip inflating: /home/adri/.tfenv/versions/0.9.2/terraform [INFO] Installation of terraform v0.9.2 successful [INFO] Switching to v0.9.2 [INFO] Switching completed |
1 2 3 4 5 6 7 8 9 10 |
[adri@adri ~]$ tfenv install 0.10.7 [INFO] Installing Terraform v0.10.7 [INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.10.7/terraform_0.10.7_linux_amd64.zip ######################################################################## 100.0% [INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.10.7/terraform_0.10.7_SHA256SUMS tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification Archive: tfenv_download.Eze16S/terraform_0.10.7_linux_amd64.zip inflating: /home/adri/.tfenv/versions/0.10.7/terraform [INFO] Installation of terraform v0.10.7 successful [INFO] Switching to v0.10.7 |
Versión por defecto
Con tfenv use podemos indicar la versión que queremos usar. Con «latest» símplemente estaremos indicando que queremos usar la versión más actual, por defecto.
1 2 3 |
[adri@adri ~]$ tfenv use latest [INFO] Switching to v0.10.7 [INFO] Switching completed |
1 2 3 |
[adri@adri ~]$ tfenv list * 0.10.7 (set by /data/repos/my-example-repo/.terraform-version) 0.9.2 |
Así, si por ejemplo la versión más actual de las instaladas es la 0.10.7 y posteriormente instalamos una aun más reciente, la nueva (y más reciente) pasará a ser la versión usada por defecto.
1 2 3 4 5 6 7 8 9 10 11 |
[adri@adri ~]$ tfenv install 0.11.2 [INFO] Installing Terraform v0.11.2 [INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.11.2/terraform_0.11.2_linux_amd64.zip ######################################################################## 100.0% [INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.11.2/terraform_0.11.2_SHA256SUMS tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification Archive: tfenv_download.rIFcOt/terraform_0.11.2_linux_amd64.zip inflating: /home/adri/.tfenv/versions/0.11.2/terraform [INFO] Installation of terraform v0.11.2 successful [INFO] Switching to v0.11.2 [INFO] Switching completed |
1 2 3 4 |
[adri@adri ~]$ tfenv list * 0.11.2 (set by /data/repos/my-example-repo/.terraform-version) 0.10.7 0.9.2 |
Lanzando terraform
Al lanzar «terraform», estaremos haciendo uso de la versión seleccionada en «tfenv». Si necesitaramos, para ese proyecto, cambiar a otra versión, símplemente lo haríamos con un «tfenv use
1 2 3 4 |
[adri@adri ~]$ tfenv list * 0.11.2 (set by /data/repos/my-example-repo/.terraform-version) 0.10.7 0.9.2 |
1 2 3 4 5 |
[adri@adri ~]$ terraform --version Terraform v0.11.2 Your version of Terraform is out of date! The latest version is 0.11.4. You can update by downloading from www.terraform.io/downloads.html |