To enable preview feature of azure resource provider - azure-devops

I would like to enable an azure preview feature via terraform. I have configured skip provider registration but when I tried to apply still get provider already exists error. I have to import manually as a workaround.
QUERY?:
do we must import manually to avoid provider exist error when register preview feature?
as I already define skip registration but seems it didn’t work.
Thanks!
======== configuration ========
Configure the Azure provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm”
version = “~> 2.99"
}
}
required_version = “>= 1.1.0”
}
provider “azurerm” {
features {}
skip_provider_registration = true
}
resource “azurerm_resource_provider_registration” “example” {
name = “Microsoft.Network”
feature {
name = “AFWEnableNetworkRuleNameLogging”
registered = true
}
}
have configured to skip provider registration but when I tried to apply still get provider already exists.
======== error log
terraform apply main.tf plan
azurerm_resource_provider_registration.example: Creating…
╷
│ Error: A resource with the ID. “/subscriptions/xxxx-xxxx/providers/Microsoft.Network” already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for “azurerm_resource_provider_registration” for more information.
Any solution on the above requirement to enable the preview feature of the corresponding namespace resource provider.

If the Terraform statefile already contains the relevant providers, we should import it first before making any changes. Then only Terraform will read the respective changes from statefile.
Step1:
Add below code in provider tf and main tf as below
provider tf file
terraform {
required_version = ">= 1.1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.99"
}
}
}
provider "azurerm" {
features {}
skip_provider_registration = true
}
main tf file as follows
resource "azurerm_resource_provider_registration" "example" {
name = "Microsoft.Network"
feature {
name = "AFWEnableNetworkRuleNameLogging"
registered = true
}
}
Step2:
Run bellow commands
terraform plan
Run below command
terraform apply -auto-approve
NOTE:
if error saying its "already exists - to be managed via Terraform this resource needs to be imported into the State." then please run below command to import the respective service via terraform
terraform import azurerm_resource_provider_registration.example /subscriptions/************************/providers/Microsoft.Network
Output as follows:
Step3:
run below commands
terraform plan
terraform apply -auto-approve

Related

Restoring a AWS documentdb snapshot with terraform

I am unsure how to restore an AWS documentdb cluster that is managed by terraform.
My terraform setup looks like this:
resource "aws_docdb_cluster" "this" {
cluster_identifier = var.env_name
engine = "docdb"
engine_version = "4.0.0"
master_username = "USERNAME"
master_password = random_password.this.result
db_cluster_parameter_group_name = aws_docdb_cluster_parameter_group.this.name
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
db_subnet_group_name = aws_docdb_subnet_group.this.name
deletion_protection = true
backup_retention_period = 7
preferred_backup_window = "07:00-09:00"
skip_final_snapshot = false
# Added on 6.25.22 to rollback an incorrect application of the namespace
# migration, which occurred at 2AM EST on June 23.
snapshot_identifier = "...the arn for the snapshot..."
}
resource "aws_docdb_cluster_instance" "this_2a" {
count = 1
engine = "docdb"
availability_zone = "us-east-1a"
auto_minor_version_upgrade = true
cluster_identifier = aws_docdb_cluster.this.id
instance_class = "db.r5.large"
}
resource "aws_docdb_cluster_instance" "this_2b" {
count = 1
engine = "docdb"
availability_zone = "us-east-1b"
auto_minor_version_upgrade = true
cluster_identifier = aws_docdb_cluster.this.id
instance_class = "db.r5.large"
}
resource "aws_docdb_subnet_group" "this" {
name = var.env_name
subnet_ids = module.vpc.private_subnets
}
I added the snapshot_identifier parameter and applied it, expecting a rollback. However, this did not have the intended effect of restoring documentdb state to its settings on June 23rd. (As far as I can tell, nothing changed at all)
I wanted to avoid using the AWS console approach (described here) because that creates a new cluster which won't be tracked by terraform.
What is the proper way of accomplishing this rollback using terraform?
The snapshot_identifier parameter is only used when Terraform creates a new cluster. Setting it after the cluster has been created just tells Terraform "If you ever have to recreate this cluster, use this snapshot".
To actually get Terraform to recreate the cluster you would need to do something else to make Terraform think the cluster needs to be recreated. Possible options are:
Run terraform taint aws_docdb_cluster.this to signal to Terraform that the resource needs to be recreated. It will then recreate it the next time you run terraform apply.
Delete the cluster through some other means, like the AWS console, and then run terraform apply.
The general approach is this, but i have no experience with documentdb. Hope this helps.
0. Take a backup of your terrafrom state file terraform state pull > backup_state_file_timestamp.json
Restore through the console to the point in time you want.
Remove the old instances and cluster from your terraform state file
terraform state rm aws_docdb_cluster_instance.this_2a
terraform state rm aws_docdb_cluster_instance.this_2b
terraform state rm aws_docdb_cluster.this
Import the manually restored cluster and instance into terraform
terraform import aws_docdb_cluster.this cluster_identifier
terraform import rm aws_docdb_cluster_instance.this_2a identifier
terraform import rm aws_docdb_cluster_instance.this_2b identifier
(see import at the bottom https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/docdb_cluster_instance and https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/docdb_cluster)

On validating the Azure Devops git configuration in the Azure portal for Azure Data Factory service getting an error prompt

I had used Terraform to configure Azure git in Azure Data Factory but after post deployment on Validating the connection I am getting a prompt with an error. I have attached the screenshot below.
data "azurerm_client_config" "current" {}
resource "azurerm_resource_group" "example" {
name = "testadfrg"
location = "West Europe"
}
resource "azurerm_data_factory" "df" {
name = "testadfadf"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
public_network_enabled = "true"
vsts_configuration {
account_name = "organizationaz440"
branch_name = "Development"
project_name = "TestProject"
repository_name = "DataOps"
root_folder = "/ADF"
tenant_id = data.azurerm_client_config.current.tenant_id
}
}
It seems your user account which is trying to open ADF studio to validate changes doesn't have access on Git Repo. Kindly work with Git repo admins with error details. They should be able to give you relevant permissions.

Terraform tries to load old defunct provider

Attempting to use cyrilgdn/postgresql provider but terraform continues to attempt to load hashicorp/postgresql, this causes init to fail. Currently using terraform 1.0.0, although the problems happens on 14.1 too - have not upgraded from 12.x, always run 14.1 or newer on this work.
I've reduced the code to the below, nothing else in this folder and still get the problem
terraform {
required_version = ">= 0.14.1"
required_providers {
postgres = {
source = "cyrilgdn/postgresql"
version = ">=1.13.0"
}
}
}
provider "postgresql" {
host = "TBC"
port = 5432
username = "TBC"
password = "TBC"
}
init reports:
Initializing provider plugins...
- Finding cyrilgdn/postgresql versions matching ">= 1.13.0"...
- Finding latest version of hashicorp/postgresql...
- Installing cyrilgdn/postgresql v1.13.0...
- Installed cyrilgdn/postgresql v1.13.0 (self-signed, key ID 3918DD444A3876A6)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/postgresql: provider registry registry.terraform.io does not have a
provider named registry.terraform.io/hashicorp/postgresql
terraform providers reports
Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/postgresql]
└── provider[registry.terraform.io/cyrilgdn/postgresql] >= 1.13.0
How can I stop it trying to find hashicorp/postgresql ?
It should be postgresql, not postgres:
terraform {
required_version = ">= 0.14.1"
required_providers {
postgresql = {
source = "cyrilgdn/postgresql"
version = ">=1.13.0"
}
}
}

Terraform running in Azure Pipeline attempting to install azcli provider

I'm running Terraform in an Azure Pipeline (something I have experience of doing) and for some reason the init step is attempting to install a provider for azcli, which I don't think exists. This does not happen when I run Terraform on my local machine.
My providers file is:
terraform {
required_version = ">=0.13"
backend "azurerm" {
container_name = "tfstate"
key = "terraform.tfstate"
}
required_providers {
grafana = {
source = "grafana/grafana"
version = "=1.5.0"
}
}
}
This is the error I'm seeing:
I'm not sure why Terraform is trying to install the azcli provider; I don't think it even exists. Has anyone seen this before?
Terraform searches directly and indirectly for providers when initialization. It is possible there is a mistake in the resource name or provider definition. Search your codebase for azcli.
▶ cat .\main.tf
resource "azcli_test" "test" {
test = "true"
}
~\projects\test\t5 ◷ 10:10:21 AM
▶ C:\Users\pearcec\bin\terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/azcli...
Error: Failed to install provider
Error while installing hashicorp/azcli: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/azcli
or
~\projects\test\t5 ◷ 10:10:23 AM
▶ cat .\main.tf
provider "azcli" {
features {}
}
~\projects\test\t5 ◷ 10:13:41 AM
▶ C:\Users\pearcec\bin\terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/azcli...
Error: Failed to install provider
Error while installing hashicorp/azcli: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/azcli
or
▶ cat .\main.tf
terraform {
required_providers {
azcli = {
source = "-/azcli"
}
}
}
~\projects\test\t5 ◷ 10:16:09 AM
▶ C:\Users\pearcec\bin\terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of -/azcli...
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider -/azcli:
provider registry registry.terraform.io does not have a provider named
registry.terraform.io/-/azcli

How to reconcile the Terraform State with an existing bucket?

Using Terraform 11.14
My terraform file contains the following resource:
resource "google_storage_bucket" "assets-bucket" {
name = "${local.assets_bucket_name}"
storage_class = "MULTI_REGIONAL"
force_destroy = true
}
And this bucket has already been created (it exists on the infrastructure based on a previous apply)
However the state (remote on gcs) is inconsistent and doesn't seem to include this bucket.
As a result, terraform apply fails with the following error:
google_storage_bucket.assets-bucket: googleapi: Error 409: You already own this bucket. Please select another name., conflict
How can I reconcile the state? (terraform refresh doesn't help)
EDIT
Following #ydaetskcoR's response, I did:
terraform import module.bf-nathan.google_storage_bucket.assets-bucket my-bucket
The output:
module.bf-nathan.google_storage_bucket.assets-bucket: Importing from ID "my-bucket"...
module.bf-nathan.google_storage_bucket.assets-bucket: Import complete! Imported google_storage_bucket (ID: next-assets-bf-nathan-botfront-cloud)
module.bf-nathan.google_storage_bucket.assets-bucket: Refreshing state... (ID: next-assets-bf-nathan-botfront-cloud)
Error: module.bf-nathan.provider.kubernetes: 1:11: unknown variable accessed: var.cluster_ip in:
https://${var.cluster_ip}
The refreshing step doesn't work. I ran the command from the project's root where a terraform.tfvars file exists.
I tried adding -var-file=terraform.tfvars but no luck. Any idea?
You need to import it into the existing state file. You can do this with the terraform import command for any resource that supports it.
Thankfully the google_storage_bucket resource does support it:
Storage buckets can be imported using the name or project/name. If the project is not passed to the import command it will be inferred from the provider block or environment variables. If it cannot be inferred it will be queried from the Compute API (this will fail if the API is not enabled).
e.g.
$ terraform import google_storage_bucket.image-store image-store-bucket
$ terraform import google_storage_bucket.image-store tf-test-project/image-store-bucket