Skip to content

Commit 5810719

Browse files
authored
feat(graalvm): GraalVM support for powertools-cloudformation (#2090)
1 parent 11da725 commit 5810719

File tree

41 files changed

+1511
-400
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1511
-400
lines changed

.github/workflows/check-build.yml

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ jobs:
9292
- id: checkout
9393
name: Checkout repository
9494
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
95+
with:
96+
fetch-depth: 0
97+
- name: Get changed files
98+
id: changed-files
99+
uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5
100+
with:
101+
files: |
102+
powertools-*/**
95103
- name: Setup GraalVM
96104
uses: graalvm/setup-graalvm@7f488cf82a3629ee755e4e97342c01d6bed318fa # v1.3.5
97105
with:
@@ -100,18 +108,36 @@ jobs:
100108
cache: maven
101109
- id: graalvm-native-test
102110
name: GraalVM Native Test
111+
if: steps.changed-files.outputs.any_changed == 'true'
112+
env:
113+
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
103114
run: |
104115
# Build the entire project first to ensure test-jar dependencies are available
116+
echo "::group::Building project dependencies"
105117
mvn -B -q install -DskipTests
118+
echo "::endgroup::"
119+
120+
echo "Changes detected in powertools modules: $CHANGED_FILES"
106121
107-
# Find modules with graalvm-native profile and run tests recursively.
108-
# This will make sure to discover new GraalVM supported modules automatically in the future.
122+
# Find modules with graalvm-native profile and run tests
109123
find . -name "pom.xml" -path "./powertools-*" | while read module; do
110124
if grep -q "<id>graalvm-native</id>" "$module"; then
111125
module_dir=$(dirname "$module")
112-
echo "Regenerating GraalVM metadata for $module_dir"
113-
mvn -B -q -f "$module" -Pgenerate-graalvm-files clean test
114-
echo "Running GraalVM native tests for $module_dir"
115-
mvn -B -q -f "$module" -Pgraalvm-native test
126+
module_name=$(basename "$module_dir")
127+
128+
# Check if this specific module or common dependencies changed
129+
if echo "$CHANGED_FILES" | grep -q "$module_name/" || \
130+
echo " $CHANGED_FILES " | grep -q " pom.xml " || \
131+
echo "$CHANGED_FILES" | grep -q "powertools-common/"; then
132+
echo "::group::Building $module_name with GraalVM"
133+
echo "Changes detected in $module_name - running GraalVM tests"
134+
echo "Regenerating GraalVM metadata for $module_dir"
135+
mvn -B -q -f "$module" -Pgenerate-graalvm-files clean test
136+
echo "Running GraalVM native tests for $module_dir"
137+
mvn -B -q -f "$module" -Pgraalvm-native test
138+
echo "::endgroup::"
139+
else
140+
echo "No changes detected in $module_name - skipping GraalVM tests"
141+
fi
116142
fi
117143
done

GraalVM.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,31 @@ java.lang.InternalError: com.oracle.svm.core.jdk.UnsupportedFeatureError: Defini
5656
```
5757
- This has been [fixed](https://github.com/apache/logging-log4j2/discussions/2364#discussioncomment-8950077) in Log4j 2.24.x. PT has been updated to use this version of Log4j
5858

59+
3. **Test Class Organization**
60+
- **Issue**: Anonymous inner classes and lambda expressions in Mockito matchers cause `NoSuchMethodError` in GraalVM native tests
61+
- **Solution**:
62+
- Extract static inner test classes to separate concrete classes in the same package as the class under test
63+
- Replace lambda expressions in `ArgumentMatcher` with concrete implementations
64+
- Use `mockito-subclass` dependency in GraalVM profiles
65+
- **Example**: Replace `argThat(resp -> resp.getStatus() != expectedStatus)` with:
66+
```java
67+
argThat(new ArgumentMatcher<Response>() {
68+
@Override
69+
public boolean matches(Response resp) {
70+
return resp != null && resp.getStatus() != expectedStatus;
71+
}
72+
})
73+
```
74+
75+
4. **Package Visibility Issues**
76+
- **Issue**: Test handler classes cannot access package-private methods when placed in subpackages
77+
- **Solution**: Place test handler classes in the same package as the class under test, not in subpackages like `handlers/`
78+
- **Example**: Use `software.amazon.lambda.powertools.cloudformation` instead of `software.amazon.lambda.powertools.cloudformation.handlers`
79+
80+
5. **Test Stubs Best Practice**
81+
- **Best Practice**: Avoid mocking where possible and use concrete test stubs provided by `powertools-common` package
82+
- **Solution**: Use `TestLambdaContext` and other test stubs from `powertools-common` test-jar instead of Mockito mocks
83+
- **Implementation**: Add `powertools-common` test-jar dependency and replace `mock(Context.class)` with `new TestLambdaContext()`
84+
5985
## Reference Implementation
6086
Working example is available in the [examples](examples/powertools-examples-core-utilities/sam-graalvm).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build-HelloWorldFunction:
2+
chmod +x target/hello-world
3+
cp target/hello-world $(ARTIFACTS_DIR) # (ARTIFACTS_DIR --> https://github.com/aws/aws-lambda-builders/blob/develop/aws_lambda_builders/workflows/custom_make/DESIGN.md#implementation)
4+
chmod +x src/main/config/bootstrap
5+
cp src/main/config/bootstrap $(ARTIFACTS_DIR)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Use the official AWS SAM base image for Java 21
2+
FROM public.ecr.aws/sam/build-java21@sha256:a5554d68374e19450c6c88448516ac95a9acedc779f318040f5c230134b4e461
3+
4+
# Install GraalVM dependencies
5+
RUN curl -4 -L curl https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_linux-x64_bin.tar.gz | tar -xvz
6+
RUN mv graalvm-jdk-21.* /usr/lib/graalvm
7+
8+
# Make native image and mvn available on CLI
9+
RUN ln -s /usr/lib/graalvm/bin/native-image /usr/bin/native-image
10+
RUN ln -s /usr/lib/maven/bin/mvn /usr/bin/mvn
11+
12+
# Set GraalVM as default
13+
ENV JAVA_HOME=/usr/lib/graalvm
14+
ENV PATH=/usr/lib/graalvm/bin:$PATH
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Powertools for AWS Lambda (Java) - CloudFormation Custom Resource Example with SAM on GraalVM
2+
3+
This project contains an example of a Lambda function using the CloudFormation module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/custom_resources/).
4+
5+
In this example you pass in a bucket name as a parameter and upon CloudFormation events a call is made to a lambda. That lambda attempts to create the bucket on CREATE events, create a new bucket if the name changes with an UPDATE event and delete the bucket upon DELETE events.
6+
7+
Have a look at [App.java](../../src/main/java/helloworld/App.java) for the full details.
8+
9+
## Build the sample application
10+
11+
> [!NOTE]
12+
> Building AWS Lambda packages on macOS (ARM64/Intel) for deployment on AWS Lambda (Linux x86_64 or ARM64) will result in incompatible binary dependencies that cause import errors at runtime.
13+
14+
Choose the appropriate build method based on your operating system:
15+
16+
### Build locally using Docker
17+
18+
Recommended for macOS and Windows users: Cross-compile using Docker to match target platform of Lambda:
19+
20+
```shell
21+
docker build --platform linux/amd64 . -t powertools-examples-cloudformation-sam-graalvm
22+
docker run --platform linux/amd64 -it -v `pwd`/../..:`pwd`/../.. -w `pwd`/../.. -v ~/.m2:/root/.m2 powertools-examples-cloudformation-sam-graalvm mvn clean -Pnative-image package -DskipTests
23+
sam build --use-container --build-image powertools-examples-cloudformation-sam-graalvm
24+
```
25+
26+
**Note**: The Docker run command mounts your local Maven cache (`~/.m2`) and builds the native binary with SNAPSHOT support, then SAM packages the pre-built binary.
27+
28+
### Build on native OS
29+
30+
For Linux users with GraalVM installed:
31+
32+
```shell
33+
export JAVA_HOME=<path to GraalVM>
34+
cd ../..
35+
mvn clean -Pnative-image package -DskipTests
36+
cd infra/sam-graalvm
37+
sam build
38+
```
39+
40+
## Deploy the sample application
41+
42+
```shell
43+
sam deploy --guided --parameter-overrides BucketNameParam=my-unique-bucket-2.3.0718
44+
```
45+
46+
This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting started with SAM in [the examples directory](../../../README.md)
47+
48+
## Test the application
49+
50+
The CloudFormation custom resource will be triggered automatically during stack deployment. You can monitor the Lambda function execution in CloudWatch Logs to see the custom resource handling CREATE, UPDATE, and DELETE events for the S3 bucket.
51+
52+
Check out [App.java](../../src/main/java/helloworld/App.java) to see how it works!
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
powertools-examples-cloudformation-graalvm
5+
6+
Sample SAM Template for powertools-examples-cloudformation with GraalVM native image
7+
8+
Globals:
9+
Function:
10+
Timeout: 20
11+
12+
Parameters:
13+
BucketNameParam:
14+
Type: String
15+
16+
Resources:
17+
HelloWorldCustomResource:
18+
Type: AWS::CloudFormation::CustomResource
19+
Properties:
20+
ServiceToken: !GetAtt HelloWorldFunction.Arn
21+
BucketName: !Ref BucketNameParam
22+
23+
HelloWorldFunction:
24+
Type: AWS::Serverless::Function
25+
Properties:
26+
CodeUri: ../../
27+
Handler: helloworld.App::handleRequest
28+
Runtime: provided.al2023
29+
Architectures:
30+
- x86_64
31+
MemorySize: 512
32+
Policies:
33+
- Statement:
34+
- Sid: bucketaccess1
35+
Effect: Allow
36+
Action:
37+
- s3:GetLifecycleConfiguration
38+
- s3:PutLifecycleConfiguration
39+
- s3:CreateBucket
40+
- s3:ListBucket
41+
- s3:DeleteBucket
42+
Resource: '*'
43+
Metadata:
44+
BuildMethod: makefile
45+
46+
Outputs:
47+
HelloWorldFunction:
48+
Description: "Hello World Lambda Function ARN"
49+
Value: !GetAtt HelloWorldFunction.Arn

0 commit comments

Comments
 (0)