Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to Fargate #227

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.terraform*
terraform.tfstate
8 changes: 8 additions & 0 deletions fargate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM dtandersen/factorio:latest

RUN apk add --update python py-pip
Copy link
Member

Choose a reason for hiding this comment

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

You should not update any packages if it isn't necessary and you forgot two flags.
I would use apk --no-cache --no-progress add py-pip

Copy link
Author

Choose a reason for hiding this comment

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

I'm not super familiar with APK (or APT, for that matter) but although there IS an APK awscli package, it's still unreleased (edge?) and I couldn't figure out how to install that instead. Otherwise, the python install of the AWS CLI is the simplest set of steps, even if it does balloon the image size :/

Copy link
Member

Choose a reason for hiding this comment

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

That is apk from Alpine and not apt from Debian :).
I was more nitpicking about the --update. I think it is fine to use the ``pip`´ variant but if you are interested you can find it here https://pkgs.alpinelinux.org/package/edge/testing/x86_64/aws-cli

RUN pip install awscli
Copy link
Member

Choose a reason for hiding this comment

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

Combine those two run commands with && \


COPY fargate-entrypoint.sh fargate-entrypoint.sh

ENTRYPOINT ["/fargate-entrypoint.sh"]
25 changes: 25 additions & 0 deletions fargate/fargate-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SIGTERM-handler
term_handler() {
if [ $pid -ne 0 ]; then
aws s3 cp /factorio $STORAGE --recursive
kill -SIGTERM "$pid"
wait "$pid"
fi
exit 143; # 128 + 15 -- SIGTERM
}

trap 'kill ${!}; term_handler' SIGTERM

# Load last saved game state
aws s3 cp $STORAGE /factorio --recursive

# run application
./docker-entrypoint.sh &
pid="$!"

# save game state every five minutes
while true
do
aws s3 cp /factorio $STORAGE --recursive
sleep $AUTOSAVE_RATE
done
97 changes: 97 additions & 0 deletions fargate/infra/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
provider "aws" {
region = "us-west-2"
}

resource "aws_s3_bucket" "factorio" {
bucket_prefix = "factorio-"
versioning {
enabled = true
}
}

resource "aws_ecs_cluster" "factorio" {
name = "factorio-ecs-cluster"
}

data "template_file" "factorio" {
template = "${file("${path.module}/tasks/factorio.json")}"

vars {
image = "rangerscience/factorio:fargate"
storage = "s3://${aws_s3_bucket.factorio.id}"
autosave_rate = 300 # Seconds between saves to S3
}
}

resource "aws_ecs_task_definition" "factorio" {
family = "factorio"
container_definitions = "${data.template_file.factorio.rendered}"

requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"

requires_compatibilities = ["FARGATE"]
cpu = "4096" // 4 vCPU
memory = "8192" // 8 GB (min for 4 vCPU)

task_role_arn = "${aws_iam_role.factorio.arn}"
}

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "factorio" {
cidr_block = "10.0.1.0/26"
vpc_id = "${aws_vpc.main.id}"
}

resource "aws_ecs_service" "factorio" {
name = "factorio"
launch_type = "FARGATE"
cluster = "${aws_ecs_cluster.factorio.id}"
task_definition = "${aws_ecs_task_definition.factorio.arn}"
desired_count = 1

network_configuration {
subnets = ["${aws_subnet.factorio.id}"]
assign_public_ip = true
}
}

resource "aws_iam_role" "factorio" {
name = "factorio"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

data "aws_iam_policy_document" "factorio" {
statement {
sid = "1"
actions = ["*"]
resources = ["${aws_s3_bucket.factorio.arn}"]
}
}

resource "aws_iam_policy" "factorio" {
policy = "${data.aws_iam_policy_document.factorio.json}"
}

resource "aws_iam_role_policy_attachment" "factorio" {
role = "${aws_iam_role.factorio.name}"
policy_arn = "${aws_iam_policy.factorio.arn}"
}
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the empty file from git. :)

Empty file added fargate/infra/output.tf
Empty file.
23 changes: 23 additions & 0 deletions fargate/infra/tasks/factorio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"name": "factorio",
"environment": [
{ "name": "STORAGE", "value": "${storage}"},
{ "name": "AUTOSAVE_RATE", "value": "${autosave_rate}"}
],
"essential": true,
"image": "${image}",
"portMappings": [
{
"containerPort": 34197,
"hostPort": 34197,
"protocol": "udp"
},
{
"containerPort": 27015,
"hostPort": 27015,
"protocol": "tcp"
}
]
}
]