-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
khaled Taha
committed
Apr 10, 2023
1 parent
e3a4ad6
commit 9d8dead
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "4.52.0" | ||
} | ||
random = { | ||
source = "hashicorp/random" | ||
version = "3.4.3" | ||
} | ||
} | ||
required_version = ">= 1.1.0" | ||
|
||
} | ||
|
||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
|
||
resource "random_pet" "sg" {} | ||
|
||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "name" | ||
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] | ||
} | ||
|
||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
|
||
owners = ["099720109477"] # Canonical | ||
} | ||
|
||
resource "aws_instance" "web2" { | ||
ami = data.aws_ami.ubuntu.id | ||
instance_type = "t2.micro" | ||
subnet_id = "subnet-0f5e8a6adba3ccd7f" | ||
user_data = <<-EOF | ||
#!/bin/bash | ||
apt-get update | ||
apt-get install -y apache2 | ||
sed -i -e 's/80/8080/' /etc/apache2/ports.conf | ||
echo "Hello World" > /var/www/html/index.html | ||
systemctl restart apache2 | ||
EOF | ||
} | ||
|
||
resource "aws_security_group" "web-sg2" { | ||
name = "${random_pet.sg.id}-sg" | ||
ingress { | ||
from_port = 8080 | ||
to_port = 8080 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
// connectivity to ubuntu mirrors is required to run `apt-get update` and `apt-get install apache2` | ||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
output "web-address" { | ||
value = "${aws_instance.web.public_dns}:8080" | ||
} | ||
|
||
|