Terraform states I am trying to initialized file in an empty directory - terraform

Terraform states I am trying to initialized file in an empty directory which is my local profile: C:\Users
c:\users\a874193\kplabs\sectiontwo.
When I run an ls command it shows the tf file in there and when I look at the .tf file within the actual \sectiontwo folder it does show a more or less empty .tf file.
I am very new to TF, so please break it down to me. Thanks
provider "aws" {
region = "us-west-2"
access_key = "---------------"
secret_key = "----------------------"
}
resource "aws_instance" "kelec2" {
ami = "ami-0d593311db5abb72b"
instance_type = "t2.micro"
}
resource "aws_eip" "elastic" {
vpc = true
}
With the above I have tried to comment out the 2 resources specified, I also replaced all of this with just this:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.37.0"
}
}
}
Result when trying to run terraform init:
PS C:\Users\a874193\kplabs\sectiontwo> terraform init
Terraform initialized in an empty directory!
with Terraform immediately by creating Terraform configuration files.
Result from ls:
PS C:\Users\a874193\kplabs\sectiontwo> ls
Directory: C:\Users\a874193\kplabs\sectiontwo
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 29/10/2022 18:04 .terraform
-a---- 29/10/2022 18:04 1152 .terraform.lock.hcl
-a---- 29/10/2022 18:43 310 attributes.tf.bak
-a---- 29/10/2022 18:44 156 terraform.tfstate
-a---- 29/10/2022 18:44 15786 terraform.tfstate.backup
PS C:\Users\a874193\kplabs\sectiontwo> ls
Directory: C:\Users\a874193\kplabs\sectiontwo
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 29/10/2022 18:04 .terraform
-a---- 29/10/2022 18:04 1152 .terraform.lock.hcl
-a---- 29/10/2022 18:43 310 attributes.tf.bak
-a---- 29/10/2022 18:44 156 terraform.tfstate
-a---- 29/10/2022 18:44 15786 terraform.tfstate.backup

EDIT:
OK, I ended up adding just the kplabs folder which then showed the sectiontwo folder within the left hand pane.
It has init and produced the correct plan resources.
I think I am getting confused with the folder structure so if anyone can make any sense of why it only works when adding the kplabs folder and not the sectiontwo within the kplabs I would appreciate it.
Thanks

Related

Vitejs/Rollupjs/Esbuild: bundle content scripts for chrome extension

How to correctly bundle content scripts for a chrome extension using Vite.js? Content scripts do not support ES modules (compared to background service worker), so every content script javascript file should not have any import/export statements, all libraries and other external imports should be inlined in every content script as a single file.
I'm currently using the following vite configuration, where I programmatically pass all my content script paths as the entry.
vite.config.ts
export const extensionScriptsConfig = (
entry: string[],
outDir: string,
mode: Env,
): InlineConfig => {
const config = defineConfig({
root: __dirname,
mode,
build: {
outDir,
emptyOutDir: false,
minify: false,
copyPublicDir: false,
target: 'esnext',
lib: {
entry,
formats: ['es'],
fileName: (_, entry) => entry + '.js',
},
},
});
return { configFile: false, ...config };
};
entry contains the following paths
Directory: C:\Users\code\extension\src\content-scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 15/01/2023 14:11 1135 inject-iframe.ts
-a--- 15/01/2023 14:11 2858 inject.ts
-a--- 14/01/2023 17:49 223 tsconfig.json
The setup above works correctly - each content script is a separate file with no import.
But when I try to import a node_module into inject-iframe.ts and inject.ts the following happens in the dist folder:
dist\src\content-scripts\inject.js
import { m as mitt } from './mitt-eb023923.mjs';
...
dist\src\content-scripts\inject-iframe.js
import { m as mitt } from './mitt-eb023923.mjs';
...
The dist:
Directory: C:\Users\code\extension\dist\src\content-scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 15/01/2023 14:44 1093 inject-iframe.js
-a--- 15/01/2023 14:44 2300 inject.js
-a--- 15/01/2023 14:44 334 mitt-eb023923.mjs
And since ES modules are not supported in the content script context the above doesn't work.
I am also not sure if it should be configured via vite, esbuild or rollup options and if I should use target: 'esnext', and formats: ['es'], for my content scripts vite config.
I target manifest v3 extension and the recent chrome version, so older browsers support is not important.
And as seen in the snippets above I also need to import some node_modules into my content scripts.

Kubernetes gitconfig mounted as directory instead of file

I have some helm charts in which I would like to mount the git config globally on each container.
In this case the home directory for each container is / path. When I would like to do it manually on the container I am getting following
git config --global --add safe.directory "*" error: could not lock config file //.gitconfig: Permission denied
Now I want to map my config map to the global .gitconfig file.
set {
name = "git.sync.extraVolumeMounts[0].name"
value = "git-config"
}
set {
name = "git.sync.extraVolumeMounts[0].mountPath"
value = "/.gitconfig"
}
set {
name = "git.sync.extraVolumeMounts[0].subPath"
value = ".gitconfig"
}
With such config I am getting the .gitconfig as folder not the file
bitnami#airflow-web-7cdb6f5d6f-48mzh:/$ ls -la
total 84
drwxr-xr-x 1 root root 4096 Dec 2 12:20 .
drwxr-xr-x 1 root root 4096 Dec 2 12:20 ..
drwxrwsrwx 2 root bitnami 4096 Dec 2 12:20 .gitconfig
drwxr-xr-x 1 root root 4096 Jul 30 11:21 bin
Any idea what I am doing wrong? Is there any environment variable instead I can set?
I tried to use system config but it does not work either as some folder structure is missing.
Just for future, I was able to achieve that with the usage of the proper escaping
{
"name": "git.sync.extraVolumeMounts[0].mountPath",
"type": null,
"value": "/\\.gitconfig"
},
{
"name": "git.sync.extraVolumeMounts[0].name",
"type": null,
"value": "git-config"
},
{
"name": "git.sync.extraVolumeMounts[0].subPath",
"type": null,
"value": "gitconfig"
},
This basically, creates a .gitconfig file and not a folder.
Setup is working fine.

How to add a label to my vm instance in gcp via terraform/terragrunt

I have an issue in our environment where i cannot add a label to a vm instance in GCP via terraform/terragrunt after creation. We have a google repository that is setup via terraform and we use git to clone and update from a local repository, this will activate a trigger on cloudbuild to push the changes to the repo. We do not use terraform/grunt commands at all. It is all controlled via git. The labels are referenced in our compute module as shown.
variable "labels" {
description = "Labels to add."
type = map(string)
default = {}
}
Ok onto the issue. We have in our environment a mix of lift and shift and native cloud vm instances. We recently decided we wanted to add an additional label in the code to identify if the instance was under terraform control - ie terraform = "true/false"
labels = {
application = "demo-test"
businessunit = "homes"
costcentre = "90imt"
createdby = "ab"
department = "it"
disasterrecovery = "no"
environment = "rnd"
contact = "abriers"
terraform = "false"
}
}
So i add the label and use the usual git commands to add/commit push etc which triggers the cloudbuild as usual. The problem is, the label does not appear in the console when viewing it.
It's as if cloudbuild or terraform/terragrunt isn't recognising it as a change. I can change the value of a label no problem, but i cannot seem to add or remove a label after the vm has been created.
It has been suggested to run terraform/terragrunt plan in vs code but as mentioned, this has all been setup to use git so the above commands do not work.
For example i run terragrunt init in the directory and get this error
PS C:\Cloudrepos\placesforpeople> terragrunt init
time=2022-07-27T09:56:27+01:00 level=error msg=Error reading file at path C:/Cloudrepos/placesforpeople/terragrunt.hcl: open C:/Cloudrepos/placesforpeople/terragrunt.hcl: The system cannot find the
file specified.
time=2022-07-27T09:56:27+01:00 level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1
PS C:\Cloudrepos\placesforpeople> cd org
PS C:\Cloudrepos\placesforpeople\org> cd rnd
PS C:\Cloudrepos\placesforpeople\org\rnd> cd adam_play_area
PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area> ls
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 20/07/2022 14:18 modules
d----- 20/07/2022 14:18 test_project_001
PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area> cd test_project_001
PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001> cd compute
PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute> ls
Directory: C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 07/07/2022 15:51 start_stop_schedule
d----- 20/07/2022 14:18 umig
-a---- 07/07/2022 16:09 1308 .terraform.lock.hcl
-a---- 27/07/2022 09:56 2267 terragrunt.hcl
PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute> terragrunt init
Initializing modules...
- data_disk in ..\compute_data_disk
Initializing the backend...
Successfully configured the backend "gcs"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Reusing previous version of hashicorp/google from the dependency lock file
- Reusing previous version of hashicorp/google-beta from the dependency lock file
╷
│ Warning: Backend configuration ignored
│
│ on ..\compute_data_disk\backend.tf line 3, in terraform:
│ 3: backend "gcs" {}
│
│ Any selected backend applies to the entire configuration, so Terraform
│ expects provider configurations only in the root module.
│
│ This is a warning rather than an error because it's sometimes convenient to
│ temporarily call a root module as a child module for testing purposes, but
│ this backend configuration block will have no effect.
╵
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider
│ hashicorp/google: could not connect to registry.terraform.io: Failed to
│ request discovery document: Get
│ "https://registry.terraform.io/.well-known/terraform.json": Proxy
│ Authorization Required
╵
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider
│ hashicorp/google-beta: could not connect to registry.terraform.io: Failed
│ to request discovery document: Get
│ "https://registry.terraform.io/.well-known/terraform.json": Proxy
│ Authorization Required
╵
time=2022-07-27T09:57:40+01:00 level=error msg=Hit multiple errors:
Hit multiple errors:
exit status 1
PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute>
But as mentioned, we dont use and have never used these commands to push the changes.
I cannot work out why these labels wont add/remove after the vm has already been created.
I have tried making a change to an instance to trigger the change such as increase the disk size.
I have tried to create a block in the module for all the labels needed but this doesn't work as you cannot have labels as a block in this module.
labels {
application = var.labels.application
businessunit = var.labels.businessunit
costcentre = var.labels.costcentre
createdby = var.labels.createdby
department = var.labels.department
disasterrecovery = var.labels.disasterrecovery
environment = var.labels.environment
contact = var.labels.contact
terraform = var.labels.terraform
}
}
Any ideas? I know you cannot add a label to a project post creation, does the same apply to vm instances? Is there any alternative method i can test?
As requested this is the code for the vm instance
terraform {
source = "../../modules//compute_instance_static_ip/"
}
# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders("org.hcl")
}
dependency "project" {
config_path = "../project"
# Configure mock outputs for the terraform commands that are returned when there are no outputs available (e.g the
# module hasn't been applied yet.
mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
project_id = "project-not-created-yet"
}
}
prevent_destroy = false
inputs = {
gcp_instance_sa_email = "testprj-compute#gc-r-prj-testprj-0001-9627.iam.gserviceaccount.com" # This well tell gcp to use the default GCE service account
instance_name = "rnd-demo-test1"
network = "projects/gc-a-prj-vpchost-0001-3312/global/networks/gc-r-vpc-0001"
subnetwork = "projects/gc-a-prj-vpchost-0001-3312/regions/europe-west2/subnetworks/gc-r-snet-middleware-0001"
zone = "europe-west2-c"
region = "europe-west2"
project = dependency.project.outputs.project_id
os_image = "debian-10-buster-v20220118"
machine_type = "n1-standard-4"
boot_disk_size = 100
instance_scope = ["cloud-platform"]
instance_tags = ["demo-test"]
deletion_protection = "false"
metadata = {
windows-startup-script-ps1 = "Set-TimeZone -Id 'GMT Standard Time' -PassThru"
}
ip_address_region = "europe-west2"
ip_address_type = "INTERNAL"
attached_disks = {
data = {
size = 60
type = "pd-standard"
}
}
/*/ instance_schedule_policy = {
name = "start-stop"
#region = "europe-west2"
vm_start_schedule = "30 07 * * *"
vm_stop_schedule = "00 18 * * *"
time_zone = "GMT"
}
*/
labels = {
application = "demo-test"
businessunit = "homes"
costcentre = "90imt"
createdby = "ab"
department = "it"
disasterrecovery = "no"
environment = "rnd"
contact = "abriers"
terraform = "false"
}
}
terragrunt validate-inputs result below
PS C:\Cloudrepos\placesforpeople\org\rnd> terragrunt validate-inputs
time=2022-07-27T14:25:19+01:00 level=warning msg=The following inputs passed in by terragrunt are unused:
prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
time=2022-07-27T14:25:19+01:00 level=warning msg= - billing_account prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
time=2022-07-27T14:25:19+01:00 level=warning msg= - host_project_id prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
time=2022-07-27T14:25:19+01:00 level=warning prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
time=2022-07-27T14:25:19+01:00 level=info msg=All required inputs are passed in by terragrunt. prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
time=2022-07-27T14:25:19+01:00 level=error msg=Terragrunt configuration has misaligned inputs
time=2022-07-27T14:25:19+01:00 level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1
PS C:\Cloudrepos\placesforpeople\org\rnd>
I have found the culprit!
In the compute instance module i discovered this block of code. I removed labels and voila the extra labels now appear. Thanks for the assistance and advice on post formatting.
lifecycle {
ignore_changes = [
boot_disk.0.initialize_params.0.image,
attached_disk, labels
]
}

Terraform use local provider/plugin

I installed Terraform v1.0.1 on linux_amd64 (Oracle Linux Srv 8.4 64bit).
I’m trying to use a local provider/plugin that I saved in the folder: /root/.terraform.d/plugins
# ll /root/.terraform.d/plugins
drwxr-xr-x. 2 root root 38 Jun 29 15:42 oldversion
-rwxr-xr-x. 1 root root 30068808 Jun 29 15:42 terraform-provider-zabbix
drwxr-xr-x. 2 root root 52 Jun 29 15:42 test_plugging
This is my vim /root/.terraformrc:
provider_installation {
filesystem_mirror {
path = "/root/.terraform.d/plugins"
}
direct {
exclude = ["registry.terraform.io/*/*"]
}
}
This is my main.tf:
terraform {
required_version = ">= 0.12.6"
}
provider "zabbix" {
username = local.provider_vars.zabbix.username
password = local.provider_vars.zabbix.password
url = local.provider_vars.zabbix.endpoint
tls_insecure = true
}
but when I run: terraform init
Initializing the backend...
Initializing provider plugins...
Finding latest version of hashicorp/zabbix...
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/zabbix: provider registry.terraform.io/hashicorp/zabbix was
not found in any of the search locations
/root/.terraform.d/plugins
How can fix this problem?
Thanks for the help
Marco
Assuming you have a binary
~/.terraform.d/plugins/terraform.local/local/zabbix/1.0.0/linux_amd64/terraform-provider-zabbix_v1.0.0
Configure Terraform as follows
terraform {
required_providers {
zabbix = {
source = "terraform.local/local/zabbix"
version = "1.0.0"
# Other parameters...
}
}
}
Which works as follows
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding terraform.local/local/zabbix versions matching "1.0.0"...
- Installing terraform.local/local/zabbix v1.0.0...
- Installed terraform.local/local/zabbix v1.0.0 (unauthenticated)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
The solution above is absolutely correct, but needs to be clarified edit .terraformrc:
provider_installation {
filesystem_mirror {
path = "/home/user/.terraform.d/plugins"
}
direct {
exclude = ["terraform.local/*/*"]
}
}

Hiera 3 + Puppet 4.2 can't manage empty value in yaml datasource

I'm facing some issues to use Hiera 3 with Puppet 4.2.
When applying my puppet manifest using this command:
puppet apply environments/production/manifests/init.pp --hiera_config=hiera.yaml
I get the following error:
Error: Evaluation Error: Error while evaluating a Function Call, Could not find data item myclass::ensure in any Hiera data file and no default supplied at /home/vagrant/temp/environments/production/manifests/init.pp:13:39 on node a
Here is the directory structure :
$ tree
.
├── environments
│   └── production
│   ├── config.yaml
│   └── manifests
│   └── init.pp
└── hiera.yaml
content of config.yaml:
$ cat hiera.yaml
---
:backends:
- yaml
:hierarchy:
- config
:yaml:
:datadir: environments/production
content of init.pp:
class myclass(
$version = $myclass::params::version,
$ensure = $myclass::params::ensure,
) inherits myclass::params {
notify {"$version": }
notify {"$ensure": }
}
class myclass::params {
$version = hiera('myclass::version', '1.0.0')
$ensure = hiera('myclass::ensure', 'running')
}
class {'myclass': }
content of hiera data source:
$ cat config.yaml
---
myclass::version: '1.0.0'
myclass::ensure:
Looks like Hiera can't handle empty values from a yaml data source and/or replace them by default value?

Resources