-1

I'm attempting to read a parameter from aws parameter store in my spring boot application. I succeeded on my local machine but I want to make this function without having to supply my access-key and secret-key. I want to build a image, push it to ECR and create a task using aws fargate. It works on AWS but only with my secret-key and access-key and I receive an error when I try to build the image without my credentials on application.yml. Anyone could help me with this?

I created this 2 parameters, depending on the profile which is dev on the application.yml that I'm posting here. /config/application_dev/db.username /config/application_prod/db.username

When I comment access-key and secret-key on my application.yml I get this error bellow.

Unable to load AWS parameter from /config/application_dev/. Profile file contained no credentials for profile 'default': ProfileFile(profilesAndSectionsMap=[])

I think the problem exists because spring is not capable to pick values from my credentials file on ~/.aws/credentials. I also set the values as environment variables(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY) and it didn't work.

application.yml as follows


db:
  username: usuariolocal
spring:
  profiles:
    active: dev
  application:
    name: demoweb
  # AWS parameter store configuration
  cloud:
    aws:
      credentials:
        instanceProfile: false
        use-default-aws-credentials-chain: true

      region:
        static: us-east-2
  config:
    import:
      - optional:aws-parameterstore:/config/application_${spring.profiles.active}/

logging:
  level:
    io.awspring.cloud.parameterstore: DEBUG
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demoweb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoweb</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-cloud-aws.version>3.0.1</spring-cloud-aws.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-aws -->
        <dependency>
            <groupId>io.awspring.cloud</groupId>
            <artifactId>spring-cloud-aws-starter-parameter-store</artifactId>
        </dependency>

        <!--dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>ssm</artifactId>
        </dependency-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-docker-compose</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.awspring.cloud</groupId>
                <artifactId>spring-cloud-aws-dependencies</artifactId>
                <version>${spring-cloud-aws.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

compose.yml

services:
  app:
    image: demoweb
    build:
      context: .
    ports:
      - 8087:8080
    volumes:
      - '${USERPROFILE}/.aws:/root/.aws'
    environment:
      - AWS_REGION=us-east-2
      - AWS_PROFILE=default

Dockerfile

# Use uma imagem base oficial do OpenJDK
FROM  maven:latest as builder

# Defina o diretório de trabalho
WORKDIR /app

COPY pom.xml .
COPY src ./src

RUN mvn clean package

FROM  eclipse-temurin:21-jre-jammy

COPY --from=builder /app/target/demoweb-0.0.1-SNAPSHOT.jar app.jar

ENTRYPOINT ["java","-jar","/app.jar"]

Controller

package com.example.demoweb;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teste")
public class TesteController {

    @Value("${db.username}")
    private String test;

    @GetMapping(path = "/add")
    public @ResponseBody String addNewUser() {
        System.out.println("hi"+test);
        return "Registro adicionado com sucesso!";
    }

}
3
  • Could you provide the error? Commented Nov 1 at 16:00
  • @Max I have updated the code adding optional on the config.import with optional:aws-parameterstore and commented access-key and secret-key and now I'm getting this error "Unable to load AWS parameter from /config/application_dev/. Profile file contained no credentials for profile 'default': ProfileFile(profilesAndSectionsMap=[])". My ~/.aws/credentials file is configured correctly in my windows. Commented Nov 1 at 18:41
  • I solved creating a volume on compose.yml with ${USERPROFILE}/.aws:/root/.aws which pointed to the correct aws credentials on my local environment. Commented Nov 1 at 22:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.