Search code examples
amazon-web-servicesspring-bootdockerterraformamazon-ecs

How to use environment variables for a docker image in AWS-ECR?


I made a docker image from a Spring-Boot-App, which has application.properties . One property for example is user.name=xxx. Now I uploaded the image to a AWS-ECR repository. Is there a way to inject this variable through a ECS-Task or maybe a other way ? For Deployment I use Terraform.


Solution

  • Spring Boot allows you to set configuration by a huge number of different ways:

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

    1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
    2. @TestPropertySource annotations on your tests.
    3. @SpringBootTest#properties annotation attribute on your tests.
    4. Command line arguments.
    5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
    6. ServletConfig init parameters.
    7. ServletContext init parameters.
    8. JNDI attributes from java:comp/env.
    9. Java System properties (System.getProperties()).
    10. OS environment variables.
    11. A RandomValuePropertySource that only has properties in random.*.
    12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
    13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
    14. Application properties outside of your packaged jar (application.properties and YAML variants).
    15. Application properties packaged inside your jar (application.properties and YAML variants).
    16. @PropertySource annotations on your @Configuration classes.
    17. Default properties (specified using SpringApplication.setDefaultProperties).

    The simplest thing then is to set environment variables for the ECS container definition so they get injected into the task at runtime. This will also override any properties set in application.properties due to the ordering of them.

    A very basic example of the task definition in Terraform would look something like this:

    resource "aws_ecs_task_definition" "example" {
      family                = "example"
      container_definitions = <<TASK_DEFINITION
    [
        {
            "cpu": 10,
            "command": ["sleep", "10"],
            "entryPoint": ["/"],
            "environment": [
                {
                    "name": "USER_NAME"
                    "value": "xxx"
                }
            ],
            "essential": true,
            "image": "example",
            "memory": 128,
            "name": "example",
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 8080
                }
            ]
        }
    ]
    TASK_DEFINITION
    
    }