You are on page 1of 2

terraform {

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

# Configure the AWS Provider


provider "aws" {
region = "us-east-1"
}

# Configure the AWS Provider


resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "main" {


vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1"
}

resource "aws_security_group" "ssh" {


name_prefix = "ssh-"
vpc_id = aws_vpc.main.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["193.137.28.212/32"]
}
}

resource "aws_key_pair" "ssh_key" {


key_name = "my-ssh-key"
public_key = file("~/.ssh/id_rsa.pub")
}

resource "aws_instance" "ec2" {


ami = "ami-0947d2ba12ee1ff75"
instance_type = "t2.nano"
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.ssh.id]
key_name = aws_key_pair.ssh_key.key_name
user_data = <<-EOT
#!/bin/bash
sudo apt update -y
sudo apt upgrade -y
EOT

root_block_device {
volume_size = 5
}
}

output "public_ip" {
value = aws_instance.ec2.public_ip
}

You might also like