From 845473c86ac799dd79b9c756b19a46b55225f9f0 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Thu, 31 Aug 2023 11:22:19 -0400 Subject: [PATCH] update the Readme --- javav2/usecases/creating_fsa_app/README.md | 75 +++++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/javav2/usecases/creating_fsa_app/README.md b/javav2/usecases/creating_fsa_app/README.md index fbad15ab82d..9684d5d689d 100644 --- a/javav2/usecases/creating_fsa_app/README.md +++ b/javav2/usecases/creating_fsa_app/README.md @@ -808,9 +808,78 @@ package com.example.fsa; public class FSAApplicationResources { - public static final String STORAGE_BUCKET = "fsa-scmacdon-javascript-mediabucket8240391c-1wba4q2k3xco9"; + public static final String STORAGE_BUCKET = ""; } +``` + +### ModifyLambda class + +Add the The following Java code to the **com.example.fsa** package. This represents the **ModifyLambda** class that is used to update all four Lambda functions created using the CDK script with the Lambda functions created using the AWS SDK for Java V2. Besure to specify the correct Lambda function names and the name of the FAT JAR that is placed in the S3 bucket (see the instructions later in this document). + +```java +package com.example.fsa; + +import software.amazon.awssdk.core.waiters.WaiterResponse; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.lambda.LambdaClient; +import software.amazon.awssdk.services.lambda.model.GetFunctionConfigurationRequest; +import software.amazon.awssdk.services.lambda.model.GetFunctionConfigurationResponse; +import software.amazon.awssdk.services.lambda.model.LambdaException; +import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeRequest; +import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeResponse; +import software.amazon.awssdk.services.lambda.waiters.LambdaWaiter; + +public class ModifyLambda { + + public static void main(String[]args) { + String analyzeSentimentLambda = "" ; + String synthesizeAudioLambda = """ ; + String extractTextLambda = "" ; + String translateTextLambda = """ ; + String bucketName = FSAApplicationResources.STORAGE_BUCKET; + String key = ""; + + Region region = Region.US_EAST_1; + LambdaClient awsLambda = LambdaClient.builder() + .region(region) + .build(); + + // Update all four Lambda functions. + updateFunctionCode(awsLambda, analyzeSentimentLambda, bucketName, key); + updateFunctionCode(awsLambda, synthesizeAudioLambda, bucketName, key); + updateFunctionCode(awsLambda, extractTextLambda, bucketName, key); + updateFunctionCode(awsLambda, translateTextLambda, bucketName, key); + System.out.println("You have successfully updated the AWS Lambda functions"); + } + + public static void updateFunctionCode(LambdaClient awsLambda, String functionName, String bucketName, String key) { + try { + LambdaWaiter waiter = awsLambda.waiter(); + UpdateFunctionCodeRequest functionCodeRequest = UpdateFunctionCodeRequest.builder() + .functionName(functionName) + .publish(true) + .s3Bucket(bucketName) + .s3Key(key) + .build(); + + UpdateFunctionCodeResponse response = awsLambda.updateFunctionCode(functionCodeRequest) ; + GetFunctionConfigurationRequest getFunctionConfigRequest = GetFunctionConfigurationRequest.builder() + .functionName(functionName) + .build(); + + WaiterResponse waiterResponse = waiter.waitUntilFunctionUpdated(getFunctionConfigRequest); + waiterResponse.matched().response().ifPresent(System.out::println); + System.out.println("The last modified value is " +response.lastModified()); + + } catch(LambdaException e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } +} + + ``` @@ -830,11 +899,11 @@ After you execute the AWS CDK script, the Lambda functions are created. However, 2. Create the FAT JAR by running **mvn package**. The JAR file can be located in the target folder. -3. Use the AWS Management Console to place the FAT JAR into the Amazon S3 bucket named **mediabucket**. (This was created by the AWS CDK script.) +3. Use the AWS Management Console to place the FAT JAR into the Amazon S3 bucket named **STORAGE_BUCKET**. (This was created by the AWS CDK script.) ![AWS Photo Analyzer](images/kotlinJarS3.png) -4. Create a new Java project. For information, see [Basic setup to work with AWS services](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup-basics.html). +4. Run the ModifyLambda Java class. 5. Add the following Java code to the project and update the Java variable names with the resources created by the AWS CDK script. Make sure to specify the exact names, which are provided in the AWS Lambda console. Otherwise, the code does not work. Run the following code.