Search code examples
amazon-web-servicesamazon-ec2terraformterraform-provider-aws

Error: Invalid provider configuration / invalid AWS Region


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.


Solution

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