1data "aws_ami" "test" {
2  most_recent = true
3
4  owners = var.ami_owners
5
6  filter {
7    name   = "virtualization-type"
8    values = ["hvm"]
9  }
10
11  filter {
12    name   = "is-public"
13    values = ["false"]
14  }
15
16  filter {
17    name   = "name"
18    values = ["consul-test-*"]
19  }
20}
21
22# ---------------------------------------------------------------------------------------------------------------------
23# Start up test servers to run tests from
24# ---------------------------------------------------------------------------------------------------------------------
25resource "aws_security_group" "test-servers" {
26  name   = "${local.random_name}-test-server-sg"
27  vpc_id = module.vpc.vpc_id
28
29  ingress {
30    from_port       = 8500
31    to_port         = 8500
32    security_groups = [module.consul_clients.security_group_id]
33    protocol        = "6"
34    cidr_blocks     = ["0.0.0.0/0"]
35  }
36  ingress {
37    from_port   = 22
38    to_port     = 22
39    protocol    = "6"
40    cidr_blocks = ["0.0.0.0/0"]
41  }
42  egress {
43    from_port   = 0
44    to_port     = 0
45    protocol    = "-1"
46    cidr_blocks = ["0.0.0.0/0"]
47  }
48}
49
50resource "aws_instance" "test-server" {
51  ami                         = var.test_server_ami == null ? data.aws_ami.test.id : var.test_server_ami
52  instance_type               = var.test_instance_type
53  key_name                    = module.keys.key_name
54  vpc_security_group_ids      = toset([aws_security_group.test-servers.id])
55  associate_public_ip_address = var.test_public_ip
56  subnet_id                   = (module.vpc.public_subnets)[0]
57  provisioner "remote-exec" {
58    inline = [
59      "export LB_ENDPOINT=${module.alb.this_lb_dns_name}",
60      "k6 run -q /home/ubuntu/scripts/loadtest.js"
61    ]
62    connection {
63      type        = "ssh"
64      user        = "ubuntu"
65      timeout     = "1m"
66      private_key = module.keys.private_key_pem
67      host        = aws_instance.test-server.public_ip
68    }
69  }
70}
71