Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variables in an AWS instance

I create an AMI in EC2 with terraform with this resource:

resource "aws_instance" "devops-demo" {
  ami           = "jnkdjsndjsnfsdj"
  instance_type = "t2.micro"
  key_name      = "demo-devops"
  user_data     = "${file("ops_setup.sh")}"
}

The user data executes a shell script that install Java JRE:

  sudo yum remove java-1.7.0-openjdk -y
  sudo wget -O /opt/server-jre-8u172-linux-x64.tar.gz --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u172-b11/a58eab1ec242421181065cdc37240b08/server-jre-8u172-linux-x64.tar.gz"
  sudo tar xzf /opt/server-jre-8u172-linux-x64.tar.gz
  export JAVA_HOME=/jdk1.8.0_172
  export JRE_HOME=/jdk1.8.0_171/jre
  export PATH=$JAVA_HOME/bin:$PATH

But none of the environment variables work. However, if I connect by ssh to the instance and I execute the export command, it works fine.

Is there any way to define the environment variables with terraform?

like image 844
gtx911 Avatar asked Jun 03 '18 16:06

gtx911


People also ask

How do I add an environment variable in AWS?

To set environment variables Sign in to the AWS Management Console and open the Amplify console . In the Amplify console, choose App Settings, and then choose Environment variables. In the Environment variables section, choose Manage variables. In the Manage variables section, under Variable, enter your key.

How do I set environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

What are environment variables in AWS?

An environment variable is a pair of strings that is stored in a function's version-specific configuration. The Lambda runtime makes environment variables available to your code and sets additional environment variables that contain information about the function and invocation request.


2 Answers

Using the export command only sets those variables for the current shell and all processes that start from that shell. It is not a persistent setting. Anything you wish to make permanent should be set in /etc/environment.

For example in userdata:

echo "JAVA_HOME=/jdk1.8.0_172" >> /etc/environment

This would add the JAVA_HOME=/jdk1.8.0_172 line to that file. Note, you should not use export inside that file.

The PATH variable is likely already defined in the /etc/environment file and you'll need to overwrite that appropriately if you are going to append additional paths to it.

There is really great details on setting environment variables available in this answer.

like image 109
Brandon Miller Avatar answered Sep 28 '22 12:09

Brandon Miller


If you are using one of the Amazon Linux 2 AMIs, then /etc/environment will not work for you. However, you can add the environment variables to a new file at /etc/profile.d/ and this will work. Something like this would go in your user_data:

echo "JAVA_HOME=/jdk1.8.0_172" | sudo tee /etc/profile.d/java_setup.sh
echo "JRE_HOME=/jdk1.8.0_171/jre" | sudo tee -a /etc/profile.d/java_setup.sh
echo "PATH=$JAVA_HOME/bin:$PATH" | sudo tee -a /etc/profile.d/java_setup.sh
like image 29
eatsfood Avatar answered Sep 28 '22 12:09

eatsfood