Skip to content

Commit c096829

Browse files
committed
Add Java CDK example
1 parent afde5da commit c096829

21 files changed

+818
-0
lines changed

.tools/prepare_release_zip.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ create_release_zip python/end-to-end-applications/food-ordering python-food-orde
2525
create_release_zip java/basics java-basics
2626
create_release_zip java/end-to-end-applications/food-ordering java-food-ordering
2727
create_release_zip java/end-to-end-applications/subway-fare-calculator java-subway-fare-calculator
28+
create_release_zip java/integrations/java-gradle-lambda-cdk java-hello-world-lambda-cdk
2829
create_release_zip java/patterns-use-cases java-patterns-use-cases
2930
create_release_zip java/templates/java-gradle java-hello-world-gradle
3031
create_release_zip java/templates/java-maven java-hello-world-maven
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
cdk.out
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2021": true
6+
},
7+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
8+
"overrides": [],
9+
"parser": "@typescript-eslint/parser",
10+
"parserOptions": {
11+
"ecmaVersion": "latest"
12+
},
13+
"plugins": ["@typescript-eslint"],
14+
"rules": {}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
!jest.config.js
2+
node_modules
3+
.cdk.staging
4+
cdk.out
5+
.gradle
6+
lambda/build
7+
*.js
8+
*.d.ts
9+
*.class
10+
cdk.context.json
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 2,
4+
"semi": true,
5+
"arrowParens": "always",
6+
"printWidth": 120
7+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Hello world - Java Lambda (CDK) example
2+
3+
Sample project deploying a Java-based Restate service to AWS Lambda using the AWS Cloud Development Kit (CDK).
4+
The stack uses the Restate CDK constructs library to register the service with against a Restate Cloud environment.
5+
6+
For more information on CDK, please see [Getting started with the AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html).
7+
8+
* [CDK app entry point `lambda-jvm-cdk.ts`](bin/lambda-jvm-cdk.ts)
9+
* [CDK stack consisting of a Lambda function and providing Restate service registration](cdk/lambda-jvm-cdk-stack.ts)
10+
* [Java Lambda handler](lambda) - based on [`hello-world-java`](../java-gradle)
11+
12+
## Download the example
13+
14+
- Via the CLI:
15+
```shell
16+
restate example java-hello-world-lambda-cdk && cd java-hello-world-lambda-cdk
17+
```
18+
19+
- Via git clone:
20+
```shell
21+
git clone [email protected]:restatedev/examples.git
22+
cd examples/templates/java-gradle-lambda-cdk
23+
```
24+
25+
- Via `wget`:
26+
```shell
27+
wget https://github.com/restatedev/examples/releases/latest/download/java-hello-world-lambda-cdk.zip && unzip java-hello-world-lambda-cdk.zip -d java-hello-world-lambda-cdk && rm java-hello-world-lambda-cdk.zip
28+
```
29+
30+
## Deploy
31+
32+
**Pre-requisites:**
33+
34+
* npm
35+
* gradle
36+
* JDK >= 21
37+
* AWS account, bootstrapped for CDK use
38+
* valid AWS credentials with sufficient privileges to create the necessary resources
39+
* an existing [Restate Cloud](https://restate.dev) environment (environment id + API key)
40+
41+
Install npm dependencies:
42+
43+
```shell
44+
npm install
45+
```
46+
47+
To deploy the stack, export the Restate Cloud environment id and API key, and run `cdk deploy`:
48+
49+
```shell
50+
export RESTATE_ENV_ID=env_... RESTATE_API_KEY=key_...
51+
npx cdk deploy
52+
```
53+
54+
The stack output will print out the Restate server ingress URL.
55+
56+
### Test
57+
58+
You can send a test request to the Restate ingress endpoint to call the newly deployed service:
59+
60+
```shell
61+
curl -k ${restateIngressUrl}/Greeter/greet \
62+
-H "Authorization: Bearer $RESTATE_API_KEY" \
63+
-H 'content-type: application/json' -d '"Restate"'
64+
```
65+
66+
### Useful commands
67+
68+
* `npm run build` compile the Lambda handler and synthesize CDK deployment artifacts
69+
* `npm run deploy` perform a CDK deployment
70+
* `npm run destroy` delete the stack and all its resources
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env node
2+
3+
/*
4+
* Copyright (c) 2024 - Restate Software, Inc., Restate GmbH
5+
*
6+
* This file is part of the Restate examples,
7+
* which is released under the MIT license.
8+
*
9+
* You can find a copy of the license in the file LICENSE
10+
* in the root directory of this repository or package or at
11+
* https://github.com/restatedev/examples/
12+
*/
13+
14+
import "source-map-support/register";
15+
import * as cdk from "aws-cdk-lib";
16+
import { LambdaJvmCdkStack } from "../cdk/lambda-jvm-cdk-stack";
17+
18+
const app = new cdk.App();
19+
new LambdaJvmCdkStack(app, "LambdaJvmCdkStack", {
20+
env: {
21+
account: process.env.CDK_DEFAULT_ACCOUNT,
22+
region: process.env.CDK_DEFAULT_REGION,
23+
},
24+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/lambda-jvm-cdk.ts",
3+
"watch": {
4+
"include": ["**"],
5+
"exclude": [
6+
"README.md",
7+
"cdk*.json",
8+
"**/*.d.ts",
9+
"**/*.js",
10+
"tsconfig.json",
11+
"package*.json",
12+
"yarn.lock",
13+
"node_modules",
14+
"test"
15+
]
16+
},
17+
"context": {
18+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
19+
"@aws-cdk/core:checkSecretUsage": true,
20+
"@aws-cdk/core:target-partitions": ["aws", "aws-cn"],
21+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
22+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
23+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
24+
"@aws-cdk/aws-iam:minimizePolicies": true,
25+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
26+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
27+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
28+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
29+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
30+
"@aws-cdk/core:enablePartitionLiterals": true,
31+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
32+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
33+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
34+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
35+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
36+
"@aws-cdk/aws-route53-patters:useCertificate": true,
37+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
38+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
39+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
40+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
41+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
42+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
43+
"@aws-cdk/aws-redshift:columnId": true,
44+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
45+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
46+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
47+
"@aws-cdk/aws-kms:aliasNameRef": true,
48+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
49+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
50+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
51+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
52+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
53+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
54+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
55+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
56+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
57+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true
58+
}
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate examples,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in the file LICENSE
8+
* in the root directory of this repository or package or at
9+
* https://github.com/restatedev/examples/
10+
*/
11+
12+
import * as restate from "@restatedev/restate-cdk";
13+
import * as cdk from "aws-cdk-lib";
14+
import * as lambda from "aws-cdk-lib/aws-lambda";
15+
import * as secrets from "aws-cdk-lib/aws-secretsmanager";
16+
import { Construct } from "constructs";
17+
18+
export class LambdaJvmCdkStack extends cdk.Stack {
19+
constructor(scope: Construct, id: string, props: cdk.StackProps) {
20+
super(scope, id, props);
21+
22+
const handler: lambda.Function = new lambda.Function(this, "GreeterService", {
23+
runtime: lambda.Runtime.JAVA_21,
24+
architecture: lambda.Architecture.ARM_64,
25+
code: lambda.Code.fromAsset("lambda/build/distributions/lambda.zip"),
26+
handler: "my.example.LambdaHandler",
27+
timeout: cdk.Duration.seconds(10),
28+
loggingFormat: lambda.LoggingFormat.JSON,
29+
applicationLogLevelV2: lambda.ApplicationLogLevel.DEBUG,
30+
systemLogLevelV2: lambda.SystemLogLevel.INFO,
31+
});
32+
33+
// Set the RESTATE_ENV_ID and RESTATE_API_KEY environment variables to point to your Restate Cloud environment.
34+
// This construct automatically creates an invoker role that Restate Cloud will be able to assume to invoke handlers
35+
// on behalf of your environment. See https://docs.restate.dev/deploy/cloud for more information.
36+
const restateEnvironment = new restate.RestateCloudEnvironment(this, "RestateCloud", {
37+
environmentId: process.env.RESTATE_ENV_ID! as restate.EnvironmentId,
38+
// Warning: this will result in the API key being baked into the CloudFormation template!
39+
// For improved security, pre-populate the secret and pass it to the construct as a reference.
40+
// See: https://docs.aws.amazon.com/secretsmanager/latest/userguide/cdk.html
41+
apiKey: new secrets.Secret(this, "RestateCloudApiKey", {
42+
secretStringValue: cdk.SecretValue.unsafePlainText(process.env.RESTATE_API_KEY!),
43+
}),
44+
});
45+
const deployer = new restate.ServiceDeployer(this, "ServiceDeployer");
46+
47+
// Alternatively, you can deploy a standalone Restate server using the SingleNodeRestateDeployment construct.
48+
// Please see https://docs.restate.dev/deploy/lambda/self-hosted and the construct documentation for more details.
49+
// const vpc = ec2.Vpc.fromLookup(this, "Vpc", { vpcId: "..." });
50+
// const restateEnvironment = new restate.SingleNodeRestateDeployment(this, "Restate", {
51+
// vpc,
52+
// networkConfiguration: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
53+
// });
54+
// const deployer = new restate.ServiceDeployer(this, "ServiceDeployer", {
55+
// vpc,
56+
// securityGroups: [restateEnvironment.adminSecurityGroup],
57+
// });
58+
59+
deployer.deployService("Greeter", handler.currentVersion, restateEnvironment);
60+
new cdk.CfnOutput(this, "restateIngressUrl", { value: restateEnvironment.ingressUrl });
61+
}
62+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
arguments=--init-script /var/folders/4x/r9ldvl2j0pldhp2nb9qvxy0w0000gn/T/db3b08fc4a9ef609cb16b96b200fa13e563f396e9bb1ed0905fdab7bc3bc513b.gradle
2+
auto.sync=false
3+
build.scans.enabled=false
4+
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
5+
connection.project.dir=
6+
eclipse.preferences.version=1
7+
gradle.user.home=
8+
java.home=/Library/Java/JavaVirtualMachines/zulu-19.jdk/Contents/Home
9+
jvm.arguments=
10+
offline.mode=false
11+
override.workspace.settings=true
12+
show.console.view=true
13+
show.executions.view=true

0 commit comments

Comments
 (0)