AWS CDK v2 Java maven alpha dependencies - amazon-web-services

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

Related

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.

Enable AWS JAVA JVM Cloudwatch Metrics

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>

AWS SES Service For sending mail using java

I am facing below error while using AWS SES Mail sending example?
"Exception in thread "main" java.lang.NoSuchMethodError: com.amazonaws.client.AwsSyncClientParams.getAdvancedConfig()Lcom/amazonaws/client/builder/AdvancedConfig;
at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.<init>(AmazonSimpleEmailServiceClient.java:277)
at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.<init>(AmazonSimpleEmailServiceClient.java:261)
at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder.build(AmazonSimpleEmailServiceClientBuilder.java:61)
at com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder.build(AmazonSimpleEmailServiceClientBuilder.java:27)
at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
at saurabh.aws.learning.awsLearning.SendMailService.main(SendMailService.java:50)
"
Problem:
I also faced same issue.
The reason was that I was using difference artefact versions for my aws libraries: aws-java-sdk-core and aws-java-sdk-s3.
Solution (Maven):
In case you are using Maven, Amazon suggests to use BOM dependencyManagement. This way you ensure that the modules you specify use the same version of the SDK and that they're compatible with each other.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.522</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
And as you already declared the SDK version in the BOM, you don't need to specify the version number for each component. Like this:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
</dependency>
</dependencies>
Source: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-project-maven.html#configuring-maven-individual-components
I have experienced this error myself. It is caused most likely by different versions of artifacts like: aws-java-sdk-ses and aws-java-sdk-core. Try using the same version for the two above in your pom.xml (if you are using maven). If this doesnt work, can you share your pom.xml ?
Even i had the same issue :
Exception in thread "main" java.lang.NoSuchMethodError:
com.amazonaws.client.AwsSyncClientParams.getAdvancedConfig()Lcom/amazonaws/client/builder/AdvancedConfig;
I tried configuring auto-scaling for my DynamoDB from java i faced this issue.
Solution:
We need to make sure aws sdk version match with the specific service version.
I used below dependency when i got exception,
compile group: 'com.amazonaws', name: 'aws-java-sdk-applicationautoscaling', version: '1.11.500'
compile group: 'com.amazonaws', name: 'aws-java-sdk-core', version: '1.11.123'
After changing to same version, issue got solved
compile group: 'com.amazonaws', name: 'aws-java-sdk-applicationautoscaling', version: '1.11.500'
compile group: 'com.amazonaws', name: 'aws-java-sdk-core', version: '1.11.500'

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