0

[Service class logic]

@Autowired
private static AWSCostExplorer awsCostExplorerClient;

final String serviceEndpoint = "https://ce.us-east-1.amazonaws.com";

final String signingRegion = "us-east-1";

 public GetCostAndUsageRequest costUsage() {
     AWSCostExplorerClientBuilder builder = AWSCostExplorerClientBuilder.standard();
     awsCostExplorerClient = builder.withCredentials(new AWSStaticCredentialsProvider(new ProfileCredentialsProvider("default").getCredentials()))
                .withRegion(Regions.US_EAST_1).withEndpointConfiguration(new EndpointConfiguration(serviceEndpoint, signingRegion)).build();

        GetCostAndUsageRequest request = new GetCostAndUsageRequest()
                .withTimePeriod(new DateInterval().withStart("02-13-2019").withEnd("02-19-2019"))
                .withGranularity("DAILY")
                .withMetrics("BlendedCost");

        GetCostAndUsageResult result = awsCostExplorerClient.getCostAndUsage(request);

        result.getResultsByTime().forEach(resultByTime -> {
            System.out.println(resultByTime.toString());
        });

        awsCostExplorerClient.shutdown();
        return request;
 }

When after doing maven install, I'am getting this exception

[Exception]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3': 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
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:830) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127) [spring-boot-test-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365) [surefire-junit4-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273) [surefire-junit4-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238) [surefire-junit4-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159) [surefire-junit4-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) [surefire-booter-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) [surefire-booter-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) [surefire-booter-2.22.1.jar:2.22.1]
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) [surefire-booter-2.22.1.jar:2.22.1]

Can somebody help me

gdlmx
  • 6,479
  • 1
  • 21
  • 39
  • Possible duplicate of https://stackoverflow.com/questions/45818092/spring-boot-startup-error-for-aws-application-there-is-not-ec2-meta-data-avail – racraman Feb 22 '19 at 06:10
  • Hi @racraman thanks for the response but the link what you posted here, I'am unable to understand my task is different from spring-cloud-aws-messaging. – Aarya Linga Reddy Feb 22 '19 at 06:33
  • Put this line to your application.properties; cloud.aws.region.static=us-east-1 – Habil Feb 22 '19 at 07:19

1 Answers1

0

According to the error:

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

you're running the code outside of AWS, but the code expects to be running inside AWS on an EC2 instance to get to the EC2 metadata that is attached to such an instance.

Sorry for the second part of my first answer, if you saw that. I looked more closely and saw that it is in building with Maven that this happens, and it's happening when running the tests associated with the code. You probably have to disable those tests since you're building on a non-EC2 machine. The code should run fine once you get it onto EC2. I do find it a bit strange that the test code assumes you're building on an EC2 instance, but that's what seems to be going on.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Steve! Can you help where I'am going wrong? give me your mail-id so that I can explain clearly my code and task. – Aarya Linga Reddy Feb 22 '19 at 06:17
  • Just talk here. Others may know more than I do. We can turn this into a Chat to have plenty of room to talk. So does what I say make sense? Are you building something on a local workstation that you expect to have run on an EC2 instance? – CryptoFool Feb 22 '19 at 06:20
  • Looking a little at your code, I'm surprised that it is trying to find EC2 metadata. It looks to me like code you'd expect to run outside of AWS. I use AWS a lot, and am familiar with using other APIs, like S3, RDS, etc. But I'm not familiar with what you're doing here. – CryptoFool Feb 22 '19 at 06:22
  • Ok sure! Yeah there are nothing running on EC2 instance. What I'am looking for is, I have an AWS account. So I want to get the bill for my account using the Java code. In the aws account -> cost explorer my cost is 0.0 $ . So I want that cost as my response data in Java client application. – Aarya Linga Reddy Feb 22 '19 at 06:24
  • Did you see the link that @racraman gave in a comment attached to your original question? I took a look at that. That looks promising if you want to get this code to run. If you're asking for another way to do it, I can't really provide any suggestions. - It looks like maybe it's a more generic problem with AWS APIs, although I've never run into it. – CryptoFool Feb 22 '19 at 06:25
  • And by the way Steve., Ia'm new to AWS usage, I'am not familiar using AWS and AWS SDK for Java. Blindly I'am using the Java SDK which was provided by AWS ppl. I'am taking the references from some of the site's to how to develop this PoC. – Aarya Linga Reddy Feb 22 '19 at 06:27
  • Yes @racraman posted a link but I'am unable to relate my task and that task. that link is all about spring-cloud-aws-messaging. – Aarya Linga Reddy Feb 22 '19 at 06:32
  • I see your dilemma. I really like helping others when I can. I don't have more time right now, but I'd be open to helping you more with it when I have more time...assuming you don't figure it out before then. – CryptoFool Feb 22 '19 at 06:34
  • I guess there's no notion of PM (private message) on SO, so I don't know how I'd give you my email address without giving to everyone. Maybe I don't need to worry about that, but then neither should you. If you want to post your email address here (suggest obfuscating it so robots won't pick it up), I'll respond so you have my email too. – CryptoFool Feb 22 '19 at 06:42
  • Finally I got my program successfully executed and got the result Mr. Steve. Thanks to you and @racraman – Aarya Linga Reddy Feb 22 '19 at 08:21