-
Notifications
You must be signed in to change notification settings - Fork 2
/
7-launchtemplate.tf
69 lines (59 loc) · 2.03 KB
/
7-launchtemplate.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
resource "aws_launch_template" "T4Gapp1_LT" {
name_prefix = "T4Gapp1_LT"
image_id = "ami-079db87dc4c10ac91"
instance_type = "t2.micro"
key_name = "OurGoldBox"
vpc_security_group_ids = [aws_security_group.T4Gapp1-80-sg01-servers.id]
user_data = base64encode(<<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
# Get the IMDSv2 token
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
# Background the curl requests
curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/local-ipv4 &> /tmp/local_ipv4 &
curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/placement/availability-zone &> /tmp/az &
curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/ &> /tmp/macid &
wait
macid=$(cat /tmp/macid)
local_ipv4=$(cat /tmp/local_ipv4)
az=$(cat /tmp/az)
vpc=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$macid/vpc-id)
# Create HTML file
cat <<-HTML > /var/www/html/index.html
<!doctype html>
<html lang="en" class="h-100">
<head>
<title>Details for EC2 instance</title>
</head>
<body>
<div>
<h1>Tech For Gold investors</h1>
<h1>We invest in getting money through residual income </h1>
<p><b>Instance Name:</b> $(hostname -f) </p>
<p><b>Instance Private Ip Address: </b> $local_ipv4</p>
<p><b>Availability Zone: </b> $az</p>
<p><b>Virtual Private Cloud (VPC):</b> $vpc</p>
</div>
</body>
</html>
HTML
# Clean up the temp files
rm -f /tmp/local_ipv4 /tmp/az /tmp/macid
EOF
)
tag_specifications {
resource_type = "instance"
tags = {
Name = "T4Gapp1_LT"
Service = "GetMoneyApp1"
Group = "TechForGold"
Planet = "RedPill"
}
}
lifecycle {
create_before_destroy = true
}
}