Created
March 12, 2018 05:18
-
-
Save picatz/91c24b6ead608f2fcdaca709cc2d90be to your computer and use it in GitHub Desktop.
This file contains hidden or 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" {} | |
data "aws_ami" "ubuntu" { | |
most_recent = true | |
filter { | |
name = "name" | |
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"] | |
} | |
filter { | |
name = "virtualization-type" | |
values = ["hvm"] | |
} | |
owners = ["099720109477"] # Canonical | |
} | |
resource "aws_security_group" "kumo_dojo" { | |
name = "kumo_dojo" | |
description = "Allow any SSH traffic to this box, like a gangster do." | |
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"] | |
} | |
tags { | |
Name = "Adds SSH access" | |
} | |
} | |
resource "aws_key_pair" "kumo_dojo" { | |
key_name = "kumo_dojo_admin" | |
public_key = "${file("kumo_dojo_admin.pub")}" | |
} | |
resource "aws_instance" "kumo_dojo" { | |
ami = "${data.aws_ami.ubuntu.id}" | |
instance_type = "t2.micro" | |
availability_zone = "us-east-1a" | |
vpc_security_group_ids = ["${aws_security_group.kumo_dojo.id}"] | |
key_name = "kumo_dojo_admin" | |
provisioner "remote-exec" { | |
connection { | |
type = "ssh" | |
user = "ubuntu" | |
private_key = "${file("kumo_dojo_admin")}" | |
} | |
scripts = [ | |
"install_docker.sh" | |
] | |
} | |
provisioner "file" { | |
connection { | |
type = "ssh" | |
user = "ubuntu" | |
private_key = "${file("kumo_dojo_admin")}" | |
} | |
source = "bot" | |
destination = "~/bot" | |
} | |
provisioner "remote-exec" { | |
connection { | |
type = "ssh" | |
user = "ubuntu" | |
private_key = "${file("kumo_dojo_admin")}" | |
} | |
inline = [ | |
"sudo sh -c \"echo 'AuthorizedKeysFile %h/.ssh/authorized_keys %h/.ssh/game_keys' >> /etc/ssh/sshd_config\"", | |
"touch ~/.ssh/game_keys", | |
"sudo chmod 600 ~/.ssh/game_keys", | |
"cd ~/bot", | |
"sudo docker build -t kumo_dojo_bot .", | |
"sudo docker run -itd --restart unless-stopped --name kumo-bot -v ~/.ssh/game_keys:/game_keys --health-cmd=\"ps aux | grep -q kumo_dojo_bot.rb\" --health-interval=10s kumo_dojo_bot", | |
"sudo service ssh restart" | |
] | |
} | |
} | |
output "instance_ips" { | |
value = ["${aws_instance.kumo_dojo.*.public_ip}"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment