Com.amazonaws.sdkclientexception: failed to connect to service endpoint:

Spring Cloud AWS SQS fails to connect to service endpoint locally

Questions : Spring Cloud AWS SQS fails to connect to service endpoint locally

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00

89

I'm trying to use Spring Cloud's AWS SQS in anycodings_spring-boot a project I'm working on. At this time, I'm anycodings_spring-boot only running the application locally on my anycodings_spring-boot dev machine. Thus, what I want is to connect anycodings_spring-boot to the SQS on AWS without having to deploy anycodings_spring-boot my app to an EC2 instance.

However, it seems like the AWS SDK used in anycodings_spring-boot Spring Cloud's AWS package will attempt to anycodings_spring-boot authenticate through metadata and wants to anycodings_spring-boot resolve anycodings_spring-boot 169.254.169.254/latest/meta-data/instance-id. anycodings_spring-boot Since I'm still the running the app locally, anycodings_spring-boot the endpoint cannot be resolved and an error anycodings_spring-boot is thrown:

2019-12-29 16:38:27.420  WARN 22462 --- [  restartedMain] com.amazonaws.util.EC2MetadataUtils      : Unable to retrieve the requested metadata (/latest/meta-data/instance-id). Failed to connect to service endpoint: 

com.amazonaws.SdkClientException: Failed to connect to service endpoint: 
    at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:100) ~[aws-java-sdk-core-1.11.699.jar:na]
    at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:70) ~[aws-java-sdk-core-1.11.699.jar:na]
    at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:75) ~[aws-java-sdk-core-1.11.699.jar:na]
    at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:62) ~[aws-java-sdk-core-1.11.699.jar:na]
    at com.amazonaws.util.EC2MetadataUtils.getItems(EC2MetadataUtils.java:400) ~[aws-java-sdk-core-1.11.699.jar:na]
    at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:369) ~[aws-java-sdk-core-1.11.699.jar:na]
    at org.springframework.cloud.aws.context.support.env.AwsCloudEnvironmentCheckUtils.isRunningOnCloudEnvironment(AwsCloudEnvironmentCheckUtils.java:38) ~[spring-cloud-aws-context-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.cloud.aws.context.annotation.OnAwsCloudEnvironmentCondition.matches(OnAwsCloudEnvironmentCondition.java:37) ~[spring-cloud-aws-context-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:221) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:587) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
...

I've tried to explicitly supply an SQS anycodings_spring-boot endpoint in my bean, but it still attempts anycodings_spring-boot to connect to 169.254.169.254 resulting in anycodings_spring-boot the error above:

public AmazonSQSAsync sqsClient() {
   EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(
        "sqs.us-east-1.amazonaws.com",
        "us-east-1"
   );

   return AmazonSQSAsyncClientBuilder
                .standard()
                .withEndpointConfiguration(endpointConfig)
                .withCredentials(new AWSStaticCredentialsProvider(new DefaultAWSCredentialsProviderChain().getCredentials()))
                .build();
}

Although I'm not certain about this, I anycodings_spring-boot suspect that the error is occurring because anycodings_spring-boot I'm running the app locally on my dev anycodings_spring-boot machine and so it couldn't resolve to the anycodings_spring-boot endpoint. But I'm not entirely sure about anycodings_spring-boot this too because I'm running other AWS anycodings_spring-boot services in the same app using the AWS SDK anycodings_spring-boot and I don't have this error.

I've the following dependencies in my anycodings_spring-boot pom.xml and it appears that having any one anycodings_spring-boot of them will cause the error to occur. anycodings_spring-boot That's, I don't even have to create the anycodings_spring-boot beans to have that error. Adding these anycodings_spring-boot dependencies will immediately, for some anycodings_spring-boot reason, causes the SDK to attempt to resolve anycodings_spring-boot that endpoint and fail with that error.

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-messaging</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

What else should I do to fix the error that anycodings_spring-boot it failed to connect to service endpoint?

Total Answers 11

24

Answers 1 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

I have received below errors when anycodings_amazon-web-services running Spring Boot Project for AWS SNS

i.InstanceMetadataServiceResourceFetcher : Fail to retrieve token
com.amazonaws.util.EC2MetadataUtils      : Unable to retrieve the requested metadata (/latest/dynamic/instance-identity/document). Failed to connect to service endpoint:


Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stackResourceRegistryFactoryBean' defined in class path resource [org/springframework/cloud/aws/autoconfigure/context/ContextStackAutoConfiguration.class]: Unsatisfied dependency expressed through method 'stackResourceRegistryFactoryBean' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonCloudFormation' defined in class path resource [org/springframework/cloud/aws/autoconfigure/context/ContextStackAutoConfiguration.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

Error creating bean with name 'org.springframework.cloud.aws.core.env.ResourceIdResolver.BEAN_NAME': Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stackResourceRegistryFactoryBean' defined in class path resource [org/springframework/cloud/aws/autoconfigure/context/ContextStackAutoConfiguration.class]: Unsatisfied dependency expressed through method 'stackResourceRegistryFactoryBean' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonCloudFormation' defined in class path resource [org/springframework/cloud/aws/autoconfigure/context/ContextStackAutoConfiguration.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

Resolution applied: Add below code anycodings_amazon-web-services snippet to SpringBootApplication & anycodings_amazon-web-services Click on Run Button in the IDE

@SpringBootApplication(
        exclude = {
                org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration.class,
                org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration.class,
                org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration.class
        }
)

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

mRahman

5

Answers 2 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

I know many people have already anycodings_amazon-web-services explained what's issue and how to skip anycodings_amazon-web-services this error. But, all the above method anycodings_amazon-web-services didn't work for me. Finally after lot of anycodings_amazon-web-services search I set following aws configuration anycodings_amazon-web-services property and it worked for me.

cloud.aws.region.use-default-aws-region-chain=true
cloud.aws.stack.auto=false
cloud.aws.credentials.access-key=
cloud.aws.credentials.secret-key=
cloud.aws.region.auto=false
cloud.aws.region.static=
cloud.aws.stack=false

logging.level.com.amazonaws.util.EC2MetadataUtils=error
logging.level.com.amazonaws.internal.InstanceMetadataServiceResourceFetcher=error

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

jidam

5

Answers 3 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

When using the AWS Java SDK outside of anycodings_amazon-web-services an EC2 instance, you can disable anycodings_amazon-web-services loading credentials or regions from EC2 anycodings_amazon-web-services Metadata instance service with a System anycodings_amazon-web-services property or an Environment variable.

  • System Property : com.amazonaws.sdk.disableEc2Metadata
  • Environment variable : AWS_EC2_METADATA_DISABLED

However, you will still get a warning at anycodings_amazon-web-services application startup, it tells you the anycodings_amazon-web-services EC2Metadata is disabled by configuration anycodings_amazon-web-services : "EC2 Instance Metadata Service is anycodings_amazon-web-services disabled"

2020-03-27 18:10:42.483  WARN 71123 --- [main] com.amazonaws.util.EC2MetadataUtils      : Unable to retrieve the requested metadata (/latest/meta-data/instance-id). EC2 Instance Metadata Service is disabled

com.amazonaws.AmazonClientException: EC2 Instance Metadata Service is disabled
    at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:65)
    at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:66)
    at com.amazonaws.util.EC2MetadataUtils.getItems(EC2MetadataUtils.java:402)
    at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:371)
    at org.springframework.cloud.aws.context.support.env.AwsCloudEnvironmentCheckUtils.isRunningOnCloudEnvironment(AwsCloudEnvironmentCheckUtils.java:38)
    ...

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

miraj

4

Answers 4 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

The following configuration worked for anycodings_amazon-web-services me:

cloud:
  aws:
    region:
      auto: false
      static: eu-west-1
      use-default-aws-region-chain: true
    stack: false
    stack.auto: false
logging:
  level:
    com:
      amazonaws:
        internal:
          InstanceMetadataServiceResourceFetcher: error
        util:
          EC2MetadataUtils: error

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

joy

3

Answers 5 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

Fix the problem adding in the anycodings_amazon-web-services application.yaml file:

logging:
  level:
    com:
      amazonaws:
        util:
          EC2MetadataUtils: error

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

raja

5

Answers 6 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

As far as I understand its trying to anycodings_amazon-web-services check if your application is running on anycodings_amazon-web-services cloud environment, from the 5th last anycodings_amazon-web-services line of the stacktrace

org.springframework.cloud.aws.context.support.env.AwsCloudEnvironmentCheckUtils.isRunningOnCloudEnvironment(AwsCloudEnvironmentCheckUtils.java:38)

I checked out the the repo and here is anycodings_amazon-web-services the code snippet -

public static boolean isRunningOnCloudEnvironment() {
    if (isCloudEnvironment == null) {
        try {
            isCloudEnvironment = EC2MetadataUtils
                    .getData(EC2_METADATA_ROOT + "/instance-id", 1) != null;
        }
        catch (AmazonClientException e) {
            isCloudEnvironment = false;
        }
    }
    return isCloudEnvironment;
}

As per the snippet it tried to fetch anycodings_amazon-web-services instance metadata and if it fails it anycodings_amazon-web-services return false, which means you are not on anycodings_amazon-web-services the Cloud Environment. However this code anycodings_amazon-web-services is expected to Catch anycodings_amazon-web-services AmazonClientException but the stacktrace anycodings_amazon-web-services you shared is throwing anycodings_amazon-web-services SdkClientException exception, which this anycodings_amazon-web-services part of the snippet can't catch and anycodings_amazon-web-services hence can't return false that you are anycodings_amazon-web-services not on a cloud environment.

Seems like some issue with this build of anycodings_amazon-web-services the library, As per the stacktrace you anycodings_amazon-web-services are using 2.2.1.RELEASE1 which was anycodings_amazon-web-services released 9 days back only. Could you anycodings_amazon-web-services please try to use one of the older anycodings_amazon-web-services version and then report if you are still anycodings_amazon-web-services facing the same issue.


Or as a temporary fix you can try anycodings_amazon-web-services tunnelling to your VPC's entry server. anycodings_amazon-web-services You can use sshuttle for tunnelling anycodings_amazon-web-services purpose if you want. This will mostly anycodings_amazon-web-services make that IP reachable from local anycodings_amazon-web-services machine as well.

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

raja

3

Answers 7 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

I was getting same kind of error when anycodings_amazon-web-services running s3 bucket in spring boot i anycodings_amazon-web-services solved using different location used anycodings_amazon-web-services while creating bucket:

   return AmazonS3ClientBuilder
            .standard()
            .withRegion("us-east-1")  //This is the code i added to fix 
            .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
            .build();

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

jidam

3

Answers 8 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

  1. Set cloud.aws.region.auto property to false in Spring application properties (or via environment variables etc.).
  2. Define cloud.aws.region.static property.

This will tell Spring not to use anycodings_amazon-web-services automatic detection based EC2 meta data. anycodings_amazon-web-services Some details.

You may need to configure credentials anycodings_amazon-web-services similarly, you can find related info in anycodings_amazon-web-services the same doc.

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

jidam

2

Answers 9 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

The issue is with the spring-cloud anycodings_amazon-web-services version Hoxton.SR8. Replace your anycodings_amazon-web-services spring-cloud version in your pom.xml anycodings_amazon-web-services with Finchley.SR1.

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

miraj

4

Answers 10 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

I also faced similar error but not from anycodings_amazon-web-services the Spring cloud. When i added below anycodings_amazon-web-services configuration in application.properties, anycodings_amazon-web-services error went away.

logging.level.com.amazonaws.util.EC2MetadataUtils=error logging.level.com.amazonaws.internal.InstanceMetadataServiceResourceFetcher=error

But i'm facing some other issues while anycodings_amazon-web-services opening s3a path. Then i printed the anycodings_amazon-web-services stacktrace of the exception and then i anycodings_amazon-web-services got below error

Caused by: anycodings_amazon-web-services com.amazonaws.SdkClientException: Unable anycodings_amazon-web-services to load AWS credentials from any anycodings_amazon-web-services provider in the chain: anycodings_amazon-web-services [BasicAWSCredentialsProvider: Access key anycodings_amazon-web-services or secret key is null, anycodings_amazon-web-services WebIdentityTokenCredentialsProvider: You anycodings_amazon-web-services must specify a value for roleArn and anycodings_amazon-web-services roleSessionName, anycodings_amazon-web-services EnvironmentVariableCredentialsProvider: anycodings_amazon-web-services Unable to load AWS credentials from anycodings_amazon-web-services environment variables (AWS_ACCESS_KEY_ID anycodings_amazon-web-services (or AWS_ACCESS_KEY) and AWS_SECRET_KEY anycodings_amazon-web-services (or AWS_SECRET_ACCESS_KEY)), anycodings_amazon-web-services com.amazonaws.auth.InstanceProfileCredentialsProvider@7700439a: anycodings_amazon-web-services Failed to connect to service endpoint: ]

Already credentials are there /.aws anycodings_amazon-web-services folder but not sure why application anycodings_amazon-web-services still giving the error. Fixed by anycodings_amazon-web-services creating environment variables anycodings_amazon-web-services AWS_ACCESS_KEY_ID, AWS_SECRET_KEY and anycodings_amazon-web-services AWS_SESSION_TOKEN

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

miraj

6

Answers 11 : of Spring Cloud AWS SQS fails to connect to service endpoint locally

This was coming from an internal anycodings_amazon-web-services dependency at my company, so I couldn't anycodings_amazon-web-services get any of the previously posted anycodings_amazon-web-services solutions to work. What worked for me anycodings_amazon-web-services was to simply set in my environment anycodings_amazon-web-services variables AWS_REGION = 'us-east-1'.

0

2022-09-16T02:42:31+00:00 2022-09-16T02:42:31+00:00Answer Link

joy