-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
129 lines (104 loc) · 2.89 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
locals {
instance_tags = merge(var.tags,
{ Name = var.name }
)
}
resource "aws_ebs_volume" "this" {
availability_zone = var.availability_zone
size = var.ebs_volume_size
tags = var.tags
type = var.ebs_volume_type
}
data "template_file" "init" {
template = file("${path.module}/cloudinit/init.cfg")
}
data "template_cloudinit_config" "this" {
base64_encode = true
gzip = true
# Main cloud-config configuration file.
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = data.template_file.init.rendered
}
part {
content_type = "text/cloud-boothook"
content = <<EOF
#!/bin/bash
exec > >(tee /var/log/user-data.log 2>/dev/console) 2>&1
INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
# wait for ebs volume to be attached
while true
do
# attach EBS (run multiple times in case the volume was still detaching elsewhere)
aws --region us-east-1 ec2 attach-volume --volume-id ${aws_ebs_volume.this.id} --instance-id $INSTANCE_ID --device /dev/xvdg
# see if the volume is mounted before proceeding
lsblk |grep xvdg
if [ $? -eq 0 ]
then
break
else
sleep 5
fi
done
sleep 2
# create fs if needed
/sbin/parted /dev/xvdg print 2>/dev/null |grep Linux
if [ $? -eq 0 ]
then
echo "Data partition found, ensuring it is mounted"
mount | grep /data
if [ $? -eq 1 ]
then
echo "Data partition not mounted, mounting and adding to fstab"
echo "/dev/xvdg1 /data xfs defaults,noatime 1 1" >> /etc/fstab
mount /data
fi
else
echo "Data partition not initialized. Initializing and moving base data volume"
parted -s /dev/xvdg mklabel gpt
parted -s /dev/xvdg mkpart primary xfs 0% 100%
while true
do
lsblk |grep xvdg1
if [ $? -eq 0 ]
then
break
else
sleep 5
fi
done
mkfs.xfs /dev/xvdg1
mount /dev/xvdg1 /mnt
rsync -a /data/ /mnt
umount /mnt
echo "Data partition initialized, mounting and adding to fstab"
echo "Data partition initialized, mounting and adding to fstab" > /dev/console
echo "/dev/xvdg1 /data xfs defaults,noatime 1 1" >> /etc/fstab
mount /data
fi
EOF
}
}
resource "aws_instance" "this" {
monitoring = var.enable_monitoring
iam_instance_profile = aws_iam_instance_profile.this.id
ami = var.instance_image
instance_type = var.instance_type
key_name = var.keypair
subnet_id = var.instance_subnet
tags = local.instance_tags
user_data_base64 = data.template_cloudinit_config.this.rendered
vpc_security_group_ids = concat(
[aws_security_group.this.id],
var.instance_additional_sgs,
)
root_block_device {
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
lifecycle {
ignore_changes = [user_data_base64, ami]
}
}