在AWS实例中设置环境变量 [英] Set environment variables in an AWS instance

查看:728
本文介绍了在AWS实例中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下资源在EC2中使用地形创建了 AMI :

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")}"
}

用户数据执行安装Java JRE的 shell脚本:

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

但是所有环境变量都不起作用.但是,如果我通过ssh连接到实例并执行导出命令,则它可以正常工作.

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.

有什么方法可以用terraform定义环境变量吗?

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

推荐答案

使用export命令仅为当前shell以及从该shell启动的所有进程设置这些变量.这不是持久性设置.您希望设置为永久的任何内容都应在/etc/environment中设置.

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.

例如在userdata中:

For example in userdata:

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

这会将JAVA_HOME=/jdk1.8.0_172行添加到该文件.请注意,您不应在该文件内使用export.

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

PATH变量可能已经在/etc/environment文件中定义,并且如果要向其附加其他路径,则需要适当地覆盖它.

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.

这篇关于在AWS实例中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆