I’m facing a timeout error when I run terraform apply. Here’s what I’m trying to do:
I want to host my app on an Ubuntu server. To do this, I’m creating all the required resources using Terraform. However, when I run terraform apply, I encounter the following error
│ timeout – last error: dial tcp 3.81.61.64:22: i/o timeout
code:
https://privatebin.net/?78a142719701b8ed#6dU3aLZdL9DEBBNEPbYmAokEbQT71ZaH17yJS8nTUvMx
# Specify the AWS provider and region for deploying the resources
provider "aws" {
region = "us-east-1" # AWS region where resources will be created
}
# Create an AWS key pair to allow SSH access to EC2 instances
resource "aws_key_pair" "keyone" {
key_name = "my-key" # Name of the key pair
public_key = file("C:/Users/.ssh/xsec.pub") # Path to the public key file
}
# Create a VPC (Virtual Private Cloud)
resource "aws_vpc" "cityvpc" {
cidr_block = var.cidr # CIDR block for the VPC, passed via a variable
enable_dns_support = true
enable_dns_hostnames = true
}
# Create a subnet within the VPC
resource "aws_subnet" "citysub" {
cidr_block = var.cidr_256 # Subnet CIDR block, passed via a variable
vpc_id = aws_vpc.cityvpc.id
tags = {
Name = "citysub"
}
}
# Create an internet gateway to allow public internet access
resource "aws_internet_gateway" "citygate" {
vpc_id = aws_vpc.cityvpc.id
tags = {
Name = "citygate"
}
}
# Create a route table to define routing rules
resource "aws_route_table" "cityroute" {
vpc_id = aws_vpc.cityvpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.citygate.id
}
}
# Associate the route table with the subnet
resource "aws_route_table_association" "cityrouteassn" {
route_table_id = aws_route_table.cityroute.id
subnet_id = aws_subnet.citysub.id
}
# Create a security group in the VPC
resource "aws_security_group" "citysecurity" {
name = "TLS Allow"
description = "Allow TLS traffic"
vpc_id = aws_vpc.cityvpc.id
}
# Ingress rule to allow HTTP traffic (port 80)
resource "aws_vpc_security_group_ingress_rule" "allowHTTP" {
security_group_id = aws_security_group.citysecurity.id
cidr_ipv4 = "0.0.0.0/0"
from_port = 80
to_port = 80
ip_protocol = "tcp"
}
# Ingress rule to allow SSH traffic (port 22)
resource "aws_vpc_security_group_ingress_rule" "allowSSH" {
security_group_id = aws_security_group.citysecurity.id
cidr_ipv4 = "0.0.0.0/0"
from_port = 22
to_port = 22
ip_protocol = "tcp"
}
# Create an EC2 instance
resource "aws_instance" "myinstance" {
ami = "ami-0e2c8caa4b6378d8c"
instance_type = "t2.micro"
subnet_id = aws_subnet.citysub.id
key_name = aws_key_pair.keyone.id
associate_public_ip_address = true
# SSH connection settings
connection {
type = "ssh"
user = "admin"
private_key = file("C:\Users\.ssh\xsec")
host = self.public_ip
}
# Provisioner to copy a file
provisioner "file" {
source = "app.py"
destination = "/home/admin/app.py"
}
# Provisioner to run remote commands
provisioner "remote-exec" {
inline = [
"echo 'Hello from remote server'",
"sudo apt-get update -y",
"sudo apt-get install python3-pip -y"
]
}
}
New contributor
itsmepillai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.