Terraform and AWS: No Configuration Files Found Error
Asked Answered
S

8

27

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?

Salvo answered 16/9, 2018 at 7:7 Comment(1)
If you're executing terraform apply from within a shell script, make sure that shell script doesn't cd into a different directory before the terraform apply command is executedMulholland
A
0

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.
Abramabramo answered 24/10, 2022 at 20:46 Comment(0)
C
64

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.

Cultivated answered 16/9, 2018 at 10:47 Comment(3)
thats what I did .. so my .tf files are in a folder project1 .. so I cd project1 and then run terraform init and then did the terraform planSalvo
Can you do ls -la in this folder and post the results here? Also try pwd to list the current directory.Cultivated
Looks like there was some issue with the vpn connectivity and was not able to connect to aws system .. it is working now after making some changes.Salvo
M
4
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)
Mitchmitchael answered 20/9, 2020 at 11:53 Comment(1)
But what if you ARE in the folder and you still get this message?Laruelarum
H
0

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

Hasen answered 18/1, 2019 at 11:15 Comment(0)
T
0

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

Tina answered 11/8, 2021 at 6:56 Comment(1)
This is the same solution as in this other answer.Crider
M
0

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.

Mizzle answered 29/9, 2021 at 4:52 Comment(0)
I
0

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)!

Isocrates answered 25/3, 2022 at 14:32 Comment(0)
A
0

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.
Abramabramo answered 24/10, 2022 at 20:46 Comment(0)
P
0

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.

Phlegethon answered 1/8, 2024 at 4:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.