Skip to content

Commit

Permalink
Add ec2-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
yujunz committed Nov 24, 2019
1 parent 38c7fb2 commit 0277260
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions modules/ec2-instance/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
variable "ssh_public_key_path" {
default = "~/.ssh/id_rsa.pub"
}

variable "ami" {}

variable "instance_type" {
default = "t2.micro"
}

locals {
label_id = "terraform-${formatdate("YYYY-MM-DD", timestamp())}"
}

resource "aws_instance" "this" {
ami = var.ami
instance_type = var.instance_type

key_name = aws_key_pair.this.key_name
security_groups = [
aws_security_group.allow_ssh.name
]

tag {
Name = "terraform-quickstart"
}
}

resource "aws_key_pair" "this" {
key_name = local.label_id
public_key = file(var.ssh_public_key_path)
}

resource "aws_security_group" "allow_ssh" {
name = local.label_id
description = "Allow SSH inbound traffic"

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

data "http" "myip" {
url = "http://ipv4.icanhazip.com"
}

output "public_ip" {
value = aws_instance.this.public_ip
}

0 comments on commit 0277260

Please sign in to comment.