0

Problem Statement
AWS 'Fargate' application works fine with aws actuator but fails to start when adding s3 support

Details
I have an application running on AWS Fargate, it is using spring-cloud-aws-actuator & spring-cloud-starter-aws packages.
The application runs fine when running in "fargate" and locally (using management.metrics.export.cloudwatch.enabled=false). I recently added support to read from an S3 bucket, testing locally this code ran fine but when I deploy it to "fargate" the application fails to start with the following error .

Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'amazonCloudWatchAsync' defined in class path resource [org/springframework/cloud/aws/autoconfigure/metrics/CloudWatchExportAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException:
There is no EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance

Since this worked before I added the S3 code I can only surmise the problem is related to new code, but I don't really know where to begin to t-shoot this.

What I did
In the program I added code to check if I'm running locally or in AWS. If I am running locally I create a AmazonS3 object using the ProfileCredentialsProvider, for example

           // running locally, create our own credentials
           AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                    .withCredentials(new ProfileCredentialsProvider("myprofile"))
                    .withRegion(Regions.US_EAST_1)
                    .build();

When I detect I'm on AWS (using Spring profiles) I do the same as above except I use DefaultAWSCredentialsProviderChain, perhaps this is the issue? But I'm not sure how to get the AmazonS3 object autowired from spring aws when running in "Fargate" but not when local.

Suggestions?
I know the above is a bit vague, I'm happy to provide more details if you let me know what would help. That being said I'm looking for a way to run my app locally (for testing) which allows me to access the S3 bucket and disables the cloudwatch actuator code, but then allows both to be used when running in a "Fargate" environment.

Jim M.
  • 909
  • 1
  • 12
  • 29

1 Answers1

0

With the help of a co-worker we found a way around this, I'm not sure it is 100% correct but it works for me and is what I have decided to do. In my applictaion.properties I added the following lines

cloud.aws.region.static=us-east-1
cloud.aws.stack.auto=false

Setting the static region doesn't seem correct, but evidently the Spring CloudWatch code isn't detecting that it is running in the ECS Fargate environment so it was the only thing we could see to do.

Jim M.
  • 909
  • 1
  • 12
  • 29