This repository was archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Support multiple EBS volumes attached and persisted to single node ASG. #319
Open
Magicloud
wants to merge
8
commits into
master
Choose a base branch
from
many_ebs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b5b03a
Support multiple data volumes in single-node-asg module.
Magicloud b98b93b
Example single-node-asg-tester: Add Makefile/README
Magicloud 71e59dd
drop name param on alb and alb-default-forward, use tags instead
ketzacoatl 73516b0
asg: make name_suffix optional
ketzacoatl 1e5157d
single-node-asg: fixup instance-profile module name
ketzacoatl be3222b
single-node-asg: make name_suffix optional
ketzacoatl e59a3e5
single-node-asg: fixup instance-profile module name - squash me
ketzacoatl 335b5af
oops
ketzacoatl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
.PHONY: init ssh-key apply destroy clean | ||
|
||
.DEFAULT_GOAL = help | ||
|
||
## Runs terraform get and terraform init for env | ||
init: | ||
@terraform get | ||
@terraform init | ||
|
||
## Create ssh key | ||
ssh-key: | ||
@ssh-keygen -q -N "" -C "SSH key for vpc-scenario-1 example" -f ./id_rsa | ||
|
||
## use 'terraform apply' to apply the setup. | ||
apply: | ||
@terraform apply | ||
|
||
## use 'terraform destroy' to remove all resources from AWS | ||
destroy: | ||
@terraform destroy | ||
|
||
## rm -rf all files and state | ||
clean: | ||
@rm -f id_rsa | ||
@rm -f id_rsa.pub | ||
@rm -f terraform.*.backup | ||
@rm -f terraform.tfstate | ||
|
||
## Show help screen. | ||
help: | ||
@echo "Please use \`make <target>' where <target> is one of\n\n" | ||
@awk '/^[a-zA-Z\-\_0-9]+:/ { \ | ||
helpMessage = match(lastLine, /^## (.*)/); \ | ||
if (helpMessage) { \ | ||
helpCommand = substr($$1, 0, index($$1, ":")-1); \ | ||
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ | ||
printf "%-30s %s\n", helpCommand, helpMessage; \ | ||
} \ | ||
} \ | ||
{ lastLine = $$0 }' $(MAKEFILE_LIST) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# single node asg tester | ||
|
||
This example shows the basic usage of `single-node-asg` module, especially the multiple EBS attachments. | ||
|
||
The module keeps one and only one instance up at all time. And the EBS volumes are reattached when a new instance is up. Hence they are always accessible. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
locals { | ||
cidr = "192.168.0.0/16" | ||
private_subnet_cidrs = ["192.168.100.0/24", "192.168.101.0/24"] | ||
public_subnet_cidrs = ["192.168.0.0/24", "192.168.1.0/24"] | ||
region = "ap-northeast-1" | ||
} | ||
|
||
data "aws_availability_zones" "azs" { } | ||
|
||
module "vpc" { | ||
source = "fpco/foundation/aws//modules/vpc-scenario-2" | ||
cidr = local.cidr | ||
public_subnet_cidrs = local.public_subnet_cidrs | ||
private_subnet_cidrs = local.private_subnet_cidrs | ||
azs = data.aws_availability_zones.azs.names | ||
name_prefix = "test" | ||
region = local.region | ||
} | ||
|
||
module "ubuntu" { | ||
source = "fpco/foundation/aws//modules/ami-ubuntu" | ||
} | ||
|
||
resource "aws_key_pair" "main" { | ||
public_key = file("./id_rsa.pub") | ||
} | ||
|
||
resource "aws_security_group" "ssh" { | ||
vpc_id = module.vpc.vpc_id | ||
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"] | ||
} | ||
} | ||
|
||
module "tester" { | ||
source = "../../modules/single-node-asg" | ||
name_prefix = "ebs" | ||
name_suffix = "test" | ||
key_name = aws_key_pair.main.key_name | ||
ami = module.ubuntu.id | ||
instance_type = "t2.micro" | ||
subnet_id = module.vpc.public_subnet_ids[0] | ||
security_group_ids = [aws_security_group.ssh.id] | ||
region = local.region | ||
data_volumes = [{ name = "a", device = "/dev/xvdm", size = 50 }, { name = "b", device = "/dev/xvdn" }] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
locals { | ||
name_prefix_with_suffix = "${var.name_prefix}-${var.name_suffix}" | ||
name_prefix_without_suffix = "${var.name_prefix}" | ||
|
||
name_prefix = "${var.name_suffix != "" ? local.name_prefix_without_suffix : local.name_prefix_with_suffix}" | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
if which wget; then | ||
INSTANCE_ID="$(wget -O- http://169.254.169.254/latest/meta-data/instance-id)" | ||
elif which curl; then | ||
INSTANCE_ID="$(curl http://169.254.169.254/latest/meta-data/instance-id)" | ||
fi | ||
|
||
if [ "x$${INSTANCE_ID}" == "x" ]; then | ||
echo 'There is no wget or curl tool installed. Hence bootstrap cannot get instance ID.' | ||
exit 1 | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,19 @@ | ||
# start snippet - attach EBS volume | ||
${init_prefix} | ||
export AWS_DEFAULT_REGION=${region} | ||
VOLUME_ID=${volume_id} | ||
if which wget; then | ||
INSTANCE_ID="$(wget -O- http://169.254.169.254/latest/meta-data/instance-id)" | ||
elif which curl; then | ||
INSTANCE_ID="$(curl http://169.254.169.254/latest/meta-data/instance-id)" | ||
fi | ||
echo "${log_prefix} will attach $${VOLUME_ID} via the AWS API in ${region}" | ||
while ! aws ec2 attach-volume \ | ||
--volume-id "$${VOLUME_ID}" \ | ||
--instance-id "$${INSTANCE_ID}" \ | ||
--device '${device_path}'; do | ||
echo "Attaching command failed to run. Retrying." | ||
sleep '${wait_interval}' | ||
done | ||
echo "${log_prefix} $${VOLUME_ID} attached." | ||
sleep '${wait_interval}' # Wait for device up | ||
|
||
if [ "x$${INSTANCE_ID}" == "x" ]; then | ||
echo 'OS not functioning' | ||
else | ||
echo "${log_prefix} will attach $${VOLUME_ID} via the AWS API in ${region}" | ||
while ! aws ec2 attach-volume \ | ||
--volume-id "$${VOLUME_ID}" \ | ||
--instance-id "$${INSTANCE_ID}" \ | ||
--device '${device_path}'; do | ||
echo "Attaching command failed to run. Retrying." | ||
sleep '${wait_interval}' | ||
done | ||
echo "${log_prefix} $${VOLUME_ID} attached." | ||
|
||
if [ ! -e ${device_path} ]; then | ||
vol_id="$(echo "$${VOLUME_ID}" | tr -d '-')" | ||
while [ ! -e /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} ]; do | ||
sleep '${wait_interval}' | ||
done | ||
|
||
dev_id="$(ls /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} | head -1)" | ||
dev_name="/dev/$(readlink "$${dev_id}" | tr / '\n' | tail -1)" | ||
[ "$${dev_name}" == "${device_path}" ] || ln -s "$${dev_name}" "${device_path}" | ||
fi | ||
|
||
${init_suffix} | ||
Magicloud marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
variable "device_paths" { | ||
type = list(string) | ||
description = "paths, to the device's path in /dev/" | ||
default = [] | ||
} | ||
|
||
variable "init_prefix" { | ||
default = "" | ||
description = "initial init (shellcode) to prefix this snippet with" | ||
type = string | ||
} | ||
|
||
variable "init_suffix" { | ||
default = "" | ||
description = "init (shellcode) to append to the end of this snippet" | ||
type = string | ||
} | ||
|
||
variable "log_level" { | ||
default = "info" | ||
description = "default log level verbosity for apps that support it" | ||
type = string | ||
} | ||
|
||
variable "log_prefix" { | ||
default = "OPS: " | ||
description = "string to prefix log messages with" | ||
type = string | ||
} | ||
|
||
variable "region" { | ||
description = "AWS region the volume is in" | ||
type = string | ||
} | ||
|
||
variable "wait_interval" { | ||
default = "5" | ||
description = "time (in seconds) to wait when looping to find the device" | ||
type = number | ||
} | ||
|
||
variable "volume_ids" { | ||
description = "IDs of the EBS volumes to attach" | ||
type = list(string) | ||
default = [] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
locals { | ||
volume_default = { | ||
type = "gp2" | ||
iops = 0 | ||
size = 15 | ||
encrypted = true | ||
kms_key_id = "" | ||
snapshot_id = "" | ||
} | ||
volumes_default = [for x in var.volumes : merge(local.volume_default, x)] | ||
Magicloud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.