From 25396adce2d54e6ddd89f685a5a794ed29202814 Mon Sep 17 00:00:00 2001 From: matto Date: Mon, 25 Mar 2024 16:36:40 +1100 Subject: [PATCH] use `sandpiper` infra --- README.md | 42 ++++++++++--------- cdk.json | 2 +- docker-compose.yml | 2 +- .../infra/SpringNativeAwsFunctionStack.java | 9 ++-- .../ReflectionRuntimeHints.java | 26 ++++++------ .../src/shell/native/bootstrap | 2 +- 6 files changed, 43 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index e9d60bb..3176fa9 100644 --- a/README.md +++ b/README.md @@ -43,12 +43,17 @@ $ ./mvnw -ntp clean verify -U ``` The service responds ```json - [ - { - "name": "CoffeeBeans", - "saved": true - } - ] + { + "id": "production1234someapp", + "env": "production", + "costCentre": "1234", + "applicationName": "some-app", + "items": { + "GITHUB_TOKEN": "WOAH", + "AWS_ACCESS_KEY_ID": "OMG", + "AWS_SECRET_ACCESS_KEY": "OH NO" + } + } ``` #### Using `mvnw` @@ -81,16 +86,6 @@ $ ./mvnw -ntp clean verify -U "body": "{ \"env\": \"production\", \"costCentre\": \"1234\", \"applicationName\": \"some-app\", \"items\": { \"GITHUB_TOKEN\": \"WOAH\", \"AWS_ACCESS_KEY_ID\": \"OMG\", \"AWS_SECRET_ACCESS_KEY\": \"OH NO\" } }" }' ``` -```shell -curl --location --request POST 'http://localhost:8080' \ - --header 'Content-Type: application/json' \ - --data-raw '{ - "httpMethod": "GET", - "pathParameters": { - "proxy": "production-1234-someapp" - } - }' -``` The service responds ```json @@ -451,8 +446,11 @@ Now that the setup is done you can deploy to AWS. the [github action](.github/workflows/release.yml) will start and a deployment to AWS environment. 2. Test via curl - ```shell - $ curl --location --request POST 'https://lmk0qo0xrl.execute-api.ap-southeast-2.amazonaws.com/dev/' \ + +POST + +```shell + $ curl --location --request POST 'https://{apiid}.execute-api.ap-southeast-2.amazonaws.com/dev/' \ --header 'Content-Type: application/json' \ --data-raw '{ "env": "production", @@ -464,6 +462,12 @@ Now that the setup is done you can deploy to AWS. "AWS_SECRET_ACCESS_KEY": "OH NO" } }' - ``` +``` + +GET + + ```shell + curl --request GET 'https://{apiid}.execute-api.ap-southeast-2.amazonaws.com/dev/production-1234-someapp' + ``` 3. Et voila! It runs with 500 ms for cold start. diff --git a/cdk.json b/cdk.json index 323e56b..04ac302 100644 --- a/cdk.json +++ b/cdk.json @@ -1,5 +1,5 @@ { - "app": "./mvnw --settings /Users/muhammad/.m2/settings-nonqantas.xml exec:java -pl spring-native-aws-service-infra -Dexec.mainClass=com.coffeebeans.springnativeawslambda.infra.Application", + "app": "./mvnw exec:java -pl spring-native-aws-service-infra -Dexec.mainClass=com.coffeebeans.springnativeawslambda.infra.Application", "context": { "@aws-cdk/core:bootstrapQualifier": "cbcore" } diff --git a/docker-compose.yml b/docker-compose.yml index 9e14073..696ff14 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -49,7 +49,7 @@ services: ' function package_spring_native_function() { if [ "$$BUILD_ARTIFACT" = "true" ]; then - ./mvnw -ntp clean -Pnative -DskipTests native:compile package -pl "$$FUNCTION_NAME" --settings /home/worker/.m2/settings-nonqantas.xml + ./mvnw -ntp clean -Pnative -DskipTests native:compile package -pl "$$FUNCTION_NAME" else print_info_message "plain" "BUILD_ARTIFACT environment variable is not set. Skipping Maven build." fi diff --git a/spring-native-aws-service-infra/src/main/java/com/coffeebeans/springnativeawslambda/infra/SpringNativeAwsFunctionStack.java b/spring-native-aws-service-infra/src/main/java/com/coffeebeans/springnativeawslambda/infra/SpringNativeAwsFunctionStack.java index 5ac3173..ace88d6 100644 --- a/spring-native-aws-service-infra/src/main/java/com/coffeebeans/springnativeawslambda/infra/SpringNativeAwsFunctionStack.java +++ b/spring-native-aws-service-infra/src/main/java/com/coffeebeans/springnativeawslambda/infra/SpringNativeAwsFunctionStack.java @@ -21,17 +21,15 @@ import io.sadpipers.cdk.type.KebabCaseString; import io.sadpipers.cdk.type.SafeString; import io.sandpipers.cdk.core.AbstractEnvironment; +import io.sandpipers.cdk.core.construct.BaseStack; import io.sandpipers.cdk.core.construct.dynamodb.TableV2; import io.sandpipers.cdk.core.construct.dynamodb.TableV2.TableProps; import io.sandpipers.cdk.core.construct.lambda.CustomRuntime2023Function; import io.sandpipers.cdk.core.construct.lambda.CustomRuntime2023Function.CustomRuntime2023FunctionProps; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; -import software.amazon.awscdk.DefaultStackSynthesizer; import software.amazon.awscdk.Duration; import software.amazon.awscdk.RemovalPolicy; -import software.amazon.awscdk.Stack; -import software.amazon.awscdk.StackProps; import software.amazon.awscdk.services.apigateway.LambdaRestApi; import software.amazon.awscdk.services.apigateway.Resource; import software.amazon.awscdk.services.apigateway.StageOptions; @@ -51,7 +49,7 @@ import static software.amazon.awscdk.services.iam.ManagedPolicy.fromAwsManagedPolicyName; import static software.amazon.awscdk.services.lambda.Code.fromAsset; -public class SpringNativeAwsFunctionStack extends Stack { +public class SpringNativeAwsFunctionStack extends BaseStack { private static final int LAMBDA_FUNCTION_TIMEOUT_IN_SECONDS = 3; private static final int LAMBDA_FUNCTION_MEMORY_SIZE = 512; @@ -63,8 +61,7 @@ public SpringNativeAwsFunctionStack(@NotNull final Application app, @NotNull final AbstractEnvironment environment, @NotBlank final String lambdaCodePath, @NotBlank final String stage) { - super(app, "SpringNativeAwsFunctionStack", - StackProps.builder().synthesizer(DefaultStackSynthesizer.Builder.create().qualifier("cbcore").build()).build()); + super(app, environment); final List managedPolicies = List.of(fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole")); diff --git a/spring-native-aws-service/src/main/java/com/coffeebeans/springnativeawslambda/ReflectionRuntimeHints.java b/spring-native-aws-service/src/main/java/com/coffeebeans/springnativeawslambda/ReflectionRuntimeHints.java index ceeeb72..b9e3771 100644 --- a/spring-native-aws-service/src/main/java/com/coffeebeans/springnativeawslambda/ReflectionRuntimeHints.java +++ b/spring-native-aws-service/src/main/java/com/coffeebeans/springnativeawslambda/ReflectionRuntimeHints.java @@ -18,7 +18,6 @@ package com.coffeebeans.springnativeawslambda; -import java.util.List; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; import com.coffeebeans.springnativeawslambda.model.Secret; @@ -29,18 +28,21 @@ import org.springframework.aot.hint.TypeReference; import org.springframework.lang.Nullable; +import java.util.List; + public class ReflectionRuntimeHints implements RuntimeHintsRegistrar { - @Override - public void registerHints(final RuntimeHints hints, @Nullable final ClassLoader classLoader) { - final List typeReferences = List.of( - TypeReference.of(DateTime.class), - TypeReference.of(Secret.class), - TypeReference.of(APIGatewayProxyResponseEvent.class), - TypeReference.of(APIGatewayProxyRequestEvent.class), - TypeReference.of(APIGatewayProxyRequestEvent.ProxyRequestContext.class) - ); + @Override + public void registerHints(final RuntimeHints hints, @Nullable final ClassLoader classLoader) { + final List typeReferences = List.of( + TypeReference.of(DateTime.class), + TypeReference.of(Secret.class), + TypeReference.of(APIGatewayProxyResponseEvent.class), + TypeReference.of(APIGatewayProxyRequestEvent.class), + TypeReference.of(APIGatewayProxyRequestEvent.ProxyRequestContext.class), + TypeReference.of(APIGatewayProxyRequestEvent.RequestIdentity.class) + ); - hints.reflection().registerTypes(typeReferences, builder -> builder.withMembers(MemberCategory.values())); - } + hints.reflection().registerTypes(typeReferences, builder -> builder.withMembers(MemberCategory.values())); + } } diff --git a/spring-native-aws-service/src/shell/native/bootstrap b/spring-native-aws-service/src/shell/native/bootstrap index b3118d4..ff8d3b6 100755 --- a/spring-native-aws-service/src/shell/native/bootstrap +++ b/spring-native-aws-service/src/shell/native/bootstrap @@ -21,4 +21,4 @@ set -euo pipefail cd ${LAMBDA_TASK_ROOT:-.} -./spring-native-aws-function \ No newline at end of file +./spring-native-aws-service \ No newline at end of file