First of all I am new to Lambda function in AWS and all. I need to access the lambda environment variables into my spring-boot's application.properties file to put RDS username, pass etc. Can someone please give me any hint ?
datasource:
url: ${DBURL}
username: ${UNAME}
password: ${PASS}
Your example should work as it is in your question, are you getting any errors?
Alternatively, you could rename your environment variables to:
SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD and Spring Boot will automatically pick up those values and use them, without needing anything in application.properties.
Related
In a spring boot application, I would like to pass some environment variables from env files (available for different environments) to the application.yml. We decided not to use spring profiles for different environments as using .env files make the later migrations to cloud easier for us. Defining these variables as following in the application.yml:
datasource:
driverClassName: org.mariadb.jdbc.Driver
username: "${SPRING_DATASOURCE_USERNAME}"
password: "${SPRING_DATASOURCE_PASSWORD}"
Furthermore, I have exported all the environment variables in the .env file which also includes ${SPRING_DATASOURCE_USERNAME} and ${SPRING_DATASOURCE_PASSWORD} using the following command:
export $(grep -v '^#' .env | xargs -d '\n')
When running
./gradlew test
I get the following error:
Missing property (SPRING_DATASOURCE_USERNAME) for Groovy template expansion.
The problem is that gradle wrapper does not get the system environment variables or exported environment variables. Is there a way to solve this using .env files? Any hints are appreciated!
Since you've exported them as environment variables, you could remove spring.datasource.username and spring.datasource.password from your application.yml. Spring will search them automatically from the environment variables. You don't need to specify them like templates. An extreme usage is exporting all the properties via environment variables and leaving application.yml empty.
References
Priority: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
Rules: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding.environment-variables
I have a quarkus based webapp that uses Basic Authentication with Embedded Realm Configuration. The webapp runs in a docker container. The authentication properties are specified in application.properties like this:
quarkus.http.auth.basic=true
quarkus.security.users.embedded.enabled=true
quarkus.security.users.embedded.plain-text=true
quarkus.security.users.embedded.users.test=mypass
quarkus.security.users.embedded.roles.test=admin
I would like to override quarkus.security.users.embedded.users.test to specify a password at docker runtime.
I tried to do this by overriding the quarkus property using an environment variable at docker runtime.
docker run -p 9999:9999 -e QUARKUS_SECURITY_USERS_EMBEDDED_USERS_TEST=newpasswd mywebapp
This does not work. When I access mywebapp using http://locahost:9999 I must login using test/mypass. I expected to login using test/newpasswd.
Any help greatly appreciated.
I suggest you report an issue for this problem. While Quarkus won't be able to "enumerate" any new users from environment variables (the correct property names cannot be deduced), I think it should still be possible to override the password of a user that has already been specified in application.properties using an environment variable like you try to do.
I'm trying to check in my Hasura config.yaml file in a way that would be agnostic to my Hasura endpoint. The idea is that each developer will check out the project and work on a different Hasura instance, and then we would want to deploy and apply migrations separately to staging and production servers.
Is there, for instance, a way to make config.yaml get values from an .env file?
You can create a config.yaml.template file that contains the endpoints.
In this file you can define the endpoint like this:
endpoint: ${HASURA_ENDPOINT}
On startup of your hasura container you can generate a config.yaml file with envsubst:
envsubst '$HASURA_ENDPOINT' < /my_hasura_dir/config.yaml.template > /my_hasura_dir/config.yaml
You can use the —endpoint flag when executing commands. Similarly you can use flags for passing in admin secret and so on. Alternatively you can also use environment variables.
Read more here https://docs.hasura.io/1.0/graphql/manual/deployment/graphql-engine-flags/config-examples.html
I am wondering how to resolve problem: I have a spring-boot app on docker that connects to db and some other service.
Probably some clients will have db on other urls than the others.
I use spring.datasource.url property to connect to DB. Should I add it to args and use:
Properties properties = new Properties();
properties.put("spring.datasource.url", args[1]);
application.setDefaultProperties(properties);
And something like that will override it ? But every run will need adding DB url. Or use something else?
datasource could be read as a variable from the docker-compose file:
assume this is your docker-compose file:
version: '2'
services:
db:
image: customimage_mysql
restart: always
ports:
- "3306:3306"
application:
build: .
ports:
- "9111:9111"
depends_on:
- db
links:
- db
environment:
- database.url=jdbc:mysql://mysql-docker-container:3306/spring_app_db?
Now you have 2 options:
set different values for databse.url inside docker compose and build image for each app correspondingly
set different variables (databse1.url , databse2.url,databse3.url, ...) inside docker-compose file, and reference to them from
application.properties:
application.properties
spring.datasource.url=${database.url}
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
server.port=9111
According to the information that you have provided here, the database link should be a configuration to your application. basically you need to have a configurations file
application.properties
And when you want to change the URL, just change it in the configuration file and build.
you can find the documentation here
And moreover if you are using devops environment like kubernetes, you would have to have a config-map and your deployments will get configurations from those config-maps which are like application.properties files.
If you've got lots of deployments each with their own database then that will take some management one way or another. But you don't want it to require lots of builds of your app - you want to externalise that configuration.
Sorting boot has features for externalising configuration (https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html). The simplest for your case would be to use environment variables that override properties by relaxed binding of names (https://github.com/spring-projects/spring-boot/wiki/Relaxed-Binding-2.0). If app is started with an environment variable named SPRING_DATASOURCE_URL then this value will override what you have in your properties for spring.datasource.url. Your properties file effectively sets a default value that you can override. This is out of the box behaviour for Spring Boot and applies to other properties too (inc all the db ones, though if you've got databases of different types then you'll want to include all the relevant driver jars in your build).
Since you're using docker you can set environment variables in the container at deploy/startup time using a -e parameter. So you can override at deploy time for each deployed instance.
You might well use further layers on top of docker like docker-compose or Kubernetes. Then you might get into setting the environment variables in deployment descriptor files that describe your deployment configuration. But that configuration management question is at a different layer/stage and is no longer part of the build step once you have your config externalised.
I have a credentials file that is located at a different directory location than my jar. In it is the Username and Password I need to connect to my DataSource so I can run my Batch job. The Spring-Batch job is set up to read its datasource info from a yaml file. As such I am wandering how I can pull the username and password from the credential file and put it into the Spring.dataSource.username & password variables on the yml file without needing to create some java method. I tried to test it with the below code but it was never able to pull the username or password. Does anyone have any suggestions?
YAML file
spring:
config:
additional-location: C:/accessInfo.credentials
datasource:
username: ${access.user}
password: ${access.password}
Credentials File
access.user=user
access.password=password