Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


AWS EMR Instance naming

21 May 2023 »
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "ec2:CreateTags",
            "Resource": "*"
        }
    ]
}

bootstrap.sh

#!/bin/bash


sudo -H -u root bash -c 'pip3 install boto3'
pip3 install boto3

cat > /tmp/script.py <<EOF
import boto3
import os

instanceid=""

for line in open("/emr/logpusher/log/logpusher.log"):
    if "node/i-" in line:
        instanceid = line.split("/node/")[1].split("/")[0]
        break

instancerole = os.popen("grep instanceRole /emr/instance-controller/lib/info/job-flow-state.txt | cut -d: -f2 | sed 's/[ :\"]//g'").read().strip()

jobflowid = os.popen("grep jobFlowId /emr/instance-controller/lib/info/job-flow-state.txt | cut -d: -f2 | sed 's/[ :\"]//g'").read().strip()

ec2 = boto3.client("ec2", region_name="sa-east-1")

ec2.create_tags(
    Resources=[instanceid],
    Tags=[
        {
            "Key": "Name",
            "Value": f"{instancerole[0].upper()}-{jobflowid}",
        }
    ],
)
EOF

python3 /tmp/script.py