Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I created a tag on the AWS console for one of my EC2 instances.

image

However, when I look on the server, no such environment variable is set.

The same thing works with elastic beanstalk. env shows the tags I created on the console.

$ env

 [...]

 DB_PORT=5432

How can I set environment variables in Amazon EC2?

1 Answer

0 votes
by (44.4k points)

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

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...