Error: configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.(setup was working fine)

My terraform setup was running fine with Jenkins but then suddenly I am seeing this error when running the Jenkinsfile (see below). But the problem is not with the Jenkinsfile because even when I run this without Jenkins now, I see the same error.

The only modifications I was making was to the Jenkinsfile at the time where the config file and the credentials file were removed.

I went to the /home/ubuntu/.aws/credentials and the file is completely missing (config file is also missing). How could the file suddenly be removed?

Here is the error in the Jenkins script and below that without Jenkins running terraform from the terminal:(so it is not Jenkins it is a local issue of the credentials not being re-generated).

I was making no changes to providers.tf file when this happened just the Jenkinsfile and the jenkinsfile is using AWS_SHARED_CREDENTIALS_FILE=‘/home/ubuntu/.aws/credentials’
as ENV variable.

Plan: 2 to add, 0 to change, 0 to destroy.

Error: configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.

Please see Terraform Registry
for more information about providing credentials.

AWS Error: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, http response error StatusCode: 404, request to EC2 IMDS failed

with provider[“Terraform Registry”],
on providers.tf line 13, in provider “aws”:
13: provider “aws” {

==========
this is without Jenkins

dave:~/environment/mtc-terraform-ansible-jenkins (dev_terraform_FULL_ansible) $ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:

  • create

Terraform planned the following actions, but then encountered a problem:

random_id.mtc_compute_node_id[0] will be created

  • resource “random_id” “mtc_compute_node_id” {
    • b64_std = (known after apply)
    • b64_url = (known after apply)
    • byte_length = 2
    • dec = (known after apply)
    • hex = (known after apply)
    • id = (known after apply)
      }

random_id.random will be created

  • resource “random_id” “random” {
    • b64_std = (known after apply)
    • b64_url = (known after apply)
    • byte_length = 2
    • dec = (known after apply)
    • hex = (known after apply)
    • id = (known after apply)
      }

Plan: 2 to add, 0 to change, 0 to destroy.

│ Error: configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.

│ Please see Terraform Registry
│ for more information about providing credentials.

│ AWS Error: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, http response error StatusCode: 404, request to EC2 IMDS failed


│ with provider[“Terraform Registry”],
│ on providers.tf line 13, in provider “aws”:
│ 13: provider “aws” {

==========
My providers.tf does use the shared_credentials_files

provider “aws” {
region = “us-west-1”

shared_credentials_files=[“/home/ubuntu/.aws/credentials”]
}

======

dave:~/environment $ aws configure list
Name Value Type Location
---- ----- ---- --------
profile None None
access_key None None
secret_key None None
region None None

=====

I am on AWS Cloud9. The AWS CLI is installed automatically on this Cloud9 with my login credentials. I know I can re-create the files manually but can someone help me out so that I do it correctly. I also see there is an aws configure command that can do this for me but there is also a aws configure set aws_session_token but where do I get the token (ephemeral).

What could have happened here?

warmest regards

Dave

I resolved this issue (at least for now). I still do not know why or how the credentials file got deleted but AWS Cloud9 will disable this function if there is a new user added to access Cloud9 Environment until the owner of the env approves the user. Then the owner can re-enable it. When Cloud9 disables this function the file is removed/deleted. I did not add any new users for accessibility but I am guessing that the Jenkins access via the shared credentials file in providers.tf and/or via AWS_SHARED_CREDENTIALS_FILE in the Jenkinsfile spooked the Cloud9 into disabling it ???

It was disabled when I pulled up the Cloud9 env preferences.

To re-enable it use this url as a resource

Here is the relevant info:

“• If the AWS managed temporary credentials setting is set to off, whenever you turn it back on. (To view or change this setting, choose AWS Cloud9, Preferences in the menu bar of the IDE. On the Preferences tab, in the navigation pane, choose AWS Settings, Credentials.)”

the same can be done via the API as well.

warmest regards Dave