Category: Terraform

  • terraform multi-version tfenv

    tfenv works like nvm for Node.js — it lets you install and switch between Terraform versions easily.

    git clone https://github.com/tfutils/tfenv.git ~/.tfenv

    Update zshrc
    export PATH=$PATH:/snap/bin:~/bin:~/.local/bin:~/.pyenv/bin:$HOME/.tfenv/bin

    tfenv install 1.8.5

    ➜ ~ tfenv install 1.8.5
    Installing Terraform v1.8.5
    Downloading release tarball from https://releases.hashicorp.com/terraform/1.8.5/terraform_1.8.5_linux_amd64.zip
    ##################################################################################################################################################################################### 100.0%
    Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.8.5/terraform_1.8.5_SHA256SUMS
    Not instructed to use Local PGP (/home/yangy9/.tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
    Archive: /tmp/tfenv_download.5vM1kH/terraform_1.8.5_linux_amd64.zip
    inflating: /home/yangy9/.tfenv/versions/1.8.5/LICENSE.txt
    inflating: /home/yangy9/.tfenv/versions/1.8.5/terraform
    Installation of terraform v1.8.5 successful. To make this your default version, run ‘tfenv use 1.8.5’

    tfenv use 1.8.5

    ➜ ~ which terraform
    /home/myusername/.tfenv/bin/terraform

    terraform –version
    Terraform v1.8.5

  • terraform terragrunt

    https://www.terraform-best-practices.com/
    https://github.com/terraform-linters/tflint

    check:

    terraform init
    # terragrunt init -upgrade 重新更新依赖
    terraform validate
    terraform plan
    

    交互 approve 方式执行

    terraform apply
    

    直接执行,省去交互

    terraform apply -auto-approve

    格式化代码

    terraform fmt -recursive
    terragrunt hclfmt
    

    查看 state

    terraform state list
    

    import state

    terragrunt import --terragrunt-working-dir dev/k8s/namespaces 'kubernetes_namespace.namespaces["app"]' app
    

    清理 dev 集群 hongkong

    terragrunt run-all plan --terragrunt-working-dir deploy/dev/gke/hongkong
    terragrunt run-all destroy --terragrunt-working-dir deploy/dev/gke/hongkong
    

    查看依赖图

    terragrunt graph-dependencies --terragrunt-working-dir deploy/dev/gke/hongkong
    

    执行 destroy

        - terragrunt plan -destroy -target k8s
        - terragrunt destroy -target module.k8s -force
    

    terragrunt 删除目录感知不到要删除对应的 resource
    应该删除对应的 inputs, 前提 inputs 要支持空 defaut 值

    升级

    brew upgrade terragrunt