├── main.tf └── README.md /main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-west-2" #you can change to any region 3 | } 4 | 5 | # Fetch the latest Ubuntu AMI ID from the SSM parameter store 6 | data "aws_ssm_parameter" "ubuntu_ami" { 7 | name = "/aws/service/canonical/ubuntu/server/jammy/stable/current/amd64/hvm/ebs-gp2/ami-id" 8 | } 9 | 10 | # Create a VPC with a CIDR block 11 | resource "aws_vpc" "main" { 12 | cidr_block = "10.0.0.0/16" 13 | } 14 | 15 | # Create an Internet Gateway to allow internet access 16 | resource "aws_internet_gateway" "igw" { 17 | vpc_id = aws_vpc.main.id 18 | } 19 | 20 | # Create a public subnet within the VPC 21 | resource "aws_subnet" "public_subnet" { 22 | vpc_id = aws_vpc.main.id 23 | cidr_block = "10.0.1.0/24" 24 | availability_zone = "us-west-2a" 25 | map_public_ip_on_launch = true 26 | } 27 | 28 | # Create a route table for the public subnet 29 | resource "aws_route_table" "public_rt" { 30 | vpc_id = aws_vpc.main.id 31 | 32 | # Route all outbound traffic (0.0.0.0/0) to the Internet Gateway 33 | route { 34 | cidr_block = "0.0.0.0/0" 35 | gateway_id = aws_internet_gateway.igw.id 36 | } 37 | } 38 | 39 | # Associate the route table with the public subnet 40 | resource "aws_route_table_association" "public_rt_assoc" { 41 | subnet_id = aws_subnet.public_subnet.id 42 | route_table_id = aws_route_table.public_rt.id 43 | } 44 | 45 | # Create a security group to allow HTTP and SSH access 46 | resource "aws_security_group" "web_sg" { 47 | name_prefix = "web-sg-" 48 | 49 | vpc_id = aws_vpc.main.id 50 | 51 | # Allow HTTP access 52 | ingress { 53 | from_port = 80 54 | to_port = 80 55 | protocol = "tcp" 56 | cidr_blocks = ["0.0.0.0/0"] 57 | } 58 | 59 | # Allow SSH access 60 | ingress { 61 | from_port = 22 62 | to_port = 22 63 | protocol = "tcp" 64 | cidr_blocks = ["0.0.0.0/0"] 65 | } 66 | 67 | # Allow all outbound traffic 68 | egress { 69 | from_port = 0 70 | to_port = 0 71 | protocol = "-1" 72 | cidr_blocks = ["0.0.0.0/0"] 73 | } 74 | } 75 | 76 | # Launch an EC2 instance with the specified AMI and instance type 77 | resource "aws_instance" "legacy_web_server" { 78 | ami = data.aws_ssm_parameter.ubuntu_ami.value 79 | instance_type = "t2.micro" 80 | subnet_id = aws_subnet.public_subnet.id 81 | key_name = "vockey" # Replace with your actual key pair name 82 | vpc_security_group_ids = [aws_security_group.web_sg.id] 83 | 84 | # User data script to configure the instance on launch 85 | user_data = <<-EOF 86 | #!/bin/bash 87 | apt-get update -y 88 | apt-get install -y apache2 php php-mysql mysql-server 89 | systemctl start apache2 90 | systemctl enable apache2 91 | systemctl start mysql 92 | systemctl enable mysql 93 | 94 | # Setup MySQL database and user for WordPress 95 | mysql -e "CREATE DATABASE wordpress;" 96 | mysql -e "CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';" 97 | mysql -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';" 98 | mysql -e "FLUSH PRIVILEGES;" 99 | 100 | # Download and extract WordPress 101 | cd /var/www/html 102 | wget https://wordpress.org/latest.tar.gz 103 | tar -xzf latest.tar.gz 104 | mv wordpress/* . 105 | rm -rf wordpress latest.tar.gz 106 | chown -R www-data:www-data /var/www/html 107 | chmod -R 755 /var/www/html 108 | 109 | # Create the wp-config.php file 110 | cat > /var/www/html/wp-config.php <<-EOF2 111 | /var/www/html/wp-config.php <<-EOF2 130 |