3333
3434import java .io .File ;
3535import java .io .IOException ;
36+ import java .io .InputStream ;
3637import java .nio .file .Path ;
3738import java .util .*;
39+ import java .util .stream .Collectors ;
3840
3941import static java .util .Collections .singletonList ;
4042
@@ -92,7 +94,7 @@ private Infrastructure(Builder builder) {
9294
9395 public String deploy () {
9496 uploadAssets ();
95- LOG .debug ("Deploying '" + stackName + "' on account " + account );
97+ LOG .info ("Deploying '" + stackName + "' on account " + account );
9698 cfn .createStack (CreateStackRequest .builder ()
9799 .stackName (stackName )
98100 .templateBody (new Yaml ().dump (cfnTemplate ))
@@ -102,15 +104,15 @@ public String deploy() {
102104 .build ());
103105 WaiterResponse <DescribeStacksResponse > waiterResponse = cfn .waiter ().waitUntilStackCreateComplete (DescribeStacksRequest .builder ().stackName (stackName ).build ());
104106 if (waiterResponse .matched ().response ().isPresent ()) {
105- LOG .debug ("Stack " + waiterResponse .matched ().response ().get ().stacks ().get (0 ).stackName () + " successfully deployed" );
107+ LOG .info ("Stack " + waiterResponse .matched ().response ().get ().stacks ().get (0 ).stackName () + " successfully deployed" );
106108 } else {
107109 throw new RuntimeException ("Failed to create stack" );
108110 }
109111 return functionName ;
110112 }
111113
112114 public void destroy () {
113- LOG .debug ("Deleting '" + stackName + "' on account " + account );
115+ LOG .info ("Deleting '" + stackName + "' on account " + account );
114116 cfn .deleteStack (DeleteStackRequest .builder ().stackName (stackName ).build ());
115117 }
116118
@@ -124,10 +126,32 @@ public static class Builder {
124126 public String testName ;
125127 private String stackName ;
126128 private boolean tracing = false ;
127- private JavaRuntime runtime = JavaRuntime . JAVA11 ;
129+ private JavaRuntime runtime ;
128130 private Map <String , String > environmentVariables = new HashMap <>();
129131 private String idemPotencyTable ;
130132
133+ private Builder () {
134+ getJavaRuntime ();
135+ }
136+
137+ /**
138+ * Retrieve the java runtime to use for the lambda function.
139+ */
140+ private void getJavaRuntime () {
141+ String javaVersion = System .getenv ("JAVA_VERSION" ); // must be set in GitHub actions
142+ if (javaVersion == null ) {
143+ throw new IllegalArgumentException ("JAVA_VERSION is not set" );
144+ }
145+ if (javaVersion .startsWith ("8" )) {
146+ runtime = JavaRuntime .JAVA8AL2 ;
147+ } else if (javaVersion .startsWith ("11" )) {
148+ runtime = JavaRuntime .JAVA11 ;
149+ } else {
150+ throw new IllegalArgumentException ("Unsupported Java version " + javaVersion );
151+ }
152+ LOG .debug ("Java Version set to {}, using runtime {}" , javaVersion , runtime .getRuntime ());
153+ }
154+
131155 public Infrastructure build () {
132156 Objects .requireNonNull (testName , "testName must not be null" );
133157
@@ -153,11 +177,6 @@ public Builder tracing(boolean tracing) {
153177 return this ;
154178 }
155179
156- public Builder runtime (JavaRuntime runtime ) {
157- this .runtime = runtime ;
158- return this ;
159- }
160-
161180 public Builder idempotencyTable (String tableName ) {
162181 this .idemPotencyTable = tableName ;
163182 return this ;
@@ -203,6 +222,7 @@ private Stack createStackWithLambda() {
203222
204223 functionName = stackName + "-function" ;
205224
225+ LOG .debug ("Building Lambda function with command " + packagingInstruction .stream ().collect (Collectors .joining (" " , "[" , "]" )));
206226 Function function = Function .Builder
207227 .create (stack , functionName )
208228 .code (Code .fromAsset ("handlers/" , AssetOptions .builder ()
@@ -255,10 +275,10 @@ private void uploadAssets() {
255275 }
256276 ListObjectsV2Response objects = s3 .listObjectsV2 (ListObjectsV2Request .builder ().bucket (asset .bucketName ).build ());
257277 if (objects .contents ().stream ().anyMatch (o -> o .key ().equals (objectKey ))) {
258- System . out . println ("Asset already exists, skipping" );
278+ LOG . debug ("Asset already exists, skipping" );
259279 return ;
260280 }
261- System . out . println ("Uploading asset " + objectKey + " to " + asset .bucketName );
281+ LOG . info ("Uploading asset " + objectKey + " to " + asset .bucketName );
262282 s3 .putObject (PutObjectRequest .builder ().bucket (asset .bucketName ).key (objectKey ).build (), Path .of (cfnAssetDirectory , asset .assetPath ));
263283 });
264284 }
0 commit comments