Swap Creation.
BitFlux needs a swap volume for optimal savings.
On first boot the BitFlux AMI will run scripts that create /swapfile by default. However, you can override this behavior and use a dedicated block device either by setting environment variables or calling the script in terraform.
The relevent environment variables are:
# SWAP_INSTALLER_DEVICE=/dev/xyz # Use a block device instead of a swapfile
# SWAP_INSTALLER_VOLUME_ID=vol-abcdef012 # id at /dev/disk/by-id/"
In this example we use a dedicated block device for swap and use an argument to the script to override the default behavior.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
# This is the "test" instance
resource "aws_instance" "bitfluxinstance" {
ami = var.bitflux_ami_id
instance_type = "t3a.nano"
key_name = "bitfluxcatcher_us-east-1"
availability_zone = "us-east-1b"
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
root_block_device {
volume_size = 16 # Size in GiB
delete_on_termination = true
}
# Create a new block device for the swap
ebs_block_device {
device_name = "/dev/xvdb" # name isn't guaranteed to be used in OS
volume_size = 8
delete_on_termination = true
}
user_data = <<EOF
#!/bin/bash
echo "start" > /opt/swap_install.log
# Find the new ebs_block_device
for blkdev in $(lsblk -d -n -o NAME,TYPE | awk '$2 == "disk" {print $1}'); do
if [ -z "$(blkid | grep $blkdev )" ]; then
SWAP_INSTALLER_DEVICE="/dev/$blkdev"
echo "Found $blkdev, using" >> /opt/swap_install.log
break
else
echo "Skipping $blkdev, used" >> /opt/swap_install.log
fi
done
# Run the swap creation script
/var/lib/cloud/scripts/per-instance/01-swap_creation.sh --override --device "$SWAP_INSTALLER_DEVICE" >> /opt/swap_install.log
EOF
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH inbound traffic"
ingress {
description = "SSH from anywhere"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP from anywhere"
from_port = 80
to_port = 80
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"]
}
}
variable "bitflux_ami_id" {
description = "The ID of the BitFlux AMI to use for the EC2 instance"
type = string
}