If you need to get the IAM Role information from the attached EC2 role directly, you can do the following:

IAM_ROLE=$(curl -s 169.254.169.254/latest/meta-data/iam/info | \
  jq -r '.InstanceProfileArn' | cut -d'/' -f2)