How to configure AWS EC2 user data values and bash script
Unfortunately, you don't describe which type of Linux that you are using, but bash could be a good starting point. When an EC2 instance is starting, the user data will be executed, and the variable will be available only during that session.
If you want to use ENV variables, you can implement them in .bashrc
of the root (or in that user's .bashrc
which will run the third party application). If you want system wide ENV variables, you should implement them in /etc/profile
or /etc/environment/
. Include the variables in your user-data bash script like this: echo KEY=VALUE >> /etc/environment
You can give try to this article/howto also, with more examples.
Related videos on Youtube
cserpell
Updated on September 18, 2022Comments
-
cserpell over 1 year
When launching a AWS EC2 instance, you can give a user data string that is interpreted either as key, value pairs with some configuration variables, or as a bash script, if it begins with a
#!/bin/bash
line.We are using a third party software that needs a key, value pair to be given, but also we are configuring the machine at start time using a bash script. I added the key, value pair as an environment variable in the bash script using
export KEY=VALUE
, but it does not seem to work and arrive to the third party program configuration.I know that I could give the key, value pairs and then run the bash script using
ssh
, but it is much harder, as I have to wait the instance to be ready to do it.Has someone done this successfully?
-
cserpell about 7 yearsI was talking about AWS EC2 user data, and not shell environment variables. User data variables are taken by AWS instance manager as a configuration to do things without having to call to commands directly.