Skip to content

Adding Golang code for Serverless CDK example #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions v2/serverless_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ cdk init --language csharp

You may now open `src/MyWidgetService.sln` in Visual Studio\.

------
#### [ Go ]

```
mkdir MyWidgetService
cd MyWidgetService
cdk init --language go
```

You may now open the Go project in your IDE\.

You may also need to run `go get` and `go mod tidy` before your run `cdk synth` in next steps

------

**Note**
Expand Down Expand Up @@ -111,6 +124,11 @@ The important files in the blank project are as follows\. \(We will also be addi
+ `src/MyWidgetService/Program.cs` – Main entry point for the application
+ `src/MyWidgetService/MyWidgetServiceStack.cs` – Defines the widget service stack

------
#### [ Go ]
+ `my_widget_service.go` – Main entry point for the application and service stack
+ `my_widget_service_test.go` – Sample Go test file

------

Run the app and note that it synthesizes an empty stack\.
Expand Down Expand Up @@ -436,7 +454,56 @@ namespace MyWidgetService
```

------
#### [ Go ]

File: `lib/widget_service.go`

```
package widgetservice

import (
"github.com/aws/aws-cdk-go/awscdk/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway"
"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
"github.com/aws/aws-cdk-go/awscdk/v2/awss3"
"github.com/aws/aws-cdk-go/awscdk/v2/awss3assets"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
)

func WidgetService(scope constructs.Construct, id string) {
this := constructs.NewConstruct(scope, &id)

var bucket = awss3.NewBucket(scope, jsii.String("WidgetStore"), &awss3.BucketProps{})

var handler = awslambda.NewFunction(this, jsii.String("WidgetHandler"), &awslambda.FunctionProps{
Runtime: awslambda.Runtime_NODEJS_16_X(),
Code: awslambda.AssetCode_FromAsset(jsii.String("resources"), &awss3assets.AssetOptions{}),
Handler: jsii.String("widgets.main"),
Environment: &map[string]*string{
"BUCKET": bucket.BucketName(),
},
})

bucket.GrantReadWrite(handler, nil)

api := awsapigateway.NewRestApi(this, jsii.String("widgets-api"), &awsapigateway.RestApiProps{
RestApiName: jsii.String("Widget Service Deployed via Go"),
Description: jsii.String("This service serves widgets."),
})

getWidgetsIntegration := awsapigateway.NewLambdaIntegration(handler, &awsapigateway.LambdaIntegrationOptions{
RequestTemplates: &map[string]*string{
"application/json": jsii.String("{ \"statusCode\": 200 }"),
},
})

api.Root().AddMethod(jsii.String("GET"), getWidgetsIntegration, &awsapigateway.MethodOptions{
ApiKeyRequired: jsii.Bool(false),
})
}
```
------
**Tip**
We're using a `[lambda\.Function](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html)` in to deploy this function because it supports a wide variety of programming languages\. For JavaScript and TypeScript specifically, you might consider a `[lambda\-nodejs\.NodejsFunction](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunction.html)`\. The latter uses esbuild to bundle up the script and converts code written in TypeScript automatically\.

Expand Down Expand Up @@ -525,6 +592,19 @@ new WidgetService(this, "Widgets");

------

#### [ Go ]

File: `my_widget_service.go`

Replace the comment in the constructor with the following line of code\.

```
widgetservice.WidgetService(stack, "Widgets")

```
Ensure `widgetservice` package has been imported with statement: `"my_widget_service/lib"`

------
Be sure the app runs and synthesizes a stack \(we won't show the stack here: it's over 250 lines\)\.

```
Expand Down Expand Up @@ -777,6 +857,22 @@ File: `src/MyWidgetService/WidgetService.cs`

------

#### [ Go ]

File: `lib/my_widget_service.go`

```
widget := api.Root().AddResource(jsii.String("{id}"), nil)

widgetIntegration := awsapigateway.NewLambdaIntegration (handler, nil)

widget.AddMethod(jsii.String("POST"), widgetIntegration, nil) // POST /{id}
widget.AddMethod(jsii.String("GET"), widgetIntegration, nil) // GET /{id}
widget.AddMethod(jsii.String("DELETE"), widgetIntegration, nil) // DELETE /{id}
```

------

Save and deploy the app\.

```
Expand Down