Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 4c41820

Browse files
committed
Compatible with old functions
1 parent 21e9795 commit 4c41820

File tree

8 files changed

+203
-71
lines changed

8 files changed

+203
-71
lines changed

examples/single-node-asg-tester/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ module "tester" {
4444
subnet_id = module.vpc.public_subnet_ids[0]
4545
security_group_ids = [aws_security_group.ssh.id]
4646
region = "ap-northeast-1"
47+
compatible_with_single_volume = false
4748
data_volumes = [{ name = "a", device = "/dev/xvdm", size = 50 }, { name = "b", device = "/dev/xvdn" }]
4849
}

modules/init-snippet-attach-ebs-volume/main.tf

+8-49
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,23 @@
99
*
1010
*/
1111

12-
# variables used by this snippet of init shellcode
13-
variable "device_paths" {
14-
description = "path, to the device's path in /dev/"
15-
type = list(string)
16-
}
17-
18-
variable "volume_ids" {
19-
description = "ID of the EBS volume to attach"
20-
type = list(string)
21-
}
22-
23-
variable "init_prefix" {
24-
default = ""
25-
description = "initial init (shellcode) to prefix this snippet with"
26-
type = string
27-
}
28-
29-
variable "init_suffix" {
30-
default = ""
31-
description = "init (shellcode) to append to the end of this snippet"
32-
type = string
33-
}
34-
35-
variable "log_level" {
36-
default = "info"
37-
description = "default log level verbosity for apps that support it"
38-
type = string
39-
}
40-
41-
variable "log_prefix" {
42-
default = "OPS: "
43-
description = "string to prefix log messages with"
44-
type = string
45-
}
46-
47-
variable "region" {
48-
description = "AWS region the volume is in"
49-
type = string
50-
}
51-
52-
variable "wait_interval" {
53-
default = "5"
54-
description = "time (in seconds) to wait when looping to find the device"
55-
type = number
12+
locals {
13+
device_paths = var.compatible_with_single_volume ? [var.device_path] : var.device_paths
14+
volume_ids = var.compatible_with_single_volume ? [var.volume_id] : var.volume_ids
5615
}
5716

5817
# render init script for a cluster using our generic template
5918
data "template_file" "init_snippet" {
60-
count = length(var.volume_ids)
19+
count = length(local.volume_ids)
6120

6221
template = file("${path.module}/snippet.tpl")
6322

6423
vars = {
65-
device_path = var.device_paths[count.index]
24+
device_path = local.device_paths[count.index]
6625
log_prefix = var.log_prefix
6726
log_level = var.log_level
6827
region = var.region
69-
volume_id = var.volume_ids[count.index]
28+
volume_id = local.volume_ids[count.index]
7029
wait_interval = var.wait_interval
7130
}
7231
}
@@ -78,8 +37,8 @@ data "template_file" "instance_id" {
7837
output "init_snippet" {
7938
value = <<EOF
8039
${var.init_prefix}
81-
data.template_file.instance_id.rendered
82-
data.template_file.init_snippet.*.rendered
40+
${data.template_file.instance_id.rendered}
41+
${join("\n", data.template_file.init_snippet.*.rendered)}
8342
${var.init_suffix}
8443
EOF
8544
}

modules/init-snippet-attach-ebs-volume/snippet.tpl

+7-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ while ! aws ec2 attach-volume \
99
sleep '${wait_interval}'
1010
done
1111
echo "${log_prefix} $${VOLUME_ID} attached."
12+
sleep '${wait_interval}' # Wait for device up
1213

13-
vol_id="$(echo "$${VOLUME_ID}" | tr -d '-')"
14-
while [ ! -e /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} ]; do
15-
sleep '${wait_interval}'
16-
done
17-
18-
dev_id="$(ls /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} | head -1)"
19-
dev_name="/dev/$(readlink "$${dev_id}" | tr / '\n' | tail -1)"
20-
[ "$${dev_name}" == "${device_path}" ] || ln -s "$${dev_name}" "${device_path}"
14+
if [ ! -e ${device_path} ]; then
15+
vol_id="$(echo "$${VOLUME_ID}" | tr -d '-')"
16+
dev_id="$(ls /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} | head -1)"
17+
dev_name="/dev/$(readlink "$${dev_id}" | tr / '\n' | tail -1)"
18+
[ "$${dev_name}" == "${device_path}" ] || ln -s "$${dev_name}" "${device_path}"
19+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
variable "device_path" {
2+
default = "/dev/xvdf"
3+
description = "path, to the device's path in /dev/"
4+
type = string
5+
}
6+
7+
variable "device_paths" {
8+
type = list(string)
9+
description = "paths, to the device's path in /dev/"
10+
default = []
11+
}
12+
13+
variable "init_prefix" {
14+
default = ""
15+
description = "initial init (shellcode) to prefix this snippet with"
16+
type = string
17+
}
18+
19+
variable "init_suffix" {
20+
default = ""
21+
description = "init (shellcode) to append to the end of this snippet"
22+
type = string
23+
}
24+
25+
variable "log_level" {
26+
default = "info"
27+
description = "default log level verbosity for apps that support it"
28+
type = string
29+
}
30+
31+
variable "log_prefix" {
32+
default = "OPS: "
33+
description = "string to prefix log messages with"
34+
type = string
35+
}
36+
37+
variable "region" {
38+
description = "AWS region the volume is in"
39+
type = string
40+
}
41+
42+
variable "wait_interval" {
43+
default = "5"
44+
description = "time (in seconds) to wait when looping to find the device"
45+
type = number
46+
}
47+
48+
variable "volume_id" {
49+
description = "ID of the EBS volume to attach"
50+
type = string
51+
default = ""
52+
}
53+
54+
variable "volume_ids" {
55+
description = "IDs of the EBS volumes to attach"
56+
type = list(string)
57+
default = []
58+
}
59+
60+
variable "compatible_with_single_volume" {
61+
default = true
62+
description = "Using variables for single volumes or not."
63+
}

modules/persistent-ebs/locals.tf

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ locals {
77
kms_key_id = ""
88
snapshot_id = ""
99
}
10-
volumes_default = [for x in var.volumes : merge(local.volume_default, x)]
10+
volumes_default_no_comp = [for x in var.volumes : merge(local.volume_default, x)]
11+
volumes_default = var.compatible_with_single_volume ? [{
12+
type = var.volume_type,
13+
iops = var.iops,
14+
size = var.size,
15+
encrypted = var.encrypted,
16+
kms_key_id = var.kms_key_id,
17+
snapshot_id = var.snapshot_id,
18+
name = "default"
19+
}] : local.volumes_default_no_comp
1120
}

modules/persistent-ebs/variables.tf

+51-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,42 @@ variable "az" {
1414
type = string
1515
}
1616

17+
variable "volume_type" {
18+
default = "gp2"
19+
description = "Type of EBS volume to use for the EBS block device"
20+
type = string
21+
}
22+
23+
variable "size" {
24+
default = "15"
25+
description = "Size (in GB) of EBS volume to use for the EBS block device"
26+
type = string
27+
}
28+
29+
variable "snapshot_id" {
30+
default = ""
31+
description = "The ID of the snapshot to base the EBS block device on"
32+
type = string
33+
}
34+
35+
variable "encrypted" {
36+
default = true
37+
description = "Boolean, whether or not to encrypt the EBS block device"
38+
type = string
39+
}
40+
41+
variable "kms_key_id" {
42+
default = ""
43+
description = "ID of the KMS key to use when encyprting the EBS block device"
44+
type = string
45+
}
46+
47+
variable "iops" {
48+
default = ""
49+
description = "The amount of IOPS to provision for the EBS block device"
50+
type = string
51+
}
52+
1753
variable "extra_tags" {
1854
default = {}
1955
description = "Map with extra tags to be applied to all ebs volumes"
@@ -26,14 +62,20 @@ variable "iam_instance_profile_role_name" {
2662
}
2763

2864
variable "volumes" {
29-
type = list(object({
30-
type = string,
31-
iops = number,
32-
size = number,
33-
encrypted = bool,
34-
kms_key_id = string,
35-
snapshot_id = string,
36-
name = string
37-
}))
65+
type = list(map(any))
66+
### Note: what should be contained.
67+
# type = string,
68+
# iops = number,
69+
# size = number,
70+
# encrypted = bool,
71+
# kms_key_id = string,
72+
# snapshot_id = string,
73+
# name = string
3874
description = "Definition of volumes. `name` is required."
75+
default = []
76+
}
77+
78+
variable "compatible_with_single_volume" {
79+
default = true
80+
description = "Using variables for single volumes or not."
3981
}

modules/single-node-asg/main.tf

+21-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ locals {
3434
snapshot_id = ""
3535
}
3636

37-
data_volumes_default = [for x in var.data_volumes : merge(local.data_volume_default, x)]
37+
data_volumes_default_no_comp = [for x in var.data_volumes : merge(local.data_volume_default, x)]
38+
data_volumes_default = var.compatible_with_single_volume ? [{
39+
type = var.data_volume_type,
40+
iops = var.data_volume_iops,
41+
size = var.data_volume_size,
42+
encrypted = var.data_volume_encrypted,
43+
kms_key_id = var.data_volume_kms_key_id,
44+
snapshot_id = var.data_volume_snapshot_id,
45+
name = "${local.name_prefix_with_az}-default",
46+
device = "/dev/xvdf"
47+
}] : local.data_volumes_default_no_comp
3848
}
3949

4050
# Create an IAM Instance profile we can use on EC2, associated with the ASG
@@ -50,6 +60,7 @@ module "service-data" {
5060
region = var.region
5161
az = local.az
5262
volumes = local.data_volumes_default
63+
compatible_with_single_volume = false
5364

5465
# EBS module will create an IAM policy and associate with this role
5566
iam_instance_profile_role_name = module.instance_profile.iam_role_name
@@ -83,8 +94,10 @@ module "server" {
8394
user_data = <<END_INIT
8495
#!/bin/bash
8596
# exec > /tmp/init.log
86-
# exec 2> /tmp/init-err.log
97+
exec 2>&1
8798
# set -x
99+
apt update
100+
${module.install-awscli.init_snippet}
88101
${var.init_prefix}
89102
${module.init-attach-ebs.init_snippet}
90103
${var.init_suffix}
@@ -97,5 +110,10 @@ module "init-attach-ebs" {
97110
source = "../init-snippet-attach-ebs-volume"
98111
region = var.region
99112
volume_ids = module.service-data.volume_ids
100-
device_paths = [for x in var.data_volumes : x.device]
113+
device_paths = [for x in local.data_volumes_default : x.device]
114+
compatible_with_single_volume = false
115+
}
116+
117+
module "install-awscli" {
118+
source = "../init-snippet-install-awscli"
101119
}

modules/single-node-asg/variables.tf

+42-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,44 @@ variable "root_volume_type" {
3636
}
3737

3838
variable "root_volume_size" {
39-
default = "8"
39+
default = 8
4040
description = "Size (in GB) of EBS volume to use for the root block device"
41+
type = number
42+
}
43+
44+
variable "data_volume_type" {
45+
default = "gp2"
46+
description = "Type of EBS volume to use for the EBS volume"
47+
type = string
48+
}
49+
50+
variable "data_volume_size" {
51+
default = 10
52+
description = "Size (in GB) of EBS volume to use for the EBS volume"
53+
type = number
54+
}
55+
56+
variable "data_volume_encrypted" {
57+
default = true
58+
description = "Boolean, whether or not to encrypt the EBS block device"
59+
type = string
60+
}
61+
62+
variable "data_volume_kms_key_id" {
63+
default = ""
64+
description = "ID of the KMS key to use when encyprting the EBS block device"
65+
type = string
66+
}
67+
68+
variable "data_volume_snapshot_id" {
69+
default = ""
70+
description = "The ID of the snapshot to base the EBS block device on"
71+
type = string
72+
}
73+
74+
variable "data_volume_iops" {
75+
default = ""
76+
description = "The amount of IOPS to provision for the EBS block device"
4177
type = string
4278
}
4379

@@ -84,3 +120,8 @@ variable "data_volumes" {
84120
type = list(map(any))
85121
description = "Definition of the data volumes. `name` and `device` are required."
86122
}
123+
124+
variable "compatible_with_single_volume" {
125+
default = true
126+
description = "Using variables for single volumes or not."
127+
}

0 commit comments

Comments
 (0)