Skip to content

Commit 9e89d10

Browse files
Added Solution for AWS - Launch EC2 Web Instance Using Terraform (bregman-arie#10260)
Co-authored-by: Kranthi <devarapallykranthikumar@gmail.com>
1 parent 4425b6a commit 9e89d10

File tree

1 file changed

+55
-0
lines changed
  • topics/aws/exercises/launch_ec2_web_instance

1 file changed

+55
-0
lines changed

topics/aws/exercises/launch_ec2_web_instance/solution.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,58 @@ echo "<h1>I made it! This is is awesome!</h1>" > /var/www/html/index.html
3737
9. In the security group section, add a rule to accept HTTP traffic (TCP) on port 80 from anywhere
3838
10. Click on "Review" and then click on "Launch" after reviewing.
3939
11. If you don't have a key pair, create one and download it.
40+
41+
12. ### Solution using Terraform
42+
43+
```
44+
45+
provider "aws" {
46+
region = "us-east-1" // Or your desired region
47+
}
48+
49+
resource "aws_instance" "web_server" {
50+
ami = "ami-12345678" // Replace with the correct AMI for Amazon Linux 2
51+
instance_type = "t2.micro" // Or any instance type with 1 vCPU and 1 GiB memory
52+
53+
tags = {
54+
Name = "web-1"
55+
Type = "web"
56+
}
57+
58+
root_block_device {
59+
volume_size = 8 // Or any desired size
60+
delete_on_termination = true
61+
}
62+
63+
provisioner "remote-exec" {
64+
inline = [
65+
"sudo yum update -y",
66+
"sudo yum install -y httpd",
67+
"sudo systemctl start httpd",
68+
"sudo bash -c 'echo \"I made it! This is awesome!\" > /var/www/html/index.html'",
69+
"sudo systemctl enable httpd"
70+
]
71+
72+
connection {
73+
type = "ssh"
74+
user = "ec2-user"
75+
private_key = file("~/.ssh/your_private_key.pem") // Replace with the path to your private key
76+
host = self.public_ip
77+
}
78+
}
79+
80+
security_group_ids = [aws_security_group.web_sg.id]
81+
}
82+
83+
resource "aws_security_group" "web_sg" {
84+
name = "web_sg"
85+
description = "Security group for web server"
86+
87+
ingress {
88+
from_port = 80
89+
to_port = 80
90+
protocol = "tcp"
91+
cidr_blocks = ["0.0.0.0/0"]
92+
}
93+
}
94+
```

0 commit comments

Comments
 (0)