skip to Main Content

I am a beginner to terraform and my research leads me to believe the below should work, but it doesn’t.

Error:

terraform plan
╷
│ Error: Reference to undeclared input variable
│
│   on main.tf line 2, in provider "aws":
│    2:   access_key = var.aws_access_key_id
│
│ An input variable with the name "aws_access_key_id" has not been declared. This variable can be declared with a variable "aws_access_key_id" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on main.tf line 3, in provider "aws":
│    3:   secret_key = var.aws_secret_access_key
│
│ An input variable with the name "aws_secret_access_key" has not been declared. This variable can be declared with a variable "aws_secret_access_key" {} block.
╵

main.tf

cat main.tf
provider "aws" {
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_access_key
  region     = "us-east-1"
}

resource "aws_instance" "my_rhel9_vm" {
  ami           = var.ami //RHEL 9 AMI
  instance_type = var.type
  count         = var.number_of_instances
  key_name      = var.amu.key_pair_name

  tags = {
    Name = "${var.name_tag}"
  }
}

My export commands I tried in the same shell where I ran terraform plan

export TF_VAR_aws_access_key_id="xx"
export TF_VAR_aws_secret_access_key="yy"

How should I be configuring the above to use the environment variables?

2

Answers


  1. The error message explains that the issue is that neither variable has been declared within the module where it is being referenced for attempted value resolution, and this is especially important for a declarative language such as the Terraform derivative of HCL2. The quickest path forward is given in the error message:

    variable "aws_access_key_id" {}
    variable "aws_secret_access_key" {}
    

    and these are typically placed in a variables.tf config file within the corresponding module. More information can be found in the documentation for variables.

    Login or Signup to reply.
  2. Environment Variables:
    You can provide your credentials via the

    AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
    

    Environment variables, representing your AWS Access Key and AWS Secret Key, respectively. Note that setting your AWS credentials using either these (or legacy) environment variables will override the use of AWS_SHARED_CREDENTIALS_FILE and AWS_PROFILE. The AWS_DEFAULT_REGION and AWS_SESSION_TOKEN environment variables are also used, if applicable:

    provider "awscc" {}
    Usage:

    export AWS_ACCESS_KEY_ID=""
    export AWS_SECRET_ACCESS_KEY=""
    export AWS_DEFAULT_REGION="us-east-1"
    terraform plan
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search