Terraform issue on macOs - terraform

I started to learning terraform, I have installed terraform and docker desktop on my macbook.
I am following the quick start tutorial and got below error.
ERROR
╷
│ Error: Error pinging Docker server: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
│
│ with provider["registry.terraform.io/kreuzwerker/docker"],
│ on main.tf line 10, in provider "docker":
│ 10: provider "docker" {}
│
Docker status
❯ docker --version
Docker version 20.10.20, build 9fdeb9c
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6633f3b801e docker/getting-started "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp flamboyant_volhard
main.tf
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.13.0"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
Issue has been resolved, solution available in this link

Based on the provider documentation [1] (as the error describes as well), you need to switch to the following:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.22.0"
}
}
}
provider "docker" {}
resource "docker_container" "nginx" {
image = docker_image.nginx.image_id
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
The example from the tutorial is using a very old version of Docker provider. When you make these changes, make sure to run terraform init -upgrade prior to running terraform plan or terraform apply.
[1] https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container#example-usage

Related

Terraform init failed. Error: Failed to query available provider packages

Installed terraform & init is failing.
- Finding hashicorp/aws versions matching "~> 4.16"...
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/aws: could not connect to registry.terraform.io: Failed to request discovery document: Get
│ "https://registry.terraform.io/.well-known/terraform.json": x509: “cf-registry.tf-registry-prod-use1.terraform.io” certificate is not standards compliant
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "app_server" {
ami = "ami-830c94e3"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
I have uninstalled and reinstalled it but still not working.
Terraform version: Terraform v1.3.7 on darwin_amd64
I was facing the same issue, even with a module that was working well previously. After discarding network issues ( curl https://registry.terraform.io/.well-known/terraform.json ), trying with different files and directories, etc. It turned out that a reboot did the trick for me.

I cannot create a virtualbox with Terraform provider shekeriev/virtualbox

I'm using this provider - [shekeriev/virtualbox/0.0.4][1]
terraform -v
Terraform v1.3.6
on windows_amd64
+ provider registry.terraform.io/shekeriev/virtualbox v0.0.4
my configuration file main.tf
terraform {
required_providers {
virtualbox = {
source = "shekeriev/virtualbox"
version = "0.0.4"
}
}
}
provider "virtualbox" {
# Configuration options
}
resource "virtualbox_vm" "node" {
count = 1
name = format("node-%02d", count.index + 1)
image = "https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190507.0.0/providers/virtualbox.box"
cpus = 1
memory = "512 mib"
network_adapter {
type = "hostonly"
device = "IntelPro1000MTDesktop"
host_interface = "VirtualBox Host-Only Ethernet Adapter"
}
}
them i'm typing
terraform init
terraform plan
terraform apply
yes
and getting this error!
Error: failed to unpack image https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190507.0.0/providers/virtualbox.box: error unpacking gold image virtualbox.box: exit status 2
│
│ with virtualbox_vm.node[0],
│ on main.tf line 18, in resource "virtualbox_vm" "node":
│ 18: resource "virtualbox_vm" "node" {
I've tried different images and provider terra-farm but after all they returning this error.
[1]: https://registry.terraform.io/providers/shekeriev/virtualbox/0.0.4

index.yaml : 404 Not Found

I want to run Helm chart from Terraform script. I tried this:
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
source = "hashicorp/kubernetes"
version = "2.13.1"
}
kubectl = {
source = "gavinbunney/kubectl"
version = "1.14.0"
}
helm = {
source = "hashicorp/helm"
version = "2.6.0"
}
}
}
provider "kubectl" {
# run kubectl cluster-info to get expoint and port
host = "https://192.168.1.139:6443/"
token = "eyJhbG......."
insecure = "true"
}
provider "kubernetes" {
# run kubectl cluster-info to get expoint and port
host = "https://192.168.1.139:6443/"
token = "eyJhb...."
insecure = "true"
}
resource "kubernetes_namespace" "example" {
metadata {
annotations = {
name = "example-annotation"
}
labels = {
mylabel = "label-value"
}
name = "terraform-example-namespace"
}
}
resource "helm_release" "spring-helm-stg" {
name = "spring-helm-stg"
repository = "https://github.com/rcbandit111/terraform_helm_chart_poc/tree/main/helm/spring-helm-stg"
chart = "spring-helm-stg"
}
Full code: https://github.com/rcbandit111/terraform_helm_chart_poc
helm_release.spring-helm-stg: Creating...
╷
│ Error: could not download chart: looks like "https://github.com/rcbandit111/terraform_helm_chart_poc/tree/main/helm/spring-helm-stg" is not a valid chart repository or cannot be reached: failed to fetch https://github.com/rcbandit111/terraform_helm_chart_poc/tree/main/helm/spring-helm-stg/index.yaml : 404 Not Found
│
│ with helm_release.spring-helm-stg,
│ on main.tf line 48, in resource "helm_release" "spring-helm-stg":
│ 48: resource "helm_release" "spring-helm-stg" {
I created the helm chart using this command: helm create spring-helm-stg
But there is no file index.yaml
Full helm chart code: https://github.com/rcbandit111/terraform_helm_chart_poc/tree/main/helm/spring-helm-stg
Do you know how I can fix this?
First: your repository url is https://github.com/rcbandit111/terraform_helm_chart_poc (and NOT https://github.com/rcbandit111/terraform_helm_chart_poc/tree/main/helm/spring-helm-stg)
After fixing that, you should then place the index.yaml file at root level (instead of helm directory) and also - make it a valid one. That's also "kind of" important.
Because your repository is filled with sub-directories, lots of index files and seems pretty messed-up (it's OK to make experiments... it's also OK to delete irrelevant parts) you may consider rearranging everything in a new branch and merge it to master OR create a new better-organized repository.
RESPECT to #marko for the documentation link in the comment. Please use that when you are writing your repository's index file
Cheers

Error: Inconsistent dependency lock file when extract resource into a module

I am new to terraform and as I extracted one of the resources into a module I got this:
Error: Inconsistent dependency lock file
│
│ The following dependency selections recorded in the lock file are inconsistent with the current
│ configuration:
│ - provider registry.terraform.io/hashicorp/heroku: required by this configuration but no version is selected
│
│ To update the locked dependency selections to match a changed configuration, run:
│ terraform init -upgrade
How I did?
First I had this:
provider "heroku" {}
resource "heroku_app" "example" {
name = "learn-terraform-heroku-ob"
region = "us"
}
resource "heroku_addon" "redis" {
app = heroku_app.example.id
plan = "rediscloud:30"
}
after that terraform init was runing without error also terraform plan was successfull.
Then I extracted the redis resource declaration into a module:
provider "heroku" {}
resource "heroku_app" "example" {
name = "learn-terraform-heroku-ob"
region = "us"
}
module "key-value-store" {
source = "./modules/key-value-store"
app = heroku_app.example.id
plan = "30"
}
And the content of modules/key-value-store/main.tf is this:
terraform {
required_providers {
mycloud = {
source = "heroku/heroku"
version = "~> 4.6"
}
}
}
resource "heroku_addon" "redis" {
app = var.app
plan = "rediscloud:${var.plan}"
}
terraform get went well. but terraform plan showed me the above error!
For this code to work, you have to have the required_providers blocks in both the root and child modules. So, the following needs to happen:
Add the required_providers block to the root module (this is what you have already)
Add the required_providers block to the child module and name it properly (currently you have set it to mycloud, and provider "heroku" {} block is missing)
The code that needs to be added in the root module is:
terraform {
required_providers {
heroku = {
source = "heroku/heroku"
version = "~> 4.6"
}
}
}
provider "heroku" {}
resource "heroku_app" "example" {
name = "learn-terraform-heroku-ob"
region = "us"
}
module "key-value-store" {
source = "./modules/key-value-store"
app = heroku_app.example.id
plan = "30"
}
In the child module (i.e., ./modules/key-value-store) the following needs to be present:
terraform {
required_providers {
heroku = { ### not mycloud
source = "heroku/heroku"
version = "~> 4.6"
}
}
}
provider "heroku" {} ### this was missing as well
resource "heroku_addon" "redis" {
app = var.app
plan = "rediscloud:${var.plan}"
}
This stopped working when the second resource was moved to the module as Heroku is not an official Terraform provider hence the provider settings are not propagated to the modules. For the unofficial providers (e.g., marked with verified), corresponding blocks of required_providers and provider <name> {} have to be defined. Also, make sure to remove the .terraform directory and re-run terraform init.

helm list not showing up after terraform apply

I am using terraform helm provider to install helm package.
My current main.tf file
provider "helm" {
kubernetes {
config_path = pathexpand(var.kube_config)
}
}
provider "kubernetes" {
config_path = pathexpand(var.kube_config)
}
data "template_file" "test_values" {
template = file("./scripts/test-values.yml")
vars = {
NAMESPACE = "test"
}
}
resource "helm_release" "test" {
chart = "test"
name = "test"
repository = "."
namespace = "test"
values = [
data.template_file.test_values.rendered
]
}
Kubectl command output
kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
test 0/1 Running 0 59m
Issue is "helm list" does not show any result.
My current version
helm version
Client: &version.Version{SemVer:"v2.17.0", GitCommit:"a690bad98af45b015bd3da1a41f6218b1a451dbe", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.17.0", GitCommit:"a690bad98af45b015bd3da1a41f6218b1a451dbe", GitTreeState:"clean"}
and terrafrom version -
provider registry.terraform.io/hashicorp/google v3.69.0
+ provider registry.terraform.io/hashicorp/helm v2.1.2
+ provider registry.terraform.io/hashicorp/kubernetes v2.2.0
Any reason why "helm list" does not show any output ?

Resources