Skip to content
This repository was archived by the owner on Feb 1, 2019. It is now read-only.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This will use the default settings which are:
|-a|ami_id|ID of a specific image (e.g.: ami-dca37ea5). If left unset, will default to ubuntu. Note that some amis are only available in specific regions, which will need to be specified with `-r`. Note that these images will incur an additional cost.|
|-p|playbook|playbooks/databox.yml. Path to ansible playbook used for custom deployment tasks.|
|-s|snapshot_id|The id of a snapshot to be loaded onto the EBS volume. If not provided, an empty volume will be provisioned. The snapshot must be in the same region as specified in `aws_region`, and it must be the same size or smaller than the size of the volume specified in `volume_size`. Note that a snapshot is not saved before the resources are destroyed with `./databox.sh down`: you will need to make a new snapshot at the AWS console to persist the data.|
|-c|create_snapshot|Can only be passed when calling `./databox.sh down`. Setting this to `1` creates a snapshot of the mounted volume prior to destroying it. The snapshot information will be output to the console in a json.|

*NOTE: Ansible will require you to enter your local sudo password before continuing.*

Expand Down Expand Up @@ -213,6 +214,14 @@ Note that if you create a databox by specifying region this way, you must also p

*NOTE: Failing to pass the correct region argument to the `./databox.sh down` command will result in your resources not being found, and consequently, not destroyed.*

You can create a snapshot of the default volume by passing `-c 1` like so:

```
./databox.sh -r eu-west-1 -c 1 down
```

Information about the snapshot will be passed to the console as a json.

### Using terraform and ansible directly

If you need additional customisations, the following commands can be run without the `./databox.sh` wrapper:
Expand Down
18 changes: 17 additions & 1 deletion databox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ case $key in
SNAPSHOT_ID="$2"
shift # past argument
;;
-c|--create_snapshot)
CREATE_SNAPSHOT="1"
shift # past argument
;;
# unknown option
-p|--playbook)
PLAYBOOK="$2"
shift # past argument
Expand Down Expand Up @@ -87,20 +92,27 @@ case "$1" in
ansible-playbook -i "$DATABOX_IP," -K "$PLAYBOOK" -u ubuntu
;;
"down") echo "Destroying DataBox"

# If I don't specify the region use a default value

if [ -z "${REGION+x}" ]; then
export REGION="eu-west-2"
fi

if [ -z "${CREATE_SNAPSHOT+x}" ]; then
export CREATE_SNAPSHOT=""
fi

# Get DataBox IP from state after the script completes
export DATABOX_IP=`terraform output ec2_ip`

# Run ansible teardown tasks
ansible-playbook -i "$DATABOX_IP," -K playbooks/teardown.yml -u ubuntu

terraform destroy --var aws_region=$REGION
terraform destroy --var aws_region=$REGION --var create_snapshot=$CREATE_SNAPSHOT
;;
*) echo "DataBox - create and destroy AWS instances for Data Science"
echo ""
echo "./databox.sh up - Create a DataBox"
echo " -- options -- "
echo " -r|--region - AWS region"
Expand All @@ -111,4 +123,8 @@ case "$1" in
echo " -p|--playbook - Ansible playbook for post deployment tasks"
echo " -s|--snapshot_id - Id of the snapshot from which to create volume"
echo "./databox.sh down - Destroy the DataBox"
echo " -- options -- "
echo " -r|--region - Must match region specified at creation"
echo " -c|--create_snapshot - Set this to 1 to create a snapshot of"
echo " the mounted volume prior to destruction."
esac
14 changes: 11 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
variable "aws_region" { default = "eu-west-2" } # London
variable "username" { default = "databoxuser"}
variable "instance_type" {default = "t2.micro" }
variable "volume_size" {default = "40" }
variable "instance_type" { default = "t2.micro" }
variable "volume_size" { default = "40" }
variable "profile" { default = "gds-data" }
variable "create_snapshot" { default = "" }
variable "snapshot_id" {
default = ""
description = "Specify a snapshot_id to be used when creating the EBS Volume. Note that this snapshot must be in the same region as the instance, and must be the same size or smaller than the volume as specified in volume_size."
Expand All @@ -19,7 +21,7 @@ variable "ami_id" {

provider "aws" {
region = "${var.aws_region}"
profile = "gds-data"
profile = "${var.profile}"
}

resource "aws_security_group" "allow_ssh_from_gds" {
Expand Down Expand Up @@ -101,6 +103,12 @@ resource "aws_ebs_volume" "volume" {
tags {
Name = "DataBoxVolume"
}

provisioner "local-exec" {
when = "destroy"
command = "if [ ${var.create_snapshot} ]; then aws ec2 create-snapshot --volume-id ${aws_ebs_volume.volume.id} --region ${var.aws_region} --profile ${var.profile} ; fi;"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreagrandi this checks whether anything has been assigned to var.create_snapshot, and if there is any string then it executes the command. This still needs you to set ./databox.sh -c 1 down, it would be nice if the 1 was not required and you could simply pass ./databox.sh -c down, but I could not get this to work. Any thoughts?

on_failure = "fail"
}
}

resource "aws_volume_attachment" "ebs_att" {
Expand Down