Skip to content

Commit

Permalink
Refactored to two seaparate module - lambda and cd stack
Browse files Browse the repository at this point in the history
  • Loading branch information
djiten75 authored and djiten75 committed Nov 29, 2019
1 parent 716ad26 commit 7b1a3df
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 93 deletions.
6 changes: 4 additions & 2 deletions java/api-cors-lambda-crud-dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ This is an example of a CDK program written in Java.

## Building

To build this app, run `mvn package`. This will download the required
dependencies to compile the Java code and create jar for associated lambda code.
To build this app, run `mvn compile`. This will download the required
dependencies to compile the Java code and create jar for associated software.amazon.awscdk.examples.lambda code.

You can use your IDE to write code and unit tests, but you will need to use the
CDK toolkit if you wish to synthesize/deploy stacks.

If you need to modify the lambda code then do mvn package and copy the
jar file to lib directory in the parent module
## CDK Toolkit

The [`cdk.json`](./cdk.json) file in the root of this repository includes
Expand Down
2 changes: 1 addition & 1 deletion java/api-cors-lambda-crud-dynamodb/cdk.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.CorsLambdaCrudDynamodbApp"
"app": "mvn exec:java -pl cdk -Dexec.mainClass=software.amazon.awscdk.examples.CorsLambdaCrudDynamodbApp"
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public CorsLambdaCrudDynamodbStack(final Construct parent, final String name) {


Function getOneItemFunction = new Function(this, "getOneItemFunction",
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.lambda.GetOneItem"));
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.software.amazon.awscdk.examples.lambda.GetOneItem"));
Function getAllItemsFunction = new Function(this, "getAllItemsFunction",
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.lambda.GetAllItems"));
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.software.amazon.awscdk.examples.lambda.GetAllItems"));
Function createItemFunction = new Function(this, "createItemFunction",
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.lambda.CreateItem"));
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.software.amazon.awscdk.examples.lambda.CreateItem"));
Function updateItemFunction = new Function(this, "updateItemFunction",
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.lambda.UpdateItem"));
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.software.amazon.awscdk.examples.lambda.UpdateItem"));
Function deleteItemFunction = new Function(this, "deleteItemFunction",
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.lambda.DeleteItem"));
getLambdaFunctionProps(lambdaEnvMap, "software.amazon.awscdk.examples.software.amazon.awscdk.examples.lambda.DeleteItem"));



Expand Down Expand Up @@ -136,7 +136,7 @@ private void addCorsOptions(IResource item) {

private FunctionProps getLambdaFunctionProps(Map<String, String> lambdaEnvMap, String handler) {
return FunctionProps.builder()
.code(Code.fromAsset("target/cors-lambda-crud-dynamodb-1.0.0-jar-with-dependencies.jar"))
.code(Code.fromAsset("./lib/lambda-1.0.0-jar-with-dependencies.jar"))
.handler(handler)
.runtime(Runtime.JAVA_8)
.environment(lambdaEnvMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CreateItem implements RequestHandler<Map<String,Object>, GatewayRes
@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Inside lambda: getOneItem "+input.getClass()+ " data:"+input);
logger.log("Inside software.amazon.awscdk.examples.lambda: getOneItem "+input.getClass()+ " data:"+input);


String body = (String)input.get("body");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DeleteItem implements RequestHandler<Map<String,Object>, GatewayRes
@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Inside lambda: getOneItem "+input.getClass()+ " data:"+input);
logger.log("Inside software.amazon.awscdk.examples.lambda: getOneItem "+input.getClass()+ " data:"+input);
Map<String, Object> pathParameters = (Map<String, Object>)input.get("pathParameters");

Map<String, String> headers = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GetAllItems implements RequestHandler<Object, GatewayResponse>{
@Override
public GatewayResponse handleRequest(Object input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Inside lambda: getAllItems ");
logger.log("Inside software.amazon.awscdk.examples.lambda: getAllItems ");

String output = getData(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GetOneItem implements RequestHandler<Map<String,Object>, GatewayRes
@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Inside lambda: getOneItem "+input.getClass()+ " data:"+input);
logger.log("Inside software.amazon.awscdk.examples.lambda: getOneItem "+input.getClass()+ " data:"+input);

Map<String, Object> pathParameters = (Map<String, Object>)input.get("pathParameters");
String id=(String)pathParameters.get("id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UpdateItem implements RequestHandler<Map<String,Object>, GatewayRes
@Override
public GatewayResponse handleRequest(Map<String, Object> input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Inside lambda: getOneItem "+input.getClass()+ " data:"+input);
logger.log("Inside software.amazon.awscdk.examples.lambda: getOneItem "+input.getClass()+ " data:"+input);
Map<String, Object> pathParameters = (Map<String, Object>)input.get("pathParameters");
String id=(String)pathParameters.get("id");

Expand Down
83 changes: 8 additions & 75 deletions java/api-cors-lambda-crud-dynamodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

<groupId>com.amazonaws.cdk</groupId>
<artifactId>cors-lambda-crud-dynamodb</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>lambda</module>
<module>cdk</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -33,14 +38,14 @@
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.amazonaws.cdk.examples.CorsLambdaCrudDynamodbStack</mainClass>
<mainClass>com.amazonaws.cdk.examples.CorsLambdaCrudDynamodbApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
Expand All @@ -51,77 +56,5 @@
</plugins>
</build>

<dependencies>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>core</artifactId>
<version>[1.17.0.DEVPREVIEW, 2)</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigateway</artifactId>
<version>[1.17.0.DEVPREVIEW, 2)</version>
</dependency>

<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>dynamodb</artifactId>
<version>[1.17.0.DEVPREVIEW, 2)</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>lambda</artifactId>
<version>[1.17.0.DEVPREVIEW, 2)</version>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.10.24</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class LambdaCronApp {
public static void main(final String[] args) {
App app = new App();

new LambdaCronStack(app, "cdk-lambda-cron-example");
new LambdaCronStack(app, "cdk-software.amazon.awscdk.examples.lambda-cron-example");

app.synth();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LambdaCronStack extends Stack {
public LambdaCronStack(final Construct parent, final String name) {
super(parent, name);

SingletonFunction lambdaFunction = SingletonFunction.Builder.create(this, "cdk-lambda-cron")
SingletonFunction lambdaFunction = SingletonFunction.Builder.create(this, "cdk-software.amazon.awscdk.examples.lambda-cron")
.description("Lambda which prints \"I'm running\"")
.code(Code.fromInline(
"def main(event, context):\n" +
Expand All @@ -31,7 +31,7 @@ public LambdaCronStack(final Construct parent, final String name) {
.uuid(UUID.randomUUID().toString())
.build();

Rule rule = Rule.Builder.create(this, "cdk-lambda-cron-rule")
Rule rule = Rule.Builder.create(this, "cdk-software.amazon.awscdk.examples.lambda-cron-rule")
.description("Run every day at 6PM UTC")
.schedule(Schedule.expression("cron(0 18 ? * MON-FRI *)"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class LambdaCronStackTest {
@Before
public void setUp() throws IOException {
App app = new App();
Stack stack = new LambdaCronStack(app, "lambdaResource-cdk-lambda-cron");
Stack stack = new LambdaCronStack(app, "lambdaResource-cdk-software.amazon.awscdk.examples.lambda-cron");
actualStack = toCloudFormationJson(stack).path("Resources");
expectedStack = TestUtils.fromFileResource(getClass().getResource("testCronLambdaExpected.json")).path("Resources");
}
Expand Down

0 comments on commit 7b1a3df

Please sign in to comment.