7
votes

I am new to Terraform. I am writing a small script to put a small datafile from my machine to aws S3 bucket.. but I am getting below error.

Terraform file code:-

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"
}

Error on Terminal

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.

$ 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.

Also I have setup my default AWS Access Key ID and value.

Please help. Thanks!!

4
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 executed - Peter Berg

4 Answers

28
votes

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.

3
votes
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)
0
votes

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

0
votes

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