Assumption
- My is a SpringBoot application correctly deployed and full working in an environment of EBS
- My serves static resources deployed in standard location src/main/resources
Static Resources configuration at runtime
We can use springboot property
spring.web.resources.static-locations
for re-define location where springboot context can find static resources.
To set the property value we can use an environment property
TOOLS_STATIC_RES
Use case
I set
TOOLS_STATIC_RES = 'classpath:/,classpath:/public,classpath:/static,file:/home/ec2-user'
Problem
All static resources under classpath are available but
all resources in local file system result in 404 (NOT FOUND) for My application
Note
I have successfully tested this configuration with My application jar running in a EC2 instance
Target
Use external repository (local file system or S3 bucket) for static web resources.
Thanks in advance for you support
Giorgio
Update 2024-06-06
EBS exposition of env vars
As reported in this AWS re:Post article, we need further configuration for use external static resources because:
In Elastic Beanstalk, environment variables aren't exposed to the operating system (OS).
Add .ebextension folder to My application
As suggested in previous article, we have to:
- create a folder .ebextensions
- create a file (i.e. setvars.config) inside folder .ebextension
- put inside file setvars.config instructions for additional configurations for EBS (see article for details)
- put folder .ebextensions in the project root
For a gradle project like this, we follow indications of this StackOverflow Article for a correct deploy of .ebextension folder.
In details, we move .ebextensions folder inside src/main/resources folder.
The content of resources directory is packaged automatically in /BOOT-INF/classes/ (no scripting required).
And the .ebextensions directory will be discovered by EBS deployment scripts when unpacked.
Test env vars values
As reported in this AWS re:Post article, we can view all EBS env vars using this command
/opt/elasticbeanstalk/bin/get-config environment
In details, for testing the correct export process, run the following command:
env | grep TOOLS_STATIC_RES
Problem - The Same Problem
All static resources under classpath are available but
all resources in local file system result in 404 (NOT FOUND) for My application
Yes, I put the files directly in correct path
@giocorito then have you connect to the instance and check the environment variable TOOLS_STATIC_RES is correct or not? Thanks.
@Zen You're great! Doing printenv in EC2 instance, no env var specified in EBS configuration is present. So, does EBS not set correctly environment variables? With command /opt/elasticbeanstalk/bin/get-config environment I see all env vars definied in EBS but not visible as OS env vars