-
Notifications
You must be signed in to change notification settings - Fork 1
/
07-LaunchTemplate.tf
69 lines (59 loc) · 2 KB
/
07-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" "app1_LT" {
name_prefix = "app1_LT"
image_id = "ami-06d4b7182ac3480fa"
instance_type = "t2.micro"
key_name = "MyLinuxBox"
vpc_security_group_ids = [aws_security_group.app1-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>League of Shadows</h1>
<h1>Does Class 5 groups feel in charge?</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 = "app1_LT"
Service = "application1"
Owner = "Chewbacca"
Planet = "Mustafar"
}
}
lifecycle {
create_before_destroy = true
}
}