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.
$curl http://169.254.169.254/latest/meta-data/instance-id
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.
export DB_PORT=/what/you/retrieved/from/the/previous/call
You can also check out this shell script which was written with the above instructions.
https://github.com/12moons/ec2-tags-env