Created
October 11, 2023 19:36
-
-
Save TheNotary/282b3e150f93daf3600a283b96e38624 to your computer and use it in GitHub Desktop.
example of maintaining a persistent storage volume and with an ephemeral VM
This file contains 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
provider "aws" { | |
region = "us-west-1" | |
# profile = "${var.aws_profile}" | |
} | |
# aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId,State.Name,PrivateIpAddress,PublicIpAddress,InstanceType,Tags]" --output table | |
resource "aws_instance" "generic" { | |
ami = "ami-0beb0c1a33f8fddf2" # us-west-1 | |
instance_type = "t2.micro" | |
key_name = "${aws_key_pair.generic.id}" | |
subnet_id = aws_subnet.default.id | |
security_groups = [ | |
aws_security_group.wide_open.id | |
] | |
# quick hack to format the ebs volume if needed and mount upon boot | |
user_data = <<-EOF | |
#!/bin/bash | |
DEVICE=/dev/xvdf | |
MOUNTPOINT=/mnt/perm | |
RETRIES=30 | |
while [ ! -b $DEVICE ]; do | |
echo "Waiting for $DEVICE..." | |
sleep 10 | |
RETRIES=$((RETRIES-1)) | |
if [ $RETRIES -eq 0 ]; then | |
echo "Timeout waiting for $DEVICE. Exiting." | |
exit 1 | |
fi | |
done | |
# Check if filesystem exists, if not create one | |
# file -s /dev/xvdf | grep -q "filesystem" | |
file -s $DEVICE | grep -q "filesystem" | |
if [ $? -ne 0 ]; then | |
mkfs -t ext4 $DEVICE | |
fi | |
mkdir -p $MOUNTPOINT | |
mount $DEVICE $MOUNTPOINT | |
echo "$DEVICE $MOUNTPOINT ext4 defaults,x-systemd.automount 0 2" >> /etc/fstab | |
EOF | |
} | |
resource "aws_ebs_volume" "generic" { | |
size = 10 | |
type = "gp2" | |
availability_zone = "us-west-1b" | |
} | |
resource "aws_volume_attachment" "generic" { | |
device_name = "/dev/xvdf" | |
volume_id = "${aws_ebs_volume.generic.id}" | |
instance_id = "${aws_instance.generic.id}" | |
} | |
//////////////////////////////////////// | |
// Setup Network and Connection Stuff // | |
// Not Relevant // | |
//////////////////////////////////////// | |
resource "aws_key_pair" "generic" { | |
key_name = "personal-aws_rsa-generic" | |
public_key = "${file("keys/personal-aws_rsa.pub")}" | |
} | |
resource "aws_vpc" "default" { | |
cidr_block = "10.0.0.0/16" | |
} | |
resource "aws_subnet" "default" { | |
vpc_id = aws_vpc.default.id | |
cidr_block = "10.0.1.0/24" | |
map_public_ip_on_launch = true | |
availability_zone = "us-west-1b" | |
} | |
resource "aws_internet_gateway" "default" { | |
vpc_id = aws_vpc.default.id | |
} | |
resource "aws_route_table" "default" { | |
vpc_id = aws_vpc.default.id | |
route { | |
cidr_block = "0.0.0.0/0" | |
gateway_id = aws_internet_gateway.default.id | |
} | |
} | |
resource "aws_route_table_association" "default" { | |
subnet_id = aws_subnet.default.id | |
route_table_id = aws_route_table.default.id | |
} | |
resource "aws_security_group" "wide_open" { | |
name = "wide_open" | |
description = "Allows SSH connections in from any IP address" | |
vpc_id = aws_vpc.default.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"] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment