Created
June 9, 2020 06:57
-
-
Save deanrock/7b2500cbaa92385faee98e39a509deaf to your computer and use it in GitHub Desktop.
Spawn public EC2 instance
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
locals { | |
prefix = "random-ec2-instance" | |
} | |
provider "aws" { | |
region = "eu-central-1" | |
} | |
data "aws_vpc" "default" { | |
default = true | |
} | |
data "aws_ami" "amazon-linux-2" { | |
most_recent = true | |
owners = ["amazon"] | |
filter { | |
name = "name" | |
values = ["amzn2-ami-hvm-*-x86_64-ebs"] | |
} | |
} | |
resource "aws_security_group" "sg" { | |
name = local.prefix | |
vpc_id = data.aws_vpc.default.id | |
} | |
resource "aws_security_group_rule" "ingress_ssh" { | |
type = "ingress" | |
from_port = 22 | |
to_port = 22 | |
protocol = "tcp" | |
cidr_blocks = [ | |
"0.0.0.0/0" | |
] | |
security_group_id = aws_security_group.sg.id | |
} | |
resource "aws_security_group_rule" "egress" { | |
type = "egress" | |
from_port = 0 | |
to_port = 0 | |
protocol = "-1" | |
cidr_blocks = ["0.0.0.0/0"] | |
security_group_id = aws_security_group.sg.id | |
} | |
resource "aws_key_pair" "ssh" { | |
key_name = local.prefix | |
public_key = file("~/.ssh/monostack_ssh.pub") | |
} | |
resource "aws_instance" "instance" { | |
ami = data.aws_ami.amazon-linux-2.image_id | |
instance_type = "t3.small" | |
key_name = aws_key_pair.ssh.key_name | |
vpc_security_group_ids = [aws_security_group.sg.id] | |
root_block_device { | |
volume_size = "100" | |
encrypted = true | |
} | |
tags = merge( | |
{ | |
"Name" = local.prefix | |
}, | |
) | |
} | |
output "ip" { | |
value = aws_instance.instance.public_ip | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment