Enable AWS JAVA JVM Cloudwatch Metrics - amazon-web-services

I'm trying to follow the instructions here.
I've added -Dcom.amazonaws.sdk.enableDefaultMetrics=cloudwatchRegion=eu-west-2 to the java options. I also added the following dependencies in the maven pom:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudwatch</artifactId>
<version>1.11.557</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.557</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.557</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudwatchmetrics</artifactId>
<version>1.11.557</version>
<scope>provided</scope>
</dependency>
The docker container runs fine on an ec2 instance with the correct iam role and policies. However, there are no CloudWatch namespaces for AWS SDK/Java as the documentation suggests.

This uses another dependency, change the aws-java-sdk-cloudwatch to aws-java-sdk-cloudwatchmetrics.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudwatchmetrics</artifactId>
<version>1.11.557</version>
<scope>provided</scope>
</dependency>

Related

AWS CDK v2 Java maven alpha dependencies

I started out with the CDK generated dependencies
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>aws-cdk-lib</artifactId>
<version>${cdk.version}</version>
</dependency>
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>${constructs.version}</version>
</dependency>
But those don't resolve e.g. the HttpApi from API Gateway v2(?)
HttpApi httpApi = HttpApi.Builder.create(this, "MyApi").build();
so I added (since it's apparently in alpha on the CDK level?)
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-alpha</artifactId>
<version>2.24.1-alpha.0</version>
</dependency>
and the next stop was LambdaProxyIntegration
Integration lambdaIntegration = LambdaProxyIntegration.Builder.create().handler(lambda).build();
so I tried adding
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-integrations-alpha</artifactId>
<version>2.65.0-alpha.0</version>
</dependency>
and
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.10.0</version>
</dependency>
but no go. Is there some better way of hunting down these maven dependencies that googling around. Would the LambdaProxyIntegration class be in some lambda dependency? Or in some lambda-alpha?
Thanks in advance,
Nik

Alternative of spring-cloud-starter-aws maven dependency for Using AWS SQS

For my project I will be just using #SQSListener annotation I have added below 2 maven dependencies in my springboot pom.xm
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-messaging</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
I understand that we have to use "spring-cloud-aws-messaging" maven dependency but is there any alternative for "spring-cloud-starter-aws dependency" as it adds all the dependencies which are not necessary. I just wants to have dependency related to aws SQS. Please let me know if you have any solution to this.

Not able to run Jmeter Performance Test directly using the JAR created from MAVEN

I have been trying to run one Jmeter script through MAVEN, I was able to do that using command
"mvn verify" and able to see the reports also inside /target folder
However, I want to run the Jmeter Performance Test directly through the JAR which is created after running the "mvn verify" command.
Below is my POM.xml
<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
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>jmeter-testproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jmeter-testproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
A /target folder is being generated after running "mvn verify" and also jmeter-testproject-1.0-SNAPSHOT created.
However, if I want to run the package by directly executing the JAR, it's giving just "Hello World" as an output which is present in the main class. I want the Jmeter Performance Test also to execute after directly clicking the JAR. Please help.
enter image description here
You can't do this with the jmeter-maven-plugin.
The jmeter-maven-plugin is taking your test plan/configuration and then running a JMeter process for you.
It is not physically building a runnable jar with everything that you need inside it to be able to run performance tests.

AP_SOUTH_1 region is not present in Regions enum

I am trying a small dynamoDB example. Because regions for my dynamoDB tables is Mumbai(ap-south-1), I am setting client as following:
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
this.dynamoDb = new DynamoDB(client);
Unfortunately, AP_SOUTH_1 is not getting resolved.
My pom file looks like below:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.10.1</version>
</dependency>
</dependencies>
What am I missing?
It is possible your SDK is not the latest and has no enum for AP_SOUTH_1. Either get the latest SDK or use:
Region.getRegion("ap-south-1")
From: AWS Region Selection
If the region you are attempting to use isn't in the Regions enum, you
can set the region using a string that represents the name of the
region.

AWS Lambda NoClassDefFoundError

I am having difficulty with a Java based Lambda function setup to receive messages from SNS. My function looks like the below:
package com.mycompany;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
public class LambdaHandler {
public void Handler(SNSEvent event, Context context) {
//Process the event
}
}
It compiles just fine and I don't have any problems uploading the jar file to Lambda (via the web console).
However, when I publish to it (via SNS through to the subscribed Lambda function) with JSON representing the SNSEvent model, the Lambda function throws the following exception:
Error loading method handler on class com.mycompany.LambdaHandler:
class java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError:
com/amazonaws/services/lambda/runtime/events/SNSEvent at
java.lang.Class.getDeclaredMethods0(Native Method) at
java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at
java.lang.Class.privateGetPublicMethods(Class.java:2902) at
java.lang.Class.getMethods(Class.java:1615) Caused by:
java.lang.ClassNotFoundException:
com.amazonaws.services.lambda.runtime.events.SNSEvent at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357)
I use Maven + Netbeans and it's a Maven Java Application project. I downloaded the function from the Lambda console and confirmed, the jar has a lib/ directory with all of the jar's for the imports, including aws-lambda-java-events-1.1.0.jar, which itself includes the /com/amazonaws/services/lambda/runtime/events/SNSEvent.class file.
Why is the runtime unable to find the class when it's definitely in the jar file? Is there anything else I need to do, set any environment variables, etc?
Any help would be appreciated!
EDIT 1
I tried downgrading to aws-lambda-java-events 1.0.0 and it's still reporting the same exception. As requested, below is my POM file (with just project name changed). I don't know how to tell Maven to put the libraries in a tree structure.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.app</groupId>
<artifactId>Handler</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-lambda</artifactId>
<version>1.10.6</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Use the maven-shade plugin so that the JAR contains the dependencies in an uber-jar.
So, add this to your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Source: http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html
Potentially you may have this issue https://github.com/aws/aws-lambda-java-libs/issues/2 which requires a downgrade to aws-lambda-java-events-1.0.0.jar
=== If this issue exists even after including shaded jar ===
If you have this issue even after having shaded jar then the issue should be related to aws-lambda-java-events package version (should be some incompatibility between AWS lamda version and newer aws-lambda-java-events version) . i.e. I had this issue with latest version (2.0.2) of aws-lambda-java-events package and I have to downgrade the version to 1.3.0.
Seems like newer aws-lambda-java-events version doesn't have many dependencies.
Sometimes you have to upload your lambda again. Also I got the same issue I fixed with this pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.83</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In the plugins section of your pom.xml, add the Apache Maven Shade Plugin.It is used during the build process. This plugin is used for packaging jars to create a standalone .jar.The maven-shade-plugin will take artifacts (jars) produced by the package goal , and created a standalone .jar that contains the compiled code, and the resolved dependencies from the pom.xml.
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
If you have <scope>provided</scope>
in aws-lambda-java-events artifact, remove it.
Whenever We tried to upload java based Jar or Zip into AWS lambda Console we have to take care of some basic things like,
The Code URI, Which is present in SAM Template or the template.yml file.
eg:
Eg: runwayDetails - package Name
App - class name
handleRequest - lambda handler method.
Syntax should be like this - packageName.className::methodName
It will solve the the error
upload the function.zip instead of jar to lambda