Skip to content

Commit

Permalink
Update kotlin-gradle-lambda-cdk for restate-cdk 1.1.0-rc.1 (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcholakov authored Sep 3, 2024
1 parent 357326d commit 25d23f3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 60 deletions.
10 changes: 6 additions & 4 deletions templates/kotlin-gradle-lambda-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ For more information on CDK, please see [Getting started with the AWS CDK](https
* npm
* gradle
* JDK >= 21
* Restate Cloud access (cluster id + API token)
* AWS account, bootstrapped for CDK use
* valid AWS credentials with sufficient privileges to create the necessary resources
* an existing [Restate Cloud](https://restate.dev) environment (environment id + API key)

Install npm dependencies:

```shell
npm clean-install
npm install
```

To deploy the stack, simply run:
To deploy the stack, export the Restate Cloud environment id and API key, and run `cdk deploy`:

```shell
npm run deploy
export RESTATE_ENV_ID=env_... RESTATE_API_KEY=key_...
npx cdk deploy
```

The stack output will print out the Restate server ingress URL.
Expand All @@ -59,6 +60,7 @@ You can send a test request to the Restate ingress endpoint to call the newly de

```shell
curl -k ${restateIngressUrl}/Greeter/greet \
-H "Authorization: Bearer $RESTATE_API_KEY" \
-H 'content-type: application/json' -d '"Restate"'
```

Expand Down
69 changes: 29 additions & 40 deletions templates/kotlin-gradle-lambda-cdk/cdk/lambda-jvm-cdk-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,54 @@
* https://github.com/restatedev/examples/
*/

import * as restate from "@restatedev/restate-cdk";
import * as cdk from "aws-cdk-lib";
import * as iam from "aws-cdk-lib/aws-iam";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as logs from "aws-cdk-lib/aws-logs";
import * as restate from "@restatedev/restate-cdk";
import * as secrets from "aws-cdk-lib/aws-secretsmanager";
import { Construct } from "constructs";

export class LambdaJvmCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);

const greeter: lambda.Function = new lambda.Function(this, "RestateKotlin", {
const handler: lambda.Function = new lambda.Function(this, "GreeterService", {
runtime: lambda.Runtime.JAVA_21,
architecture: lambda.Architecture.ARM_64,
code: lambda.Code.fromAsset("lambda/build/distributions/lambda.zip"),
handler: "dev.restate.sdk.examples.LambdaHandler",
timeout: cdk.Duration.seconds(10),
logFormat: lambda.LogFormat.JSON,
applicationLogLevel: "DEBUG",
systemLogLevel: "DEBUG",
});

// This role is used by Restate to invoke Lambda service handlers; see https://docs.restate.dev/deploy/cloud for
// information on deploying services to Restate Cloud environments. For standalone environments, the EC2 instance
// profile can be used directly instead of creating a separate role.
const invokerRole = new iam.Role(this, "InvokerRole", {
assumedBy: new iam.AccountRootPrincipal(), // set up trust such that your Restate environment can assume this role
loggingFormat: lambda.LoggingFormat.JSON,
applicationLogLevelV2: lambda.ApplicationLogLevel.DEBUG,
systemLogLevelV2: lambda.SystemLogLevel.INFO,
});

// You can reference an existing Restate environment you manage yourself or a Restate Cloud environment by
// configuring its address and optionally auth token. The deployer will use these settings to register the handlers.
const restateEnvironment = restate.RestateEnvironment.fromAttributes({
adminUrl: "https://restate.example.com:9070", // pre-existing Restate server address not managed by this stack
invokerRole,
// Set the RESTATE_ENV_ID and RESTATE_API_KEY environment variables to point to your Restate Cloud environment.
// This construct automatically creates an invoker role that Restate Cloud will be able to assume to invoke handlers
// on behalf of your environment. See https://docs.restate.dev/deploy/cloud for more information.
const restateEnvironment = new restate.RestateCloudEnvironment(this, "RestateCloud", {
environmentId: process.env.RESTATE_ENV_ID! as restate.EnvironmentId,
// Warning: this will result in the API key being baked into the CloudFormation template!
// For improved security, pre-populate the secret and pass it to the construct as a reference.
// See: https://docs.aws.amazon.com/secretsmanager/latest/userguide/cdk.html
apiKey: new secrets.Secret(this, "RestateCloudApiKey", {
secretStringValue: cdk.SecretValue.unsafePlainText(process.env.RESTATE_API_KEY!),
}),
});
const deployer = new restate.ServiceDeployer(this, "ServiceDeployer");

// Alternatively, you can deploy a standalone Restate server using the RestateServer construct. Please refer to
// https://docs.restate.dev/deploy/lambda/self-hosted and the construct documentation for details.
// Alternatively, you can deploy a standalone Restate server using the SingleNodeRestateDeployment construct.
// Please see https://docs.restate.dev/deploy/lambda/self-hosted and the construct documentation for more details.
// const vpc = ec2.Vpc.fromLookup(this, "Vpc", { vpcId: "..." });
// const restateEnvironment = new restate.SingleNodeRestateDeployment(this, "Restate", {
// logGroup: new logs.LogGroup(this, "RestateLogs", {
// logGroupName: "/restate/server-logs",
// retention: logs.RetentionDays.ONE_MONTH,
// }),
// vpc,
// networkConfiguration: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
// });
// const deployer = new restate.ServiceDeployer(this, "ServiceDeployer", {
// vpc,
// securityGroups: [restateEnvironment.adminSecurityGroup],
// });

const deployer = new restate.ServiceDeployer(this, "ServiceDeployer", {
logGroup: new logs.LogGroup(this, "Deployer", {
retention: logs.RetentionDays.ONE_MONTH,
removalPolicy: cdk.RemovalPolicy.DESTROY,
}),
});

// The environment's invoker role will be granted appropriate invoke permissions automatically.
// To use CDK hotswap deployments during development, deploy `latestVersion` instead.
deployer.deployService("Greeter", greeter.currentVersion, restateEnvironment, {
// insecure: true, // accept self-signed certificate for SingleNodeRestateDeployment
});

// If deploying a standalone Restate server, we can output the ingress URL like this.
// new cdk.CfnOutput(this, "restateIngressUrl", { value: restateEnvironment.ingressUrl });
deployer.deployService("Greeter", handler.currentVersion, restateEnvironment);
new cdk.CfnOutput(this, "restateIngressUrl", { value: restateEnvironment.ingressUrl });
}
}
24 changes: 10 additions & 14 deletions templates/kotlin-gradle-lambda-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@
"destroy": "npx cdk destroy"
},
"devDependencies": {
"@restatedev/restate-cdk": "^1.0.0",
"@types/jest": "^29.5.8",
"@types/node": "20.9.1",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"aws-cdk": "^2.121.0",
"esbuild": "^0.19.8",
"eslint": "^8.47.0",
"jest": "^29.7.0",
"prettier": "^3.1.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "~5.2.2"
"@restatedev/restate-cdk": "^1.1.0-rc.1",
"@types/node": "22.5.2",
"@typescript-eslint/eslint-plugin": "^8.3.0",
"aws-cdk": "^2.155.0",
"esbuild": "^0.23.1",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
},
"dependencies": {
"aws-cdk-lib": "^2.138.0",
"constructs": "^10.0.0",
"aws-cdk-lib": "^2.155.0",
"constructs": "^10.3.0",
"source-map-support": "^0.5.21"
}
}
4 changes: 2 additions & 2 deletions templates/typescript-lambda-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Install npm dependencies:
npm install
```

To deploy the stack create a free account, export the Restate Cloud environment id and API key, and run:

To deploy the stack, export the Restate Cloud environment id and API key, and run `cdk deploy`:
```shell
export RESTATE_ENV_ID=env_... RESTATE_API_KEY=key_...
npx cdk deploy
Expand Down

0 comments on commit 25d23f3

Please sign in to comment.