Skip to content

Commit c05fd0b

Browse files
Minor cleanup and improvements (#13)
* Prometheus directories using a shell script, not EC2 User Data * Grafana directories are prepared inside their own script * [docs] First draft of documentation
1 parent 49b523f commit c05fd0b

12 files changed

Lines changed: 193 additions & 16 deletions

docs/ETHEREUM.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# WIP
2+
3+
Document is WIP
4+
5+
# Ethereum
6+
7+
This node is needed for the following chains:
8+
9+
- `mainnet`
10+
- `sepolia`
11+
- `base mainnet` because L2 chains require an integration with an L1 node
12+
- `base sepolia` because L2 chains require an integration with an L1 node
13+
14+
# Order of Setup Actions
15+
16+
## 1. AMI
17+
18+
We select the AMI to be an Ubuntu with EBS root device and EBS optimized.
19+
20+
See [../terraform/ethereum_ec2_instance.tf](../terraform/ethereum_ec2_instance.tf) resource `data.aws_ami.ubuntu`.
21+
22+
## 2. EC2 Instance
23+
24+
### User Data
25+
26+
#### Configure custom SSH port
27+
28+
### Systems Manager Agent
29+
30+
This is already installed on the AMI. So, there is no need to install.
31+
32+
However, there is need to configure.
33+
34+
- 1st it should have the correct privileges and have all its directories ready
35+
- 2nd it should be sending its logs to cloudwatch agent.
36+
37+
Does this mean that I need to have cloudwatch agent installed first?
38+
Maybe the correct order of things is:
39+
40+
41+
- prepare directories for ssm-agent
42+
- configure ssm agent to log in json format
43+
- restart ssm agent
44+
45+
46+
47+
Then at some point we prepare the cloud watch log groups for SSM agent. Does this action have any dependency. No it doesn't.
48+
49+
50+
51+
Then maybe cloudwatch agent should be the last step in the process.
52+
- install cloudwatch agent
53+
- configure cloudwatch agent to read all the services files we want to send over.
54+
This ^ needs to be done after all cloudwatch log groups have been prepared. So, there is a dependency to them.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
set -e # e: exit if any command has a non-zero exit status
4+
set -x # x: all executed commands are printed to the terminal
5+
set -u # u: all references to variables that have not been previously defined cause an error
6+
7+
# The purpose of this script is to prepare the necessary directories
8+
# for Grafana. It should be executed after the second EBS volume
9+
# is attached to the EC2 Ethereum node instance and after the /data
10+
# folder is mounted.
11+
12+
USER=$1
13+
14+
sudo mkdir -p /data/grafana/data
15+
sudo mkdir -p /home/${USER}/grafana/datasources
16+
sudo mkdir -p /home/${USER}/grafana/dashboards
17+
sudo chown -R ${USER}:${USER} /home/${USER}/grafana
18+
19+
# On most Linux systems, the user ID 472 does not correspond to
20+
# named user default, which is why getent passwd 472 returns nothing.
21+
# This ID is commonly used by Grafana when running in Docker containers,
22+
# where the Grafana process inside the container runs as UID 472 and GID 0 (root).
23+
# On the host system, the ID may not have associated names, but setting
24+
# ownership to 472:root ensures that the containerized Grafana process can access
25+
# and modify the files, because it matches the UID/GID it runs as inside the container.
26+
sudo chown -R 472:root /data/grafana/data
27+
sudo chmod -R u+rwx,g+rx,g-w,o+rx,o-w /data/grafana/data
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -e # e: exit if any command has a non-zero exit status
4+
set -x # x: all executed commands are printed to the terminal
5+
set -u # u: all references to variables that have not been previously defined cause an error
6+
7+
# The purpose of this script is to prepare the necessary directories
8+
# for Prometheus. It should be executed after the second EBS volume
9+
# is attached to the EC2 Ethereum node instance and after the /data
10+
# folder is mounted.
11+
12+
USER=$1
13+
14+
# Create the "prometheus" folder
15+
sudo mkdir -p /home/${USER}/prometheus
16+
sudo chown ${USER}:${USER} /home/${USER}/prometheus
17+
18+
sudo mkdir -p /data/prometheus/data
19+
sudo chown -R nobody:nogroup /data/prometheus/data
20+
sudo chmod -R u+rwx,g+rx,g-w,o+rx,o-w /data/prometheus/data

terraform/ethereum_ec2_instance.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ resource "aws_instance" "ethereum" {
6868
})
6969
user_data_replace_on_change = false
7070

71-
7271
tags = {
7372
Name = "${var.project}-${var.environment}-ethereum"
7473
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
locals {
2+
ethereum_prepare_grafana_directories_filename = "prepare_grafana_directories.sh"
3+
destination_ethereum_prepare_grafana_directories_filename = "/home/${var.ethereum_node.user}/ethereum_${local.ethereum_prepare_grafana_directories_filename}"
4+
}
5+
6+
resource "terraform_data" "ethereum_ec2_prepare_grafana_directories" {
7+
// 1st step is to copy the directory over to /home/${user} folder.
8+
// 2nd step is to execute the script
9+
10+
// Since it requires connection, it should depend on the ec2_instance
11+
12+
depends_on = [
13+
aws_instance.ethereum,
14+
terraform_data.ethereum_ec2_volume_preparation
15+
]
16+
17+
triggers_replace = {
18+
script_content = sha256(file("${local.bash_scripts_folder}/${local.ethereum_prepare_grafana_directories_filename}"))
19+
}
20+
21+
connection {
22+
type = "ssh"
23+
user = var.ethereum_node.user
24+
port = var.ethereum_node.ssh_port
25+
host = aws_instance.ethereum.public_ip
26+
private_key = file("${path.module}/ethereum.pem")
27+
}
28+
29+
// copy file over
30+
provisioner "file" {
31+
source = "${local.bash_scripts_folder}/${local.ethereum_prepare_grafana_directories_filename}"
32+
destination = local.destination_ethereum_prepare_grafana_directories_filename
33+
}
34+
35+
// make it executable and execute it
36+
provisioner "remote-exec" {
37+
inline = [
38+
"chmod +x ${local.destination_ethereum_prepare_grafana_directories_filename}",
39+
"sudo ${local.destination_ethereum_prepare_grafana_directories_filename} ${var.ethereum_node.user}",
40+
]
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
locals {
2+
ethereum_prepare_prometheus_directories_filename = "prepare_prometheus_directories.sh"
3+
destination_ethereum_prepare_prometheus_directories_filename = "/home/${var.ethereum_node.user}/ethereum_${local.ethereum_prepare_prometheus_directories_filename}"
4+
}
5+
6+
resource "terraform_data" "ethereum_ec2_prepare_prometheus_directories" {
7+
// 1st step is to copy the directory over to /home/${user} folder.
8+
// 2nd step is to execute the script
9+
10+
// Since it requires connection, it should depend on the ec2_instance
11+
12+
depends_on = [
13+
aws_instance.ethereum,
14+
terraform_data.ethereum_ec2_volume_preparation
15+
]
16+
17+
triggers_replace = {
18+
script_content = sha256(file("${local.bash_scripts_folder}/${local.ethereum_prepare_prometheus_directories_filename}"))
19+
}
20+
21+
connection {
22+
type = "ssh"
23+
user = var.ethereum_node.user
24+
port = var.ethereum_node.ssh_port
25+
host = aws_instance.ethereum.public_ip
26+
private_key = file("${path.module}/ethereum.pem")
27+
}
28+
29+
// copy file over
30+
provisioner "file" {
31+
source = "${local.bash_scripts_folder}/${local.ethereum_prepare_prometheus_directories_filename}"
32+
destination = local.destination_ethereum_prepare_prometheus_directories_filename
33+
}
34+
35+
// make it executable and execute it
36+
provisioner "remote-exec" {
37+
inline = [
38+
"chmod +x ${local.destination_ethereum_prepare_prometheus_directories_filename}",
39+
"sudo ${local.destination_ethereum_prepare_prometheus_directories_filename} ${var.ethereum_node.user}",
40+
]
41+
}
42+
}

terraform/locals.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ locals {
22
current_account_id = data.aws_caller_identity.current.account_id
33
current_user_arn = data.aws_caller_identity.current.arn
44

5-
templates_folder = "${path.module}/../templates"
5+
templates_folder = "${path.module}/../templates"
6+
bash_scripts_folder = "${path.module}/../bash"
67

78
cloudwatch_namespace = title("/${var.company}/${var.project}/${var.environment}")
89

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ethereum_prepare_grafana_directories.tf
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ethereum_prepare_prometheus_directories.tf
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ethereum_prepare_grafana_directories.tf

0 commit comments

Comments
 (0)