terraform-aws-ovpn-server/ec2.tf

46 lines
1.3 KiB
HCL

data "aws_ssm_parameter" "ubuntu_ami" {
name = "/aws/service/canonical/ubuntu/server/20.04/stable/current/arm64/hvm/ebs-gp2/ami-id"
}
resource "aws_security_group" "openvpn_sg" {
name = "openvpn_sg"
ingress {
from_port = 1194
to_port = 1194
protocol = "udp"
cidr_blocks = ["${var.public_ip}/32"]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "openvpn_server_ec2" {
ami = data.aws_ssm_parameter.ubuntu_ami.value
instance_type = "t4g.${var.instance_size}"
tags = {
Name = "openvpn_ephemeral_server"
}
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
vpc_security_group_ids = [aws_security_group.openvpn_sg.id]
user_data = templatefile("${path.module}/user_data.tpl", {
s3_bucket = "${aws_s3_bucket.openvpn_config_bucket.bucket}"
ovpn_config_file = "${var.ovpn_config_file}"
})
}
resource "null_resource" "obtain_ovpn_file" {
depends_on = [aws_instance.openvpn_server_ec2]
provisioner "local-exec" {
command = "export AWS_DEFAULT_REGION=${var.region}; rm -f *.ovpn; until aws s3 cp s3://${aws_s3_bucket.openvpn_config_bucket.bucket}/${var.ovpn_config_file} . > /dev/null 2>&1; do sleep 5; done;"
}
}