I need to create a sample lambda function in AWS that is capable on starting or stopping a EC2 instance on call. The first thing that I tried was to create a jar from the project using the following pom
<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>Ec2Ops</groupId>
<artifactId>Ec2Ops</artifactId>
<version>0.0.1</version>
<name>Ec2Ops</name>
<description>Application to perform EC2Operations</description>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.136</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I uploaded the jar file I got using this pom into my lambda function; there I'm getting the following error in return
{
"errorMessage": "com/amazonaws/services/ec2/AmazonEC2",
"errorType": "java.lang.NoClassDefFoundError",
"stackTrace": [
"com.ec2ops.handler.Ec2OpsHandler.handleRequest(Ec2OpsHandler.java:14)",
"com.ec2ops.handler.Ec2OpsHandler.handleRequest(Ec2OpsHandler.java:9)"
],
"cause": {
"errorMessage": "com.amazonaws.services.ec2.AmazonEC2",
"errorType": "java.lang.ClassNotFoundException",
"stackTrace": [
"java.net.URLClassLoader.findClass(URLClassLoader.java:381)",
"java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
"java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
"com.ec2ops.handler.Ec2OpsHandler.handleRequest(Ec2OpsHandler.java:14)",
"com.ec2ops.handler.Ec2OpsHandler.handleRequest(Ec2OpsHandler.java:9)"
]
}
}
Please note I'm using AWS Java SDK to manipulate the instance. I understood from the error that the library is not available; so I tried to include the library also into the jar by using the shade plugin.
<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>
When I'm doing this that size of the jar file is more than 50MB, hence I cannot upload this into the jar file. The code I'm using for the EC2 operation is given below.
public boolean startInstance(String instanceId) {
AmazonEC2 ec2 = new AmazonEC2Client();
StartInstancesRequest startInstanceRequest = new StartInstancesRequest()
.withInstanceIds(instanceId);
StartInstancesResult instancesResult = ec2
.startInstances(startInstanceRequest);
List<InstanceStateChange> stateChangeList = instancesResult
.getStartingInstances();
return checkStateOfInstance(stateChangeList, 16, instanceId) || checkStateOfInstance(stateChangeList, 80, instanceId);
}
Can someone tell what I'm doing wrong here.