bastion.tf 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ##
  2. # Create a bastion host to allow SSH in to the test network.
  3. # Connections are only allowed from ${var.allowed_network}
  4. # This box also acts as a NAT for the private network
  5. ##
  6. resource "aws_security_group" "bastion" {
  7. name = "bastion"
  8. description = "Allow access from allowed_network to SSH/Consul, and NAT internal traffic"
  9. vpc_id = "${aws_vpc.test.id}"
  10. # SSH
  11. ingress = {
  12. from_port = 22
  13. to_port = 22
  14. protocol = "tcp"
  15. cidr_blocks = [ "${var.allowed_network}" ]
  16. self = false
  17. }
  18. # Consul
  19. ingress = {
  20. from_port = 8500
  21. to_port = 8500
  22. protocol = "tcp"
  23. cidr_blocks = [ "${var.allowed_network}" ]
  24. self = false
  25. }
  26. # NAT
  27. ingress {
  28. from_port = 0
  29. to_port = 65535
  30. protocol = "tcp"
  31. cidr_blocks = [
  32. "${aws_subnet.public.cidr_block}",
  33. "${aws_subnet.private.cidr_block}"
  34. ]
  35. self = false
  36. }
  37. }
  38. resource "aws_security_group" "allow_bastion" {
  39. name = "allow_bastion_ssh"
  40. description = "Allow access from bastion host"
  41. vpc_id = "${aws_vpc.test.id}"
  42. ingress {
  43. from_port = 0
  44. to_port = 65535
  45. protocol = "tcp"
  46. security_groups = ["${aws_security_group.bastion.id}"]
  47. self = false
  48. }
  49. }
  50. resource "aws_instance" "bastion" {
  51. connection {
  52. user = "ec2-user"
  53. key_file = "${var.key_path}"
  54. }
  55. ami = "${lookup(var.amazon_nat_amis, var.region)}"
  56. instance_type = "t2.micro"
  57. key_name = "${var.key_name}"
  58. security_groups = [
  59. "${aws_security_group.bastion.id}"
  60. ]
  61. subnet_id = "${aws_subnet.dmz.id}"
  62. associate_public_ip_address = true
  63. source_dest_check = false
  64. user_data = "${file("files/bastion/cloud-init.txt")}"
  65. tags = {
  66. Name = "bastion"
  67. subnet = "dmz"
  68. role = "bastion"
  69. environment = "test"
  70. }
  71. }
  72. output "bastion" {
  73. value = "${aws_instance.bastion.public_ip}"
  74. }