'Spring-Boot can't connect programmatically to AWS SSM locally
I'm using:
- Open JDK 11
- Spring boot 2.6.6
- MacBook (my local dev environment)
I can connect to AWS from the command line (java -jar app.war) if I do the following:
export [email protected]
export AWS_ACCESS_KEY_ID=AccessKeyHere
# both AWS_SECRET_KEY and AWS_SECRET_ACCESS_KEY work, so I'm leaving both
export AWS_SECRET_KEY=SecretKeyHere
export AWS_SECRET_ACCESS_KEY=SecretKeyHereIwasTryingAbunchOfDifferentOnesSoOneExportIsNotNeeded
export AWS_DEFAULT_REGION=us-east-1
But, it fails if I only do it via the application.properties such as (unset all the exports above):
spring.config.import=aws-parameterstore:
aws.paramstore.enabled=true
aws.paramstore.prefix=/config
aws.paramstore.defaultContext=contextNameHere
aws.paramstore.profileSeparator=_
# ################################################# #
# Locally, without this line, I get this error:
# Caused by: com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
# ################################################# #
aws.paramstore.region=us-east-1
cloud.aws.stack.auto=false
cloud.aws.region.auto=false
cloud.aws.region.static=us-east-1
#
# I'll leave these here for now as I think these might be required when running on AWS/EC2
#
[email protected]
cloud.aws.credentials.access-key=AccessKeyHere
cloud.aws.credentials.secret-key=SecretKeyHere
I believe I have the correct config names (listed here https://docs.spring.io/spring-cloud-aws/docs/2.2.3.RELEASE/reference/html/appendix.html), but initially I had old AWS values from a training class higher up in my .bashrc so things kept failing and it didn't make sense from the docs I was reading.
When I finally figured out the old values where higher up in the .bashrc, removed and then reset everything, things started working, but at that point I had been throwing things at the wall hoping something would stick. While I have done quite a bit of cleanup, I do have more to do; but, with the exports everything works well, without, it fails with this error:
Caused by: com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: [EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)), SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey), WebIdentityTokenCredentialsProvider: To use assume role profiles the aws-java-sdk-sts module must be on the class path., com.amazonaws.auth.profile.ProfileCredentialsProvider@3eeb318f: No AWS profile named 'default', com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@52045dbe: Failed to connect to service endpoint: ]
NOTE: whether I set the export's or unset them, I never change the application.properties, so it's the export's that do the trick of connecting to AWS SSM.
Now, if this isn't needed running on an EC2 instance (our TechOps current plan), I'll just update the developer guides within confluence to include those exports (or system variables for windows developers), but being able to do everything within the application.properties would be the easiest, most preferable route.
What i believe is the relevant parts of the 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>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<properties>
<log4j.version>2.4</log4j.version>
<org.slf4j-version>1.7.10</org.slf4j-version>
<java.version>11</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- ********************************** -->
<!-- Needed for SSM -->
<!-- ********************************** -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
<version>2.3.0</version>
</dependency>
...
</dependencies>
...
</project>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
