-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathterraform_aws_ec2_webapp.tf
79 lines (66 loc) · 1.55 KB
/
terraform_aws_ec2_webapp.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
data "aws_ami" "amazon_linux2_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "image-id"
values = ["ami-04d29b6f966df1537"]
}
}
resource "aws_security_group" "allow_webapp_traffic" {
name = "allow_webapp_traffic"
description = "Allow inbound traffic"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_my_laptop"
}
}
resource "aws_instance" "webapp" {
ami = data.aws_ami.amazon_linux2_ami.id
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_webapp_traffic.id]
key_name = "temp_key"
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install httpd -y
sudo service httpd start
sudo chkconfig httpd on
echo "<html><h1>Your terraform deployment worked !!!</h1></html>" | sudo tee /var/www/html/index.html
hostname -f >> /var/www/html/index.html
EOF
tags = {
Name = "myfirsttfinstance"
}
}
output "instance_ip" {
value = aws_instance.webapp.public_ip
}