Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uninversal aws infrastructure #23

Merged
merged 9 commits into from
Jul 31, 2020
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea/benchmarks.iml
.idea/
files/report/
terraform.tfstate.backup
terraform.tfstate
aws-infrastructure/.terraform/
aws-infrastructure/aws_ssh_key
aws-infrastructure/aws_ssh_key.pub
aws-infrastructure/.terraform.tfstate.lock.info
30 changes: 30 additions & 0 deletions aws-infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# aws-infrastructure

This directory contains files for running 2 AWS instances(`client` and `server`) for benchmarking needs.

To start use `start.sh`:

```bash
$ ./start.sh
```

The following env variables are exported during the script run:

```bash
echo "$PRIVATE_CLIENT_IP_ADDR"
echo "$PRIVATE_SERVER_IP_ADDR"
echo "$PUBLIC_CLIENT_IP_ADDR"
echo "$PUBLIC_SERVER_IP_ADDR"
```

Connect to client instance with ssh:

```bash
$ ssh -i aws_ssh_key ubuntu@$PUBLIC_CLIENT_IP_ADDR
```

In order to stop use `stop.sh`:

```bash
$ ./stop.sh
```
161 changes: 161 additions & 0 deletions aws-infrastructure/infrastructure.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# This file contains terraform AWS infrostructure for benchmarking


# Security and variables

variable "access_key" {
description = "AWS Access Key"
}

variable "secret_key" {
description = "AWS Secret Key"
}

provider "aws" {
access_key = var.access_key
secret_key = var.secret_key
region = "eu-central-1"
}

resource "aws_key_pair" "aws_ssh_key" {
key_name = "aws_ssh_key"
public_key = file("aws_ssh_key.pub")
}


# Dedicated network

resource "aws_vpc" "perf_net" {
cidr_block = "192.168.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "perf-net",
Project = "Artipie Performance"
}
}

resource "aws_subnet" "perf_subnet" {
cidr_block = cidrsubnet(aws_vpc.perf_net.cidr_block, 12, 1234)
vpc_id = aws_vpc.perf_net.id
availability_zone = "eu-central-1b"
Sammers21 marked this conversation as resolved.
Show resolved Hide resolved
}

resource "aws_internet_gateway" "perf_gw" {
vpc_id = aws_vpc.perf_net.id
tags = {
Name = "perf-gw",
Project = "Artipie Performance"
}
}

resource "aws_route_table" "perf_routes" {
vpc_id = aws_vpc.perf_net.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.perf_gw.id
}
tags = {
Name = "perf_routes",
Project = "Artipie Performance"
}
}

resource "aws_route_table_association" "perf_subnet_association" {
subnet_id = aws_subnet.perf_subnet.id
route_table_id = aws_route_table.perf_routes.id
}


# Allow traffic

resource "aws_security_group" "allow_ssh_sg" {
name = "allow-all"
vpc_id = aws_vpc.perf_net.id
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 0
protocol = "-1"
}
egress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 0
protocol = "-1"
}
}

data "aws_ami" "ubuntu" {
most_recent = true

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["099720109477"] # Canonical
}


# Client and Server AWS instances:

resource "aws_instance" "client" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.medium"
key_name = aws_key_pair.aws_ssh_key.key_name
associate_public_ip_address = true
security_groups = [aws_security_group.allow_ssh_sg.id]
subnet_id = aws_subnet.perf_subnet.id

connection {
type = "ssh"
user = "ubuntu"
host = self.public_ip
private_key = file("aws_ssh_key")
}
}

resource "aws_instance" "server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.medium"
Sammers21 marked this conversation as resolved.
Show resolved Hide resolved
key_name = aws_key_pair.aws_ssh_key.key_name
associate_public_ip_address = true
security_groups = [aws_security_group.allow_ssh_sg.id]
subnet_id = aws_subnet.perf_subnet.id

connection {
type = "ssh"
user = "ubuntu"
host = self.public_ip
private_key = file("aws_ssh_key")
}
}


# Output env variables

output "public_client_ip_addr" {
description = "The public IP of client machine"
value = aws_instance.client.public_ip
}

output "public_server_ip_addr" {
description = "The public IP of client machine"
value = aws_instance.server.public_ip
}

output "private_client_ip_addr" {
description = "The public IP of client machine"
value = aws_instance.client.private_ip
}

output "private_server_ip_addr" {
description = "The public IP of client machine"
value = aws_instance.server.private_ip
}
27 changes: 27 additions & 0 deletions aws-infrastructure/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -x

# Enter aws-infrastructure dir, whaterver the script is called from
cd "$( dirname "${BASH_SOURCE[0]}" )"

# Generate RSA key for ssh access if not exists
if [ ! -f "aws_ssh_key" ]
then
ssh-keygen -t rsa -b 4096 -f aws_ssh_key -N ""
fi

# Start AWS infrotructure
terraform init
terraform apply -input=false -auto-approve

# Export terraform output variables
export PRIVATE_CLIENT_IP_ADDR=$(terraform output private_client_ip_addr)
export PRIVATE_SERVER_IP_ADDR=$(terraform output private_server_ip_addr)
export PUBLIC_CLIENT_IP_ADDR=$(terraform output public_client_ip_addr)
export PUBLIC_SERVER_IP_ADDR=$(terraform output public_server_ip_addr)

Sammers21 marked this conversation as resolved.
Show resolved Hide resolved
# Wait for VMs to start
for IP in $PUBLIC_SERVER_IP_ADDR $PUBLIC_CLIENT_IP_ADDR
do
until timeout 30 ssh -i aws_ssh_key -oStrictHostKeyChecking=no ubuntu@$IP exit; do sleep 5 ; done
done
10 changes: 10 additions & 0 deletions aws-infrastructure/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -x

# Enter aws-infrastructure dir, whaterver the script is called from
cd "$( dirname "${BASH_SOURCE[0]}" )"

# Auto approve terrafrom commands
export TF_CLI_ARGS="-input=false -auto-approve"

terraform destroy