I am trying to set up my first project with Terraform on AWS.
This is my main.tf:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
# Create a VPC
resource "aws_vpc" "first_vpc" {
# vpc_id = aws_vpc.first_vpc.id # An argument named "vpc_id" is not expected here.
cidr_block = "100.0.0.0/16"
}
# Create subnet for VPC
resource "aws_subnet" "first_subnet" {
vpc_id = aws_vpc.first_vpc.id
cidr_block = "100.200.0.0/24"
availability_zone = "us-east-1"
tags = {
Name = "My first subnet for Ubuntu server"
}
}
resource "aws_network_interface" "first_network_interface" {
subnet_id = aws_subnet.first_subnet.id
private_ips = ["100.200.0.100"]
tags = {
Name = "primary_network_interface"
}
}
# Use Ubuntu 22.04
resource "aws_instance" "first_ubuntuserver" {
ami = "ami-053b0d53c279acc90"
instance_type = "t3.micro"
network_interface {
network_interface_id = aws_network_interface.first_network_interface.id
device_index = 0
}
tags = {
Name = "My first Ubuntu on AWS instance"
}
}
And this an error which I see when I run #terraform apply/validate:
Error: creating EC2 Subnet: InvalidParameterValue: Value (us-east-1) for parameter availabilityZone is invalid. Subnets can currently only be created in the following availability zones: us-east-1a, us-east-1b, us-east-1c, us-east-1d, us-east-1e, us-east-1f.
│ status code: 400, request id: 16245777-37a1-404b-bfd1-91a3cbb4547e
│
│ with aws_subnet.first_subnet,
│ on main.tf line 22, in resource "aws_subnet" "first_subnet":
│ 22: resource "aws_subnet" "first_subnet" {
Would someone clarify what is going on here? I fill out each block one by one according to the documentation for a better understanding of what missing blocks lead to which errors. However over here I can not match what is missing according to the documentation: Terraform Registry
I run #terraform apply, then go the AWS account to check instances. There is my-first-web-server I open IPv4 Public address: 54.88.81.135 which does not work, why? I am getting error “This sie can’t be reached”.
I should see your very first web server. How to debug it? What to check?
Resolved. It works with http, not https (example: http://44.218.3.226/). However I have configured both HTTPs and HTTP in main.tf for Security Group, so why does it not work also for https://44.218.3.226/, and how to change on AWS to open a website with http by default?
Hey there, congratulations on successfully running your web server via TF.
Your browser trying HTTPS by default might be force HTTPS enable settings or similar security feature enabled inside the browser, its not from AWS or your web server.
HTTPS needs to be configured in your apache2 webserver with a valid domain and a certificate to work.
I can’t resolve the next issue with idea coming to my head (“Oh! Let’s add Load Balancer”). According to the documentation Terraform Registry I added block of code to the main.tf file and modified it a bit:
2023-09-04T21:28:32.481+0200 [ERROR] Checkpoint error: Get "https://checkpoint-api.hashicorp.com/v1/check/terraform?arch=386&os=windows&signature=f046a149-be7b-15f9-2e2c-f078fffd74eb&version=1.5.6": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
2023-09-04T21:28:35.279+0200 [ERROR] vertex "aws_lb.my_first_load_balancer" error: Reference to undeclared resource
╷
│ Error: Reference to undeclared resource
│
│ on main.tf line 228, in resource "aws_lb" "my_first_load_balancer":
│ 228: subnets = [for my_subnet_1 in aws_subnet.public : my_subnet_1.id]
│
│ A managed resource "aws_subnet" "public" has not been declared in the root module.
So I went to the Terraform docs again, but this time looking for aws_subnet resource: Terraform Registry , but I do not see any mention for public subnet creation. How to specify the public subnet resource to fix that issue?
Anyone would like to instruct me how to proceed with the above error?
I am practice Terraform so I am aware my mistakes, but not sure how to resolve each of them.
You cannot create public subnet resource as a type in terraform. You need to understand the difference between a public and a private subnet in AWS.
So a public subnet is the subnet that has a Internet Gateway attached to its route and private subnet has NAT gateway attached to the route. That’s the difference.
If you want to create a public subnet, you have to follow the same above procedure, i,e :
Create a subnet as above.
Create a route table
Attach the route table with that subnet
Create a route 0.0.0.0/0 into the already created internet gateway of your VPC.
If an Internet gateway doesn’t exist for your VPC, you need to create one.
Now, if you want to avoid doing these, use the VPC module, and it will do those for you.
Wow, it is amazing: Terraform Registry in one shot I create VPC, NAT, Net Int, DG and Subnets in over several lines of code. Prevously it was in over a dozens lines of code using resources.
Thanks for that advice. BTW, modules were on the learning agenda but for later. However, I skipped the other stuff to hop into modules it was a good idea to start it now.
And I really now start see the power of Terraform.