I have created a DB via Terraform and after that, I have removed and created it again with another name.
And when I changed the DB name in Terraform it says that:
Error: Error creating Database: googleapi: Error 400: Invalid request: failed to create database YYY. Detail: pq: database "YYY" already exists., invalid
I have restored a backup file and don't want to remove and recreate again via Terraform.
Do you know how can I fix it?
I have removed and created it again with another name.
Did you do that manually or using Terraform? In case you did it manually, try importing YYY database to the terraform state using terraform import. More info here.
Related
I've created an Azure Storage Account to be used as the backend state store for Terraform, and I was able to write to this from an Azure DevOps pipeline running Terraform commands. I can see the container in the Storage Account and confirm that it has the state content from the pipeline execution in it with that same key. However, when I try to run Terraform "manually" using the same backend store, I'm getting an error that it cannot find that container:
$ terraform init -backend-config="storage_account_name=<redacted>" -backend-config="container_name=auto-api-tfstate" -backend-config="access_key=<redacted>" -backend-config="key=dev-internal2/dev-internal2.tfstate:us"
Initializing modules...
Initializing the backend...
Backend configuration changed!
Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.
Error: Error inspecting states in the "azurerm" backend:
storage: service returned error: StatusCode=404, ErrorCode=ContainerNotFound, ErrorMessage=The specified container does not exist.
RequestId:89a9b361-a01e-00b1-0fb4-ba5d51000000
Time:2021-10-06T13:18:41.2460433Z, RequestInitiated=Wed, 06 Oct 2021 13:18:40 GMT, RequestId=89a9b361-a01e-00b1-0fb4-ba5d51000000, API Version=2016-05-31, QueryParameterName=, QueryParameterValue=
Prior to changing backends, Terraform inspects the source and destination
states to determine what kind of migration steps need to be taken, if any.
Terraform failed to load the states. The data in both the source and the
destination remain unmodified. Please resolve the above error and try again.
My main.tf file has simply:
terraform {
backend "azurerm" {}
}
As mentioned, this same terraform init command worked when invoked in a Bash script in an ADO pipeline, so not sure what may be the issue. Any suggestions for debugging this appreciated.
Uncovered the issue ... there was state information in the .terraform folder which conflicted with the new backend. Once I cleared that out, the "terraform init" command worked as expected.
I am trying to reapply my changes using terraform apply but when I am doing it again , it gives me error with resource already exists and stops the deployment .
Example:
Error: AlreadyExistsException: An alias with the name arn:aws:kms:us-east-1:490449857273:alias/continuedep-cmk-us-east-1 already exists
status code: 400, request id: 4447fd20-d33b-4c87-891e-cc5e09cc6108
on ../../../modules/kms_cmk/main.tf line 11, in resource "aws_kms_alias" "keyalias":
11: resource "aws_kms_alias" "keyalias" {
Error: Error creating DB Subnet Group: DBSubnetGroupAlreadyExists: The DB subnet group 'continuedep-sbg' already exists.
status code: 400, request id: 97d662b6-79d4-4fde-aaf7-a2f3e5a0bd9e
on ../../../modules/rds-postgres/main.tf line 2, in resource "aws_db_subnet_group" "generic_db_subnet_group":
2: resource "aws_db_subnet_group" "generic_db_subnet_group" {
Likewise i get errors with many other existing resources.I want to avoid/ignore such errors and continue my deployment .
What other way i can use from which I can restart my terraform resource deployment from where it is interrupted in the middle.
My terraform version is :
Terraform v0.12.9
The errors are returned by the API the Terraform provider is calling.
Possible causes of this could be:
you ( or someone else ) have executed your Terraform code and you don't have a shared / updated state
someone have created them manually
a Terraform destroy failed in a way that deleted the resources for the API but failed to save the update state
solutions depends on what you need. You can:
delete those resources from your Terraform code to stop managing them with it
delete those resources from the API ( cloud provider ) and recreate them with Terraform
Perform a terraform import of those resources and remove the terraform code that is trying to recreate them (NOT RECOMMENDED)
use terraform apply --target=xxx to apply only resources you need to apply (NOT RECOMMENDED)
I did terraform apply which leads to creating new security group and removing one security group.
Then I tried terraform import but I got this error
Can't import aws_security_group.online_ec2_security_group, would collide with an existing resource.
Please, if anyone knows how can I replace the new security group with the old security group that would be a great help
Terraform shows this error if there is already a remote object associated with the given resource address. You can see Terraform's record of that existing object by running the following command:
terraform state show aws_security_group.online_ec2_security_group
If you want Terraform to forget about that existing object (leaving it still existing in AWS, but no longer tracked by Terraform) then you can remove it directly from the Terraform state:
terraform state rm aws_security_group.online_ec2_security_group
After running that, Terraform will no longer have any record that this object exists, so if you want to delete it you'll need to do so manually outside of Terraform.
Once Terraform has "forgotten" the old object, you should be able to import the new object in its place.
I am running an application inside pod in aks, that is provisioning a aws service using terraform, if that pod is deleted or stopped in between when provisioning is going on, the terraform state file is corrupted.
When I try provisioning again using that state file I get apply error. Some of the resources got provisioned but are not updated in the state file. I get following error.
Error: Error applying plan:
1 error(s) occurred:
* aws_s3_bucket.examplebucket: 1 error(s) occurred:
* aws_s3_bucket.examplebucket: Error creating S3 bucket: BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.
status code: 409
so how to update the state file so I can use it again?
Not sure the error is related to kubernetes resources and pods.
But if you need refresh / recreate the bucket, you can taint it.
terraform taint aws_s3_bucket.examplebucket
terraform plan
terraform apply
Let me know if this is helpful or not.
If terraform tries to create something that already exists, you will need to import the resource into terraform.
Every kind of terraform resource, in this case a aws_s3_bucket, has listed in its documentation, at the bottom, on how to import it.
In this case, the following command should do the trick:
terraform import aws_s3_bucket.bucket **BUCKETNAME**
Replace BUCKETNAME with your bucket.
I am working on terraform with openstack as the cloud provider. I have a deploy.tf script that creates a role:
resource "openstack_identity_role_v3" "role_example" {
name = "creator"
}
My finding on how terraform creates resources:
If the role does not exist in openstack, terraform creates one with
no problem.
If the role exists in openstack and is created with the
same terraform script, ie. terraform.state has an entry of it,
terraform returns with no errors.
my issue is: if I remove the state file or if the role is created out of bands either manually or by some other terraform script.I get the following error:
* openstack_identity_role_v3.role_example: Error creating OpenStack role: Expected HTTP response code [201] when accessing [POST https://<example-openstack-url>/v3/roles], but got 409 instead
{"error": {"message": "Conflict occurred attempting to store role - Duplicate Entry", "code": 409, "title": "Conflict"}}
I am trying to find a workaround so that if the role doesn't exist, terraform apply creates it, and if it already exists, despite having created manually or by any other terraform deployment script, terraform skips its creation and throw no error.