Skip to content

Commit

Permalink
Merge pull request #6 from orenfromberg/random-fixes
Browse files Browse the repository at this point in the history
Random fixes
  • Loading branch information
orenfromberg committed Sep 24, 2019
2 parents 817cbb9 + 3796d1f commit 246c7dc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To use it, get your ip address and add the following terraform:

```terraform
module "my-instance" {
source = "git@github.com:orenfromberg/infrastructure.git//dev-machine?ref=tags/v0.0.1"
source = "git@github.com:orenfromberg/infrastructure.git//dev-machine?ref=tags/v0.0.5"
my-ip = "71.104.71.56"
name = "my-dev-machine"
}
Expand All @@ -21,9 +21,9 @@ $ terraform init
$ terraform apply
```

Once the command is complete, you'll have an identity file `identity.pem` and `ip_address.txt` created in your local directory.
Once the command is complete, you'll have an identity file `my-dev-machine-identity.pem` and a script called `connect-to-my-dev-machine.sh` created in your local directory.

Now ssh to the instance using the following command:
```sh
$ ssh -i identity.pem ubuntu@$(cat ip_address.txt)
$ ./connect-to-my-dev-machine.sh
```
3 changes: 3 additions & 0 deletions dev-machine/connect.sh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

ssh -i ${identity} ubuntu@${public_ip}
37 changes: 7 additions & 30 deletions dev-machine/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ resource "tls_private_key" "dev_machine" {
rsa_bits = 4096

provisioner "local-exec" {
command = "echo \"${tls_private_key.dev_machine.private_key_pem}\" > identity.pem; chmod 400 identity.pem"
command = "echo \"${tls_private_key.dev_machine.private_key_pem}\" > ${var.name}-identity.pem; chmod 400 ${var.name}-identity.pem"
}
}

data "aws_ami" "latest-ubuntu" {
data "aws_ami" "latest_ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical

Expand All @@ -22,32 +22,11 @@ data "aws_ami" "latest-ubuntu" {
}
}

data "aws_ami" "amazon-linux" {
most_recent = true

owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-hvm-*-gp2"]
}

filter {
name = "virtualization-type"
values = ["hvm"] # hvm > pv
}

filter {
name = "owner-alias"
values = ["amazon"]
}
}

resource "aws_key_pair" "dev_machine" {
public_key = tls_private_key.dev_machine.public_key_openssh
}

resource "aws_security_group" "security_grp" {
resource "aws_security_group" "dev_machine" {
name = "allow_ssh_from_me"

ingress {
Expand Down Expand Up @@ -75,13 +54,12 @@ resource "aws_security_group" "security_grp" {
}
}

resource "aws_instance" "dev-machine" {
#ami = "${data.aws_ami.amazon-linux.id}"
ami = data.aws_ami.latest-ubuntu.id
resource "aws_instance" "dev_machine" {
ami = data.aws_ami.latest_ubuntu.id
instance_type = "t2.micro"
key_name = aws_key_pair.dev_machine.key_name
associate_public_ip_address = true
security_groups = [aws_security_group.security_grp.name]
security_groups = [aws_security_group.dev_machine.name]
user_data = <<EOF
#!/bin/bash
sudo apt -y update && sudo apt -y upgrade
Expand All @@ -95,9 +73,8 @@ sudo usermod -aG docker ubuntu
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | sudo bash
EOF


provisioner "local-exec" {
command = "echo \"${aws_instance.dev-machine.public_ip}\" > ip_address.txt"
command = "echo \"${templatefile("${path.module}/connect.sh.tmpl", { identity = "${var.name}-identity.pem", public_ip = "${aws_instance.dev_machine.public_ip}"})}\" > connect-to-${var.name}.sh; chmod +x connect-to-${var.name}.sh"
}

tags = {
Expand Down

0 comments on commit 246c7dc

Please sign in to comment.