Skip to content

Commit

Permalink
update the Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon committed Aug 31, 2023
1 parent 290b2e0 commit 845473c
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions javav2/usecases/creating_fsa_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<Update the bucket name>";
}

```

### 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 = "<Enter value>" ;
String synthesizeAudioLambda = "<Enter value>"" ;
String extractTextLambda = "<Enter value>" ;
String translateTextLambda = "<Enter value>"" ;
String bucketName = FSAApplicationResources.STORAGE_BUCKET;
String key = "<Enter value>";

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<GetFunctionConfigurationResponse> 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);
}
}
}


```


Expand All @@ -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.

Expand Down

0 comments on commit 845473c

Please sign in to comment.