Based in part on https://lg.io/2015/07/05/revised-and-much-faster-run-your-own-highend-cloud-gaming-service-on-ec2.html
Last active
February 18, 2019 00:59
-
-
Save clebio/ea09f8c02cfc08cc6df189745121ec3e to your computer and use it in GitHub Desktop.
Spot Gaming rig on AWS
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
data "aws_ami" "windows" { | |
most_recent = true | |
name_regex = "Windows_Server-2019-English-Full-Base*" | |
owners = ["amazon"] | |
} | |
variable "windows_instance_type" { | |
default = "g3s.xlarge" | |
} | |
data "external" "price" { | |
program = ["bash", "describe_spot_prices.sh", "${var.windows_instance_type}"] | |
} | |
resource "aws_spot_instance_request" "cheap" { | |
ami = "${data.aws_ami.windows.id}" | |
spot_price = "${data.external.price.result.price}" | |
instance_type = "${var.windows_instance_type}" | |
spot_type = "one-time" | |
wait_for_fulfillment = true | |
get_password_data = true | |
key_name = "${var.key_name}" | |
vpc_security_group_ids = ["${module.vpc.default_security_group_id}"] | |
subnet_id = "${module.vpc.public_subnets[0]}" | |
iam_instance_profile = "${aws_iam_instance_profile.instance.name}" | |
user_data = <<EOF | |
<powershell> | |
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) | |
choco install -y firefox steam | |
$Bucket = "ec2-windows-nvidia-drivers" | |
$KeyPrefix = "latest" | |
$LocalPath = "C:\Users\Administrator\Desktop\NVIDIA" | |
$Objects = Get-S3Object -BucketName $Bucket -KeyPrefix $KeyPrefix -Region us-east-1 | |
foreach ($Object in $Objects) { | |
$LocalFileName = $Object.Key | |
if ($LocalFileName -ne '' -and $Object.Size -ne 0) { | |
$LocalFilePath = Join-Path $LocalPath $LocalFileName | |
Copy-S3Object -BucketName $Bucket -Key $Object.Key -LocalFile $LocalFilePath -Region us-east-1 | |
} | |
} | |
cd C:\Users\Administrator\Desktop\NVIDIA\latest | |
$10x = ls *win10* | |
&$10x /s | |
</powershell> | |
EOF | |
root_block_device = { | |
delete_on_termination = true | |
} | |
tags = { | |
Name = "l33tG@m3rz" | |
CostCenter = "caleb@foghornconsulting.com" | |
} | |
} | |
resource "aws_iam_instance_profile" "instance" { | |
name = "${aws_iam_role.instance.name}" | |
role = "${aws_iam_role.instance.name}" | |
} | |
resource "aws_iam_role_policy_attachment" "s3" { | |
role = "${aws_iam_role.instance.name}" | |
policy_arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess" | |
} | |
output "spot_instance" { | |
value = "${aws_spot_instance_request.cheap.private_ip}" | |
} | |
output "password" { | |
description = "Encrypted Windows password (terraform output password | base64 -D | openssl rsautl -decrypt -inkey)" | |
value = "${aws_spot_instance_request.cheap.password_data}" | |
} |
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
#!/bin/bash | |
TYPE=$1 | |
function query_history { | |
aws ec2 describe-spot-price-history \ | |
--instance-types $TYPE \ | |
--max-items 1 \ | |
--query 'SpotPriceHistory[].SpotPrice' | jq -r '.[]' | |
} | |
PRICE=`query_history` | |
if ! [ "$PRICE" == "" ] | |
then | |
jq -n --arg this $PRICE '{price:$this}' | |
else | |
jq -n --arg this "0.00" '{price:$this}' | |
fi |
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
variable "region" { | |
default = "us-west-2" | |
} | |
provider "aws" { | |
region = "${var.region}" | |
} | |
variable "key_name" { | |
default = "yours" | |
} | |
data "external" "ip" { | |
program = ["bash", "-c", "jq -n --arg this \"`curl checkip.amazonaws.com`\" '{\"ip\": $this}'"] | |
} | |
module "vpc" { | |
source = "terraform-aws-modules/vpc/aws" | |
version = "1.55.0" | |
default_vpc_name = "clebio" | |
azs = ["${var.region}a", "${var.region}b"] | |
cidr = "10.133.7.0/24" | |
public_subnets = ["10.133.7.0/28"] | |
enable_nat_gateway = true | |
tags = { | |
Name = "clebio " | |
} | |
} | |
resource "aws_security_group_rule" "ingress" { | |
protocol = "all" | |
type = "ingress" | |
from_port = 0 | |
to_port = 0 | |
security_group_id = "${module.vpc.default_security_group_id}" | |
cidr_blocks = ["${data.external.ip.result.ip}/32"] | |
} | |
resource "aws_iam_role" "instance" { | |
name_prefix = "gaming" | |
assume_role_policy = <<EOF | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Action": "sts:AssumeRole", | |
"Principal": { | |
"Service": "ec2.amazonaws.com" | |
}, | |
"Effect": "Allow", | |
"Sid": "" | |
} | |
] | |
} | |
EOF | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment