Rafal WilinskiBlogConsulting

June 05, 2020 / 2 minutes / #AWS #Golang #Lambda #CDK

Creating Golang Lambda functions in AWS CDK

Go, known also as Golang, despite being the language of the cloud still is not having proper support in CDK (Cloud Development Kit):

Github feature request for Go (golang) language support

Over 500 programmers expressed the interest, however, it's not there yet. Don't get me wrong, I'm not mad, CDK team is doing amazing job and just I suppose that's because of the fact that their team is rather small and has a lot of other issues to tackle (there are over 1120 of them as I'm writing this article).

As I believe in Open Source, I decided to solve this issue. I pulled the source of aws-cdk and decided to replicate the functionality of NodejsFunction but for Go.

The result of my work is here: aws-lambda-golang-cdk on Github What does it do? It compiles Golang based Lambda functions as a part of CDK synth process.

AWS CDK Golang Construct

Usage of Lambda Golang construct in CDK is very simple:

import * as cdk from '@aws-cdk/core'
import * as apigateway from '@aws-cdk/aws-apigateway'
export class GolangAPI extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
// Define function. Source code should be located in ./test-function/main.go
const api = new apigateway.LambdaRestApi(this, 'goapi', {
proxy: false,
})
const items = api.root.addResource('items')
items.addMethod('GET')
}
}
// Define function. Source code should be located in ./test-function/main.go

First, we define a very simple CDK-powered Stack - a Rest API using API Gateway.

Then, import aws-lambda-golang construct, define a new function using it and plug to API Gateway. And that's it!

If you don't like this animation, head to project Readme

Now, all you need to do is define your Golang function inside test-function directory.

Special kudos go to Matthew Bonig. He not onlt wrote an article about creating CDK constructs which helped me to figure out the JSII part, which made this module compatible with Python, .NET and Java stacks, but also provided a lot of feedback personally.


-- Written by Rafal Wilinski. Founder of Dynobase - Professional GUI Client for DynamoDB


GithubTwitterInstagram