terraform.tf 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ## VPC
  2. resource "aws_vpc" "infra_vpc" {
  3. cidr_block = var.cidr_block
  4. enable_dns_support = var.enable_dns_support
  5. enable_dns_hostnames = var.enable_dns_hostnames
  6. tags = var.tags
  7. }
  8. ## Public Subnet for ALB
  9. resource "aws_subnet" "infra_public_subnet" {
  10. count = length(var.public_subnet_az)
  11. vpc_id = aws_vpc.infra_vpc.id
  12. availability_zone = var.public_subnet_az[count.index]
  13. cidr_block = var.public_subnet_cidr[count.index]
  14. tags = var.tags
  15. }
  16. ## Private Subnet for EC2
  17. resource "aws_subnet" "infra_private_subnet" {
  18. vpc_id = aws_vpc.infra_vpc.id
  19. availability_zone = var.private_subnet_az
  20. cidr_block = var.private_subnet_cidr
  21. tags = var.tags
  22. }
  23. ## Internet Gateway
  24. resource "aws_internet_gateway" "infra_internet_gateway" {
  25. vpc_id = aws_vpc.infra_vpc.id
  26. tags = var.tags
  27. }
  28. ## Route Table for Public Subnet and attach Internet Gateway to it
  29. resource "aws_route_table" "infra_public_rt" {
  30. vpc_id = aws_vpc.infra_vpc.id
  31. route {
  32. cidr_block = "0.0.0.0/0"
  33. gateway_id = aws_internet_gateway.infra_internet_gateway.id
  34. }
  35. tags = var.tags
  36. }
  37. ## Route Table Public Subnet Association
  38. resource "aws_route_table_association" "infra_public_subnet_rt_association" {
  39. count = length(var.public_subnet_az)
  40. subnet_id = aws_subnet.infra_public_subnet[count.index].id
  41. route_table_id = aws_route_table.infra_public_rt.id
  42. }
  43. ## Elastic IP for NAT Gateway
  44. resource "aws_eip" "infra_nat_gateway_eip" {
  45. domain = "vpc"
  46. tags = var.tags
  47. }
  48. ## NAT Gateway
  49. resource "aws_nat_gateway" "infra_nat_gateway" {
  50. allocation_id = aws_eip.infra_nat_gateway_eip.id
  51. subnet_id = aws_subnet.infra_public_subnet[0].id
  52. tags = var.tags
  53. depends_on = [
  54. aws_internet_gateway.infra_internet_gateway
  55. ]
  56. }
  57. ## Route Table for Private Subnet and attach NAT Gateway to it
  58. resource "aws_route_table" "infra_private_rt" {
  59. vpc_id = aws_vpc.infra_vpc.id
  60. route {
  61. cidr_block = "0.0.0.0/0"
  62. nat_gateway_id = aws_nat_gateway.infra_nat_gateway.id
  63. }
  64. tags = var.tags
  65. }
  66. ## Route Table Private Subnet Association
  67. resource "aws_route_table_association" "infra_private_subnet_rt_association" {
  68. subnet_id = aws_subnet.infra_private_subnet.id
  69. route_table_id = aws_route_table.infra_private_rt.id
  70. }