How to create Environment variable (space) in AWS? - java

I am new to Amazon Web Services. I have used Elastic BeanStalk to host our web application. In web application, I have used the environment variable assigned with some location to push the files into that.
I have set the VENDOR_HOME as,
D:\Applications\spring-boot\Resume_folder
and accessing through java using System.getenv in as
String path = System.getenv("VENDOR_HOME");
It is working fine in the local tomcat server, I want the same functionality should achieve in AWS too? any help will be appreciated.

In your BeanStalk application, go to the Configuration page. Locate Software Configuration. Click on edit. You have the option to add Environment Properties at the bottom of this page.

Tomcat typically has a setenv.sh file where admins can set environment variables. In the setenv.sh file you can put a line for your VENDOR_HOME variable as such:
export VENDOR_HOME=/Applications/spring-boot/Resume_folder
For AWS BeanStalk specifically you can look at this answer for details on how to set the env variables.
In general you can set Linux environmental variables using the export command from within the script you are using to execute your application. The final answer will depend on how you are executing your application.
I typically use Spring Boot Maven Plugin that creates executable JAR files. The executable JAR file allows for a config file with the same name as the JAR file (.config instead of .jar) where admins can export environmental variables from.

Related

How to add Azure Monitor OpenTelemetry-based auto-instrumentation to Spring Boot Application?

The documented way to add Azure Monitor OpenTelemetry to Java application is by downloading applicationinsights-agent-3.2.11.jar and using the following:
-javaagent:path/to/applicationinsights-agent-3.2.11.jar.
So in Spring Boot, the way it could possibly be run:
java -javaagent:path/to/applicationinsights-agent-3.2.11.jar -jar <jar-file.jar>.
But what happens if this path varies?
The problem is depending on the system it is running, and using maven (pom.xml) to get the artifact, how do we enable opentelemetry with applicationinsights-agent-3.2.11.jar since the location Maven stores the artifact changes from user accounts and computer?
Additionally, how do I specify a relative path to applicationinsights.json file for configuration (as Azure looks for this file inside the applicationinsights-agent-3.2.11.jar directory)?
If you specify a relative path, it will be resolved relative to the
directory where applicationinsights-agent-3.2.11.jar is located.
UPDATE
Regarding suggestions, How to define a relative path in java and this other suggested question
I am not looking to read files from relative paths.
The spring boot application needs to be invoked with a specific argument where it needs to be made aware of the location where maven downloads the the appinsights jar file.
Then when the app starts, the appinsights autoconfigure based on applicationinsights.json file, which once again, may vary by location.
Note that the latest applicationinsights-agent*.jar is available through GitHub and not through Maven repository. Therefore, I don't think that it can be downloaded as dependency to the project using the pom.xml
Now, the question here is not specific to ApplicationInsights agent, but for any java agent used for monitoring. The -javaagent parameter is supplied to JVM (java commandline) along with the path. Therefore, it will have to be supplied when JVM starts. How it is being setup would depend on the Server (or the standalone application) being used and its starting mechanism. One such solution is discussed here: How to attach a java agent on to a running spring-boot application. As there are many ways that the application can be deployed/run, the relative path would vary based on it.
If you are running standlone springboot application, you may also modify the mvnw or mvnw.cmd scripts to include %MAVEN_OPTS% with the -javaagent=agent path, where path could be relative to one of the variables defined in it, like %MAVEN_PROJECTBASEDIR%
Regarding the applicationinsights.json file, you can either have it in the same directory as the agent jar, OR set up environment variables to get the settings from there (instead of having the json file). You may refer to this link for details on available Environment variables to configure the agent. These environment variables can be set based on how/where the application is being deployed/run before the JVM initializes to make it available to javaagent.

Java Spring project environment property file for both Linux and Window

I have a java spring project that contains property files. In the file, there are lots of folder path definitions. But these path are Linux paths. i.e. /home/share/Document. I would like to develop the project under both Linux and Windows so I have created a network share that I can access Linux file under Window environment. However, I have to add a prefix in front of the Linux path
such as \network\...\home\share\Document in order to make the path work.
What is the standard/normal way to handle this? I can image I should have two environment property files. For example, one is called DEV_unix.properties and the other is call DEV_win.properties. But this doesn't look perfect.
Could someone share his/her insights please ?
You can use profile in this case. Set active profile in your application.{profile}.properties. You can set this profile as a environment variable or VM arguments so that you need to set the environment variable in different machines based on the Operating system. You can check the below link for more details.
Springboot not loading application.dev.properties file

How to modify a properties file in spring boot jar

I am writing a Spring Boot Application.
This application talks to a Main Frame through MessageQueues
So inorder to talk to that MainFrame we are provided with a java jar(which has the code that talks to MessageQueues ). lets call it proxy jar.
We have to use the operations written from the above mentioned proxy jar .
We dont write any code to read/write to MessageQueues (IBM MessageQueues) in our application, this is done by the proxy jar and its dependent runtime jar.
So here comes the problem, this Proxy jar is coded in such a way that it looks for a file called 'commcfg.properties' in the classpath (expects the name to be exactly same). Actually the Proxy jar uses another dependent jar (lets call runtime jar)which reads the queue details from commcfg.properties and reads/writes from Message Queues.
commcfg.properties have the values of MessageQueue and Host and port of those Queues . In short Queue details.
So the trouble is these MessageQueue details will be different in different environments. I need to use the commcfg.properties according to environment
So far my trials to solve this.
Lets say I have two environments .
So I will have two different set of values of commcfg.properties
I have created files as follows
commcfg.DEV.properties
commcfg.PROD.properties so that they will be in classpath
Next I have written code such that it will pick up the particular property
file depending on Environment and rename it to commcfg.properties(as the
Proxy jar and its helper dependent Runtime jar need the exact name to be
commcfg.properties).
In my local workspace I could do this .
But when deployed (we use docker) that gets packaged into spring boot jar.
So my hack is not working as we cannot rename files with in jars.
Another thought I had:
Before I call my functionality code , I will put the commcfg.properties in
class path (project/src/main/resources)
I will read it and modify the values as needed per environment.
The values I will have them in application.properties.
But again I doubt that with in jar I cannot modify the file.
Hoping I am clear
Kindly help me out..
In spring boot, You can register your properties file with a Java annotation #PropertySource
#SpringBootApplication
#PropertySource("classpath:commcfg.properties")
public class ApplicationConfig
{
public static void main(String args[])
{
SpringApplication.run(ApplicationConfig.class);
}
}
and let the commcfg.properties file contain the DEV environment details
messageQueue.name=myMessageQueue
messageQueue.host=host
messageQueue.port=4040
Now build your jar file and you get yourjar.jar
Now run it in DEV environment with default values
java -jar yourjar.jar
run it in QA environment with overriding the default values in your config file
java -jar --messageQueue.name=diffrenthost --messageQueue.host=diffrenthost --messsageQueue.port=diffrentport yourjar.jar
This way you can override your default values. I hope this helps
you can put the properties files on the same path of spring-boot jar, the config items can override the properties in the jar.

Change $home for tomcat only?

I have two instances of tomcat that run under the same user in a test/dev environment. In each instance there is an application that writes configuration data to the same directory in the $home of the user that runs tomcat.
I'd like to set one instance of tomcat so that it thinks $home is somewhere else. Is this possible? If I do export home=foo/bar when I run one instance, will that affect the other instance?
Obviously I know that I can run each instance as a different user, I do not want to do that in this case.
Just create a file "setenv.sh" for both instances
{TOMCAT1_BASE_DIR}/bin/setenv.sh:
export home=/foo/bar1
{TOMCAT2_BASE_DIR}/bin/setenv.sh:
export home=/foo/bar2
On startup tomcat will load environment variables defined in setenv.sh
You can overwrite a variable for a specified command without exporting it this way:
HOME=/home/user command.sh
The HOME variable will be changed for command.sh but will still be your usual home everywhere else.
Edit {TOMCAT_INSTANCE}/bin/setenv.sh to set all required environment variables per instance.

Environment variables in Ubuntu Tomcat, Apache and Java

I have a java web app running in Windows enviroment and I want to move it to Ubuntu system.
In windows I use an environment variable to store the main path:
MAIN_PATH=C:\test
This variable is used in many configuration files and also by application, for example:
Apache and Tomcat configuration file
Application logging configuration (log4j)
Java application itself by System.getEnv(...)
examples:
tomcat config
<Host name="localhost" appBase="${MAIN_PATH}/webapps" unpackWARs="true" autoDeploy="true">
apache virtualhost
<VirtualHost *:80>
DocumentRoot "${MAIN_PATH}/www"
ServerName testmain
<Directory "${MAIN_PATH}/www">
...
I can not find some way to set it in ubuntu, I tried this places:
/etc/enviroment
/etc/profile
export MAIN_PATH=/opt/test in /etc/init.d/tomcat7.sh
All this solution didn't work or works just in one of my requirements (only in server configuration but not in application or vice versa).
Is nothing like windows global environment variable to use for all this cases?
Did you check ubuntu/linux documentation? It's fairly simple.
Refer to: https://help.ubuntu.com/community/EnvironmentVariables
This worked perfectly for me.
$ nano /etc/environment (or whatever file editor you want to use)
add 'export MAIN_PATH=/opt/test' to the file and save
$ source /etc/environment
$ echo $MAIN_PATH
Output: /opt/test
I would recommend you keep the environment variables separate for each layer of your application. For example it might seem like putting everything in one place makes sense, but I don't feel it's good design to share a configuration directory for Apache and Tomcat. Both have standard locations for config files (e.g., $CATALINA_HOME/conf).
I wouldn't think you're using log4j on Apache so why put it in a custom directory that's shared with Apache. Put it in a conf folder inside your webapp, or in the previously mentioned top level conf directory. Maybe you'll have more than one webapp on Tomcat -- in that case it makes even more sense to have logging config files within the particular webapp directory.
It's also cleaner to have the environment variables contained within the process. They don't gum up the global environment variables. For example when you start Tomcat, the setenv.sh script runs and sets all the enviroment variables just for the java process that's running Tomcat.

Categories