skip to Main Content

So i have a school project where we have to create a EC2 instance with Terraform for AWS. So I created my Terraform File. At first I tipped in the terminal terraform init. When I want to execute "terraform plan" i always get the same output:


│ Error: Invalid provider configuration

│ Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration. Add a provider block to the root module and configure the provider’s required arguments as described in the provider
│ documentation.



│ Error: Invalid AWS Region:

│ with provider["registry.terraform.io/hashicorp/aws"],
│ on line 0:
│ (source code not available)

There is also another message with Invalid AWS Region.

My Terraform code looks like this at the moment:

terraform {
    required_providers {
        aws = {
            source = "hashicorp/aws"
            version = "~> 5.0"
        }
    }
}

resource "aws_key_pair" "deployment" {
  key_name = "SSH_m346_Terraform"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCX44s1O4dFwPmPuXre/GDF9CWcbW5PBn9PgF0f+SC9uhIJiSzXGx6sOQ9MAJSBzXdGKPM/YKOh9GYtq4fHKlwA2Rw3LXqxrgEv4pvh+j4wy7eA8ZhsvCfUv8RpRsqalGnQZ2j1aYs+F/LvFGlmOUh07xtTHPB888XL59EaHsDWGudUOQJiFhi3uxMPwp4C4djKqf1IVSEm/9CMYQBiOGJjFOJ2N3TTDQvTo4ez5BwOv5G5qgL380K3V1APcYYMXqWTZJr2+sv5+bF1Mp0+oKvuEOr76LbwV922OK0TmpRRYQ2kaX34foyskuHZ3put7WUrRlorAMTNL03K5QeaMDaR rsa-key-20240115"
}

resource "aws_vpc" "Terrafrom_m346_vpc" {
  cidr_block = "10.0.0.0/16"
  enable_dns_support = true
  enable_dns_hostnames = true
  tags = {
    Name = "Terra_M346"
  }
  
}

resource "aws_internet_gateway" "Terrafrom_Internet_Gateway_m346" {
  vpc_id = aws_vpc.Terrafrom_m346_vpc.id
}

resource "aws_subnet" "Terraform_Subnet_m346" {
  vpc_id = aws_vpc.Terrafrom_m346_vpc.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-east-1"
  map_public_ip_on_launch = true

  tags = {
    Name = "M346 Datenbank Subnetz"
  }
}

resource "aws_route_table" "m346_route_table" {
  vpc_id = aws_vpc.Terrafrom_m346_vpc.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.Terrafrom_Internet_Gateway_m346.id
  }
}

resource "aws_route_table_association" "m346_srv_subnet_association" {
  subnet_id = aws_subnet.Terraform_Subnet_m346.id
  route_table_id = aws_route_table.m346_route_table.id
}

resource "aws_security_group" "allow_ssh" {
  vpc_id = aws_vpc.Terrafrom_m346_vpc.id

  egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
  
  egress {
    from_port = 22
    to_port = 22
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

After I successfully executed the command "terraform init" it created a file named .terraform.lock.hcl and dictionary.
Can someone tell me what the problem is?

I want to run my terraform EC2 on AWS. I tried google the problem but the people who have this problem is a bit different from my problem.

2

Answers


  1. us-east-1 is a region, but you have it specified as an availability zone.
    Availability zones usually look like us-east-1a or us-east-1b.

    Also, try adding the following block to your code:

    provider "aws" {
      region = "us-east-1"
    }
    
    Login or Signup to reply.
  2. Configuration for the AWS Provider can be derived from several sources, which are applied in the following order:

    1. Parameters in the provider configuration (which is what the above answer suggests)
    2. Environment variables
    3. Shared credentials files
    4. Shared configuration files
    5. Container credentials
    6. Instance profile credentials and Region

    This order matches the precedence used by the AWS CLI and the AWS SDKs.

    My personal preference (totally up to you) is using the awscli configuration file, which is easy to modify is useful in general as a toll to manage AWS environments.

    Here are some instructions to set this up:

    The AWS Provider can source credentials and other settings from the shared configuration and credentials files. By default, these files are located at $HOME/.aws/config and $HOME/.aws/credentials on Linux and macOS, and "%USERPROFILE%.awscredentials" on Windows.

    If no named profile is specified, the default profile is used.

    The locations of the shared configuration and credentials files can be

    There are several ways to set your credentials and configuration setting using AWS CLI. We will use aws configure command:

    Run the following command to quickly set and view your credentials, region, and output format. The following example shows sample values:

    $ aws configure
    AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
    AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
    Default region name [None]: us-east-1
    Default output format [None]: json
    

    To list configuration data, use the aws configire list command. This command lists the profile, access key, secret key, and region configuration information used for the specified profile. For each configuration item, it shows the value, where the configuration value was retrieved, and the configuration variable name.

    You can find more information here:
    https://registry.terraform.io/providers/hashicorp/aws/latest/docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search