Skip to content

Instantly share code, notes, and snippets.

@galargh
Created March 28, 2023 08:06
Show Gist options
  • Save galargh/32f92fe2ea28cda0ff9f17d803644afa to your computer and use it in GitHub Desktop.
Save galargh/32f92fe2ea28cda0ff9f17d803644afa to your computer and use it in GitHub Desktop.
libp2p benchmarks
#!/bin/bash
set -e
trap cleanup EXIT
function cleanup {
echo "Destroying infrastructure..."
terraform destroy -auto-approve
echo "Infrastructure destroyed."
}
# Apply the Terraform configuration
echo "Applying Terraform configuration..."
terraform init
terraform apply -auto-approve
echo "Terraform configuration applied."
# Run the benchmark script
echo "Running benchmark..."
./run_benchmark.sh $(terraform output -json instance_ids | jq -r '.[]')
echo "Benchmark completed."
#!/bin/bash
INSTANCE_IDS=("$@")
DOCUMENT_NAME="example"
# Send the command
COMMAND_ID=$(aws ssm send-command \
--instance-ids "${INSTANCE_IDS[@]}" \
--document-name "$DOCUMENT_NAME" \
--query 'Command.CommandId' \
--output text)
# Poll the status of command invocations
while true; do
STATUS_SUMMARY=$(aws ssm list-command-invocations \
--command-id "$COMMAND_ID" \
--query 'CommandInvocations[*].{InstanceId:InstanceId,Status:Status}' \
--output text)
echo "Command status:"
echo "$STATUS_SUMMARY"
COMPLETED_COUNT=$(echo "$STATUS_SUMMARY" | grep -c -E "(Success|Failed|Cancelled|TimedOut)")
TOTAL_COUNT=${#INSTANCE_IDS[@]}
if [ "$COMPLETED_COUNT" -eq "$TOTAL_COUNT" ]; then
break
else
echo "Waiting for command invocations to finish..."
sleep 10
fi
done
echo "All command invocations have completed."
{
"schemaVersion": "2.2",
"description": "Print the IP of the instance and the IPs of other instances",
"mainSteps": [
{
"action": "aws:runShellScript",
"name": "printIPs",
"inputs": {
"runCommand": [
"#!/bin/bash",
"echo 'Instance IP:'",
"curl -s http://169.254.169.254/latest/meta-data/local-ipv4",
"echo 'Other instances IPs:'",
"echo '${instance_ips}' | jq -r '.[] | select(. != \"$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)\")'"
]
}
}
]
}
provider "aws" {
region = "us-west-2"
}
variable "instance_count" {
description = "Number of instances to create"
default = 2
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH inbound traffic"
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-0c55b159cbfafe1f0" # This is the Amazon Linux 2 AMI ID, but you should use the latest one in your region.
instance_type = "t2.micro"
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
tags = {
Name = "Example Instance ${count.index + 1}"
}
}
data "template_file" "ssm_document" {
template = file("${path.module}/ssm_document.tpl")
vars = {
instance_ips = jsonencode([for instance in aws_instance.example : instance.private_ip])
}
}
resource "aws_ssm_document" "example" {
name = "example"
document_type = "Command"
content = data.template_file.ssm_document.rendered
}
output "instance_ids" {
value = [for instance in aws_instance.example : instance.id]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment