Arpan
Arpan

Reputation: 993

Terraform and AWS: No Configuration Files Found Error

I am writing a small script that takes a small file from my local machine and puts it into an AWS S3 bucket.

My terraform.tf:

provider "aws" {
  region  = "us-east-1"
  version = "~> 1.6"
}
    
terraform {
  backend "s3" {
    bucket     = "${var.bucket_testing}"
    kms_key_id = "arn:aws:kms:us-east-1:12345678900:key/12312313ed-34sd-6sfa-90cvs-1234asdfasd"
    key     = "testexport/exportFile.tfstate"
    region  = "us-east-1"
    encrypt = true
  }
}
    
data "aws_s3_bucket" "pr-ip" {
  bucket = "${var.bucket_testing}"
}
    
resource "aws_s3_bucket_object" "put_file" {
  bucket = "${data.aws_s3_bucket.pr-ip.id}"
  key    = "${var.file_path}/${var.file_name}"
  source = "src/Datafile.txt"
  etag = "${md5(file("src/Datafile.txt"))}"
    
  kms_key_id = "arn:aws:kms:us-east-1:12345678900:key/12312313ed-34sd-6sfa-90cvs-1234asdfasd"
  server_side_encryption = "aws:kms"
}

However, when I init:

terraform init

#=>

Terraform initialized in an empty directory!
    
The directory has no Terraform configuration files. You may begin working with Terraform immediately by creating Terraform configuration files.

and then try to apply:

terraform apply

#=>

Error: No configuration files found!
    
Apply requires configuration to be present. Applying without a configuration would mark everything for destruction, which is normally not what is desired. If you would like to destroy everything, please run 'terraform destroy' instead which does not require any configuration files.

I get the error above. Also, I have setup my default AWS Access Key ID and value.

What can I do?

Upvotes: 29

Views: 113799

Answers (9)

Viktor Venczel
Viktor Venczel

Reputation: 33

If you encounter this when using "cloud" backend provider, so using HCP Terraform, then you must go into your "workspace" on the HCP terraform platform then into Settings/General and look for Terraform Working Directory. If that's / then it means it'll look for files in your repo's root. I think this is applicable especially when you use VSC integration. Hope this helps some.

Upvotes: 0

kumar sourav
kumar sourav

Reputation: 11

If you are getting this error then you are running the command in the wrong place. You have to be in the directory that contains your configuration files, so before running init or apply you have to go to the Terraform project folder.

Upvotes: 1

Justin Kruse
Justin Kruse

Reputation: 1130

In case any one comes across this now, I ran into an issue where my TF_WORSPACE env var was set to a different workspace than the directory I was in. Double check your workspace with

terraform workspace show

to show your available workspaces

terraform workspace list

to use one of the listed workspaces:

terraform workspace select <workspace name>

If the TF_WORKSPACE env var is set when you try to use terraform workspace select TF will print a message telling you of the potential issue:

The selected workspace is currently overridden using the TF_WORKSPACE
environment variable.

To select a new workspace, either update this environment variable or unset
it and then run this command again.

Upvotes: 1

mojoken
mojoken

Reputation: 1362

I got this error this morning when deploying to production, on a project which has been around for years and nothing had changed. We finally traced it down to the person who created the production deploy ticket had pasted this command into an email using Outlook: terraform init --reconfigure

Microsoft, in its infinite wisdom, combined the two hyphens into one and the one hyphen wasn't even the standard ASCII hyphen character (I think it's called an "en-dash"): terraform init –reconfigure

This caused Terraform 0.12.31 to give the helpful error message:

Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.

It took us half an hour and another pair of eyes to notice that the hyphens were incorrect and needed to be re-typed! (I think terraform thought "reconfigure" was the name of the directory we wanted to run the init in, which of course didn't exist. Perhaps terraform could be improved to name the directory it's looking in when it reports this error?)

Thanks Microsoft for always being helpful (not)!

Upvotes: 0

neOn
neOn

Reputation: 21

Another possible reason could be if you are using modules where the URL is incorrect.

When I had:

source = "git::ssh://[email protected]/observability.git//modules/ec2?ref=v2.0.0"

instead of:

source = "git::ssh://[email protected]/observability.git//terraform/modules/ec2?ref=v2.0.0"

I was seeing the same error message as you.

Upvotes: 0

Vijay Kumar
Vijay Kumar

Reputation: 1

I too had the same issue, remember terraform filename should end with .tf as extension

Upvotes: 0

Ashwani Singh
Ashwani Singh

Reputation: 966

Error: No configuration files found!

The above error arises when you are not present in the folder, which contains your configuration file. To remediate the situation you can create a .tf in your project folder you will be working. Note - An empty .tf will also eliminate the error, but will be of limited use as it does not contain provider info. See the example below:-

provider "aws" {
    region = "us-east" #Below value will be asked when the terraform apply command is executed if not provided here
   }
 

So, In order for the successful execution of the terraform apply command you need to make sure the below points:-

  1. You need to be present in your terraform project folder (Can be any directory).
  2. Must contain .tf preferably should contain terraform provider info.
  3. Execute terraform init to initialize the backend & provider plugin.
  4. you are now good to execute terraform apply (without any no config error)

Upvotes: 4

raphaeljuwe
raphaeljuwe

Reputation: 119

I had the same error emulated by you, In my case it was not a VPN error but incorrect file system naming. I was in the project folder.To remedy the situation, i created a .tf file with vim editor with the command vi aws.tf, then populated the file with defined variables. Mine is working.

See my attached images

enter image description here

Upvotes: 0

Alexander
Alexander

Reputation: 1487

This error means that you have run the command in the wrong place. You have to be in the directory that contains your configuration files, so before running init or apply you have to cd to your Terraform project folder.

Upvotes: 68

Related Questions