File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
topics/aws/exercises/launch_ec2_web_instance Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -37,3 +37,58 @@ echo "<h1>I made it! This is is awesome!</h1>" > /var/www/html/index.html
37
37
9 . In the security group section, add a rule to accept HTTP traffic (TCP) on port 80 from anywhere
38
38
10 . Click on "Review" and then click on "Launch" after reviewing.
39
39
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
+ ```
You can’t perform that action at this time.
0 commit comments