github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/scripts/testing-setup/main.tf (about)

     1  provider "aws" {
     2    region                   = var.AWS_REGION
     3    shared_credentials_files = [var.AWS_CREDENTIALS_FILE]
     4  
     5    default_tags {
     6      tags = {
     7        Project = "bacalhau-test-cluster"
     8      }
     9    }
    10  }
    11  resource "random_id" "run" {
    12    byte_length = 4
    13  }
    14  
    15  resource "aws_security_group" "allow_ssh_and_bacalhau" {
    16    vpc_id      = aws_vpc.bacalhau_vpc.id
    17    name        = "allow_ssh_and_bacalhau"
    18    description = "security group that allows ssh and bacalhau and all egress traffic"
    19  
    20  }
    21  resource "aws_security_group_rule" "egress_all" {
    22    type              = "egress"
    23    from_port         = 0
    24    to_port           = 0
    25    protocol          = "-1"
    26    cidr_blocks       = ["0.0.0.0/0"]
    27    security_group_id = aws_security_group.allow_ssh_and_bacalhau.id
    28  }
    29  
    30  resource "aws_security_group_rule" "ingress_ssh" {
    31    type              = "ingress"
    32    from_port         = 22
    33    to_port           = 22
    34    protocol          = "tcp"
    35    cidr_blocks       = ["0.0.0.0/0"]
    36    security_group_id = aws_security_group.allow_ssh_and_bacalhau.id
    37  }
    38  
    39  resource "aws_security_group_rule" "ingress_http" {
    40    type              = "ingress"
    41    from_port         = 80
    42    to_port           = 80
    43    protocol          = "tcp"
    44    cidr_blocks       = ["0.0.0.0/0"]
    45    security_group_id = aws_security_group.allow_ssh_and_bacalhau.id
    46  }
    47  
    48  resource "aws_security_group_rule" "ingress_bacalhau" {
    49    type              = "ingress"
    50    from_port         = 54545
    51    to_port           = 54545
    52    protocol          = "tcp"
    53    cidr_blocks       = ["0.0.0.0/0"]
    54    security_group_id = aws_security_group.allow_ssh_and_bacalhau.id
    55  }
    56  
    57  # https://geekdudes.wordpress.com/2018/01/09/install-packages-to-amazon-virtual-machine-using-terraform/
    58  
    59  # Internet VPC
    60  resource "aws_vpc" "bacalhau_vpc" {
    61    cidr_block           = "10.0.0.0/16"
    62    instance_tenancy     = "default"
    63    enable_dns_support   = "true"
    64    enable_dns_hostnames = "true"
    65    enable_classiclink   = "false"
    66    tags = {
    67      Name = "bacalhau_vpc"
    68    }
    69  }
    70  
    71  
    72  # Subnets
    73  resource "aws_subnet" "bacalhau_public_1_a" {
    74    vpc_id                  = aws_vpc.bacalhau_vpc.id
    75    cidr_block              = "10.0.1.0/24"
    76    map_public_ip_on_launch = "true"
    77    availability_zone       = "eu-west-1a"
    78    tags = {
    79      Name = "bacalhau_public_1_a"
    80    }
    81  }
    82  resource "aws_subnet" "bacalhau_private_1_a" {
    83    vpc_id                  = aws_vpc.bacalhau_vpc.id
    84    cidr_block              = "10.0.2.0/24"
    85    map_public_ip_on_launch = "false"
    86    availability_zone       = "eu-west-1a"
    87  
    88    tags = {
    89      Name = "bacalhau_private_1_a"
    90    }
    91  }
    92  resource "aws_subnet" "bacalhau_public_1_b" {
    93    vpc_id                  = aws_vpc.bacalhau_vpc.id
    94    cidr_block              = "10.0.3.0/24"
    95    map_public_ip_on_launch = "true"
    96    availability_zone       = "eu-west-1b"
    97    tags = {
    98      Name = "bacalhau_public_1_b"
    99    }
   100  }
   101  resource "aws_subnet" "bacalhau_private_1_b" {
   102    vpc_id                  = aws_vpc.bacalhau_vpc.id
   103    cidr_block              = "10.0.4.0/24"
   104    map_public_ip_on_launch = "false"
   105    availability_zone       = "eu-west-1b"
   106  
   107    tags = {
   108      Name = "bacalhau_private_1_b"
   109    }
   110  }
   111  
   112  
   113  # Internet GW
   114  resource "aws_internet_gateway" "bacalhau_gw" {
   115    vpc_id = aws_vpc.bacalhau_vpc.id
   116  
   117    tags = {
   118      Name = "bacalhau_vpc_gateway"
   119    }
   120  }
   121  
   122  # route tables
   123  resource "aws_route_table" "bacalhau_public_route_table" {
   124    vpc_id = aws_vpc.bacalhau_vpc.id
   125    route {
   126      cidr_block = "0.0.0.0/0"
   127      gateway_id = aws_internet_gateway.bacalhau_gw.id
   128    }
   129  
   130    tags = {
   131      Name = "bacalhau_public_route_table"
   132    }
   133  }
   134  
   135  # route associations public
   136  resource "aws_route_table_association" "bacalhau_public_1_a" {
   137    subnet_id      = aws_subnet.bacalhau_public_1_a.id
   138    route_table_id = aws_route_table.bacalhau_public_route_table.id
   139  }
   140  resource "aws_route_table_association" "bacalhau_public_1_b" {
   141    subnet_id      = aws_subnet.bacalhau_public_1_b.id
   142    route_table_id = aws_route_table.bacalhau_public_route_table.id
   143  }
   144  
   145  resource "aws_lb" "nlb" {
   146    name               = "bacalhau-nlb-${random_id.run.hex}"
   147    subnets            = [aws_subnet.bacalhau_private_1_a.id, aws_subnet.bacalhau_private_1_b.id, ]
   148    load_balancer_type = "network"
   149    internal           = false
   150    idle_timeout       = 60
   151  
   152    timeouts {
   153      create = "30m"
   154      delete = "30m"
   155    }
   156    tags = {
   157      Name = "bacalhau-nlb"
   158    }
   159  }
   160  
   161  resource "aws_lb_listener" "http_listener" {
   162    load_balancer_arn = aws_lb.nlb.arn
   163    port              = 80
   164    protocol          = "TCP"
   165  
   166    default_action {
   167      target_group_arn = aws_lb_target_group.bacalhau_lb_http_target_group.arn
   168      type             = "forward"
   169    }
   170  }
   171  
   172  
   173  resource "aws_lb_listener" "bacalhau_listener" {
   174    load_balancer_arn = aws_lb.nlb.arn
   175    port              = 54545
   176    protocol          = "TCP"
   177  
   178    default_action {
   179      target_group_arn = aws_lb_target_group.bacalhau_lb_bac_target_group.arn
   180      type             = "forward"
   181    }
   182  }
   183  resource "aws_lb_target_group" "bacalhau_lb_http_target_group" {
   184    name     = "bacalhau-lb-http-target"
   185    port     = 80
   186    protocol = "TCP"
   187    vpc_id   = aws_vpc.bacalhau_vpc.id
   188    health_check {
   189      path = "/"
   190      port = 80
   191    }
   192  }
   193  
   194  
   195  resource "aws_lb_target_group" "bacalhau_lb_bac_target_group" {
   196    name     = "bacalhau-lb-bac-target-${random_id.run.hex}"
   197    port     = 54545
   198    protocol = "TCP"
   199    vpc_id   = aws_vpc.bacalhau_vpc.id
   200  }
   201  
   202  
   203  resource "aws_key_pair" "bacalhau_deployer_key" {
   204    key_name   = "bacalhau-deployer-key-${random_id.run.hex}"
   205    public_key = file("${var.PATH_TO_PUBLIC_KEY}")
   206  }
   207  
   208  module "instance" {
   209    source = "./modules/instance"
   210  
   211    count = var.NUMBER_OF_NODES
   212  
   213    PATH_TO_PUBLIC_KEY             = var.PATH_TO_PUBLIC_KEY
   214    PATH_TO_PRIVATE_KEY            = var.PATH_TO_PRIVATE_KEY
   215    SUBNET_ID                      = aws_subnet.bacalhau_public_1_a.id
   216    AWS_INTERNET_GATEWAY_ID        = aws_internet_gateway.bacalhau_gw.id
   217    SECURITY_GROUP_ALLOW_SSH_ID    = aws_security_group.allow_ssh_and_bacalhau.id
   218    AWS_KEY_PAIR_DEPLOYER_KEY_NAME = aws_key_pair.bacalhau_deployer_key.key_name
   219    AMIS                           = var.AMIS
   220    AWS_REGION                     = var.AWS_REGION
   221    INSTANCE_TYPE                  = "t2.small"
   222    NODE_NUMBER                    = tostring(count.index)
   223  }
   224  
   225  output "instance_public_dns" {
   226    description = "Public DNS address of the EC2 instance"
   227    value       = module.instance.*.public_dns
   228  }
   229  
   230  output "instance_private_dns" {
   231    description = "Private DNS address of the EC2 instance"
   232    value       = module.instance.*.private_dns
   233  }
   234  
   235  output "instance_private_ips" {
   236    description = "Private IPs address of the EC2 instance"
   237    value       = module.instance.*.instance_private_ip
   238  }