Learn how to fix the Terraform AWS provider error- failed to get shared config profile. Our AWS Support team is here to answer queries and concerns.
How to Fix “Failed to Get Shared Config Profile” Error in Terraform with AWS
If you have run into the error “Failed to get shared config profile” when working with Terraform and AWS, our Experts are here to help.
This error usually means Terraform can’t find or access the AWS profile we specified, either through environment variables or in the configuration files.
This error typically occurs due to issues with the configuration of AWS credentials or profiles. Here’s how to resolve it:
First, ensure the `AWS_PROFILE` environment variable is set to a valid profile name that exists in the `~/.aws/credentials` or `~/.aws/config` file.
For example:
export AWS_PROFILE=default
In this case, it was set to `terracase`, but my credentials file didn’t have a profile with that name. Switching to a valid profile fixed the error.
An Overview:
Understanding the Credential Lookup Order
Terraform follows a specific order of precedence:
- `provider.profile` in `main.tf`
- `AWS_PROFILE` environment variable
- Default profile in `~/.aws/credentials`
If no default profile exists, we have to reference the profile name.
Here is an example profile:
[https://sso.jumpcloud.com/saml2/aws]
aws_access_key_id = example-id
aws_secret_access_key = example-key
So, to use this, export the profile:
export AWS_PROFILE=https://sso.jumpcloud.com/saml2/aws
Or set it directly in `main.tf`:
provider "aws" {
region = "us-west-2"
profile = "https://sso.jumpcloud.com/saml2/aws"
}
Terraform AWS Provider V4 Changes
A key change in the AWS Provider v4 is that it fails if the specified profile doesn’t exist, unlike v3, which would fall back. This breaking change has tripped up many users, as also seen with other errors like AWS: Too Few Arguments.
So, to handle this in CI/CD pipelines, we can create a variable to toggle between local and pipeline credentials:
provider "aws" {
region = "us-west-2"
profile = var.pipeline ? "" : "my-local-profile"
}
Then set the `pipeline` variable to `true` in the pipeline environment, allowing Terraform to use default credentials or environment variables in CI, while keeping local profile support.
By properly managing the AWS credentials:
- Local and CI/CD environments work seamlessly.
- Also, errors related to profile access are eliminated.
- We avoid breaking changes from Terraform provider upgrades.
Common Fixes Recommended by Our Experts
- Remove quotes in profile names. This is valid for AWS CLI but not for Terraform:
[profile "project1-admin"] # Incorrect for Terraform
[project1-admin] # Correct
- Or, set `shared_credentials_file` directly in the provider block:
provider "aws" {
region = "us-east-1"
shared_credentials_files = ["C:/Users/johnny/.aws/credentials"]
profile = "johnny"
}
If you’re managing file transfers to AWS EC2, we also recommend reading how to access an AWS server using FileZilla for quick SFTP setup tips.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts demonstrated how to fix the Terraform AWS provider error- failed to get shared config profile.
0 Comments