Skip to content

ElasticBeanstalk SpringBoot Application - use local folder for static resources

0

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:

  1. create a folder .ebextensions
  2. create a file (i.e. setvars.config) inside folder .ebextension
  3. put inside file setvars.config instructions for additional configurations for EBS (see article for details)
  4. 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

asked 2 years ago480 views

1 Answer
1

Hello, have you try to connect to the instance to ensure the files are existed in correct path? https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.ec2connect.html

answered 2 years ago

  • 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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.