Browse Source

Create self contained ec2

main
gabriel becker 1 year ago
parent
commit
e96b52b85f
  1. 6
      ec2ml/config.tf
  2. 40
      ec2ml/ec2.tf
  3. 43
      ec2ml/network.tf
  4. 9
      ec2ml/outputs.tf
  5. 7
      ec2ml/variables.tf
  6. 1
      ec2ml/variables/prod.tfvars
  7. 1
      ec2ml/variables/sample.tfvars
  8. 1
      ec2ml/variables/test.tfvars

6
ec2ml/config.tf

@ -0,0 +1,6 @@
provider "aws" {
profile = "superuser"
region = "ap-southeast-2"
}

40
ec2ml/ec2.tf

@ -0,0 +1,40 @@
resource "tls_private_key" "mlkey" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "mlkey" {
key_name = "mlboxkey"
public_key = tls_private_key.mlkey.public_key_openssh
provisioner "local-exec" {
command = "echo '${tls_private_key.mlkey.private_key_pem}' > ./${var.output_ssh_key} & chmod 400 ${var.output_ssh_key}"
}
}
data "aws_ami" "aws_linux_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["debian-12-amd64-*"]
}
}
resource "aws_instance" "ml-box" {
ami = data.aws_ami.aws_linux_ami.id
instance_type = var.instance_type
key_name = aws_key_pair.mlkey.key_name
vpc_security_group_ids = [ aws_security_group.allow_tls.id ]
tags = {
Name = "terragenerated-simple-ec2"
}
}
# resource "ansible_host" "ml-box" {
# name = aws_instance.ml-box.public-ip
# groups = [ ]
# }

43
ec2ml/network.tf

@ -0,0 +1,43 @@
resource "aws_security_group" "allow_tls" {
name = "allow_tls"
description = "Allow TLS inbound traffic plys ssh"
ingress {
description = "TLS from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "TLS from VPC"
from_port = 8000
to_port = 8999
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_tls"
}
}

9
ec2ml/outputs.tf

@ -0,0 +1,9 @@
output "ml-instance-url" {
value = aws_instance.ml-box.public_dns
}
output "key" {
value = tls_private_key.mlkey.public_key_openssh
sensitive = true
}

7
ec2ml/variables.tf

@ -0,0 +1,7 @@
variable "instance_type" {
default = "t2.micro"
}
variable "output_ssh_key" {
default = "my_ssh.pem"
}

1
ec2ml/variables/prod.tfvars

@ -0,0 +1 @@
instance_type = "g3.4xlarge"

1
ec2ml/variables/sample.tfvars

@ -0,0 +1 @@
instance_type =

1
ec2ml/variables/test.tfvars

@ -0,0 +1 @@
instance_type = "t2.micro"
Loading…
Cancel
Save