1
1
package cdk ;
2
2
3
3
4
- import software .amazon .awscdk .services .lambda .Code ;
5
- import software .amazon .awscdk .services .lambda .Function ;
6
- import software .constructs .Construct ;
7
- import software .amazon .awscdk .Stack ;
8
- import software .amazon .awscdk .StackProps ;
9
- // import software.amazon.awscdk.Duration;
10
- // import software.amazon.awscdk.services.sqs.Queue;
11
- import software .amazon .awscdk .App ;
12
4
import software .amazon .awscdk .BundlingOptions ;
13
- import software .amazon .awscdk .CfnOutput ;
14
- import software .amazon .awscdk .CfnOutputProps ;
15
- import software .constructs .Construct ;
16
- import software .amazon .awscdk .DockerVolume ;
17
5
import software .amazon .awscdk .Duration ;
18
6
import software .amazon .awscdk .Stack ;
19
7
import software .amazon .awscdk .StackProps ;
8
+ import software .amazon .awscdk .services .apigateway .LambdaIntegration ;
9
+ import software .amazon .awscdk .services .apigateway .RestApi ;
20
10
import software .amazon .awscdk .services .lambda .Code ;
21
11
import software .amazon .awscdk .services .lambda .Function ;
22
- import software .amazon .awscdk .services .lambda .FunctionProps ;
23
12
import software .amazon .awscdk .services .lambda .Runtime ;
24
- import software .amazon .awscdk .services .logs . RetentionDays ;
13
+ import software .amazon .awscdk .services .lambda . Tracing ;
25
14
import software .amazon .awscdk .services .s3 .assets .AssetOptions ;
15
+ import software .constructs .Construct ;
16
+
17
+ import java .util .Arrays ;
18
+ import java .util .List ;
26
19
27
20
public class CdkStack extends Stack {
28
21
public CdkStack (final Construct scope , final String id ) {
@@ -34,11 +27,35 @@ public CdkStack(final Construct scope, final String id, final StackProps props)
34
27
35
28
// The code that defines your stack goes here
36
29
37
- final Function hello = Function .Builder .create (this , "HelloWorldFunction" )
30
+ // CDK will use this command to package your Java Lambda
31
+ List <String > functionPackageInstructions = Arrays .asList (
32
+ "/bin/sh" ,
33
+ "-c" ,
34
+ "mvn package " +
35
+ "&& cp /asset-input/target/powertools-examples-core-cdk-1.16.1.jar /asset-output/"
36
+ );
37
+
38
+ final Function helloWorldFunction = Function .Builder .create (this , "HelloWorldFunction" )
38
39
.runtime (Runtime .JAVA_11 ) // execution environment
39
- .code (Code .fromAsset ("../helloworld/" , AssetOptions .builder ()
40
- .build ())) // code loaded from the "lambda" directory
41
- .handler ("helloworld.App::handleRequest" ) // file is "hello", function is "handler"
40
+ .memorySize (512 )
41
+ .timeout (Duration .seconds (20 ))
42
+ .tracing (Tracing .ACTIVE )
43
+ .code (Code .fromAsset ("../app/" , AssetOptions .builder ()
44
+ .bundling (BundlingOptions .builder ()
45
+ .image (Runtime .JAVA_11 .getBundlingImage ())
46
+ .command (functionPackageInstructions )
47
+ .build ())
48
+ .build ()))
49
+ .handler ("helloworld.App" )
50
+ .build ();
51
+
52
+ RestApi reastApi = RestApi .Builder .create (this , "HelloWorldApi" )
53
+ .description ("API Gateway endpoint URL for Prod stage for Hello World function" )
42
54
.build ();
55
+
56
+ reastApi .getRoot ().resourceForPath ("/hello" )
57
+ .addMethod ("GET" , LambdaIntegration .Builder .create (helloWorldFunction )
58
+ .build ());
59
+
43
60
}
44
61
}
0 commit comments