First, you have to retrieve information from the metadata and later you will be able to run your own set of environment commands.
So first, run this curl statement.
Then you can call the describe-tags using the pre-installed AWS CLI (or install it on your AMI) You can call describe-tags using your AWS CLI and if not pre-installed, install it.
aws ec2 describe-tags --filters "Name=resource-id,Values=i-5f4e3d2a" "Name=Value,Values=DB_PORT"
After this, you can use the Operating System’s set environment variable command.
You can also check out this shell script which was written with the above instructions.