
AWS CDK GoLang is a game-changer for cloud development, allowing you to define cloud infrastructure in code using your preferred programming language.
With AWS CDK GoLang, you can create cloud resources such as EC2 instances, S3 buckets, and RDS databases with just a few lines of code.
This is particularly useful for developers who are already familiar with GoLang and want to leverage their existing skills to build cloud-native applications.
By using AWS CDK GoLang, you can write code that is not only concise but also reusable and maintainable.
A unique perspective: Aws Cdk S3 Bucket
Getting Started
AWS CDK for GoLang is an open-source framework that allows you to define cloud infrastructure in code using the Go programming language.
You'll need to install the AWS CDK CLI and the Go CDK package to get started. The AWS CDK CLI is a command-line tool that allows you to manage your CDK projects, while the Go CDK package provides the necessary libraries to write CDK code in Go.
To install the AWS CDK CLI, run the command `go install github.com/aws/aws-cdk-go/awscdk/v2/cmd/...` in your terminal.
Broaden your view: Azure Cdk
Directory Structure on Initialization
When you run cdk init app --language go, a specific directory structure is created. The directory structure for Go is much simpler and has no hierarchical structure.
In the case of TypeScript, the bin, lib, and test directories are separated. This is because modules are installed based on package.json at the same time as init.
The flat directory structure often used when developing in Go is likely the reason for this simplicity. I've noticed this structure is also beneficial for small to medium-sized projects.
A unique perspective: Go vs Golang
File Contents at Start
When you start with AWS CDK and Go, you'll notice that only two Go files are generated: cdk-go.go and cdk-go_test.go.
The initial code of cdk-go.go is quite straightforward, with the NewXxxStack function defining the stack and the main function generating the stack. You'll also find a defer jsii.Close() statement in the main function to ensure the CDK application cleans up automatically.
If you're curious about the test codes at initialization, you can take a look at the example provided in the article.
The L1 Construct allows you to build resources in a way that's consistent with CloudFormation, making it a powerful tool for infrastructure as code.
Here's a brief comparison of CDK code and CloudFormation YAML:
Core Concepts
CDK is built on top of Layer-1 and Layer-2 constructs, which anyone can define their own constructs on top of, called Layer-3 (and above) constructs.
These constructs are the building blocks of CDK, allowing users to create custom components that can be used to define infrastructure. With AWS CDK, you can define your infrastructure in code, making it easier to manage and maintain.
The core concepts of CDK provide a solid foundation for building custom infrastructure components, enabling you to create reusable and modular code.
Readers also liked: Create S3 Bucket Aws
More Than Just CloudFormation
AWS CDK is more than just a generator for CloudFormation stacks. It defines a higher abstraction layer, which is the app, and can contain multiple stacks.
A CDK app can have properties that don't translate to settings in the synthesized CloudFormation stack, but instead influence the behavior of API calls made by the cdk tool. For example, the TerminationProtection prop has no effect on the synthesized CloudFormation output, but affects the API call made during deployment.
You might like: Aws Api S3

CDK is a toolkit for the entire infrastructure provisioning lifecycle, not just a fancy generator for CloudFormation templates. This makes it more suitable for admins, cloud operators, and devs who want to provision and manage cloud infrastructure easily.
There's no API or programmatic way to replicate the functionality of cdk deploy from within a CDK program, it's only available via the CLI. This means users of your CDK programs need to have the CDK CLI installed to fully utilize the higher CDK abstraction layers.
You can synth CloudFormation stacks within a CDK program and deploy them via the AWS Go SDK, but you'd lose the functionality around the higher abstraction layers or need to reimplement it.
Check this out: Aws Cloud Foundry
About
Core concepts are the foundation of any successful project or endeavor. They're the building blocks that help us understand how things work and how to achieve our goals.
One of the most important core concepts is the idea that every system has its own unique set of rules and constraints. For example, a computer system has rules like binary code and a mobile app has rules like user interface design.
Understanding these rules and constraints is crucial for making progress and achieving success. It's like trying to build a puzzle without knowing the shape of the pieces – it's just not going to work.
A key concept in achieving success is the idea of feedback loops. Feedback loops are the process of receiving information about how our actions are affecting the system and making adjustments accordingly. This helps us learn and improve over time.
Feedback loops are essential for continuous improvement and are often used in fields like business, education, and personal development. By incorporating feedback loops into our work, we can refine our approach and achieve better results.
The concept of systems thinking is also critical in understanding core concepts. Systems thinking involves looking at the big picture and understanding how all the different parts of a system interact with each other. It's like trying to understand how a car works by looking at just one part – it's not very helpful.
Systems thinking helps us identify the complex relationships between different components and how they impact the overall system. By applying systems thinking, we can make more informed decisions and achieve better outcomes.
Related reading: Which One Is Better Aws or Azure
Go Specifics
Go's package management system is perfectly suited for CDK constructs, eliminating the need for a package manager and repository to publish or reference CDK constructs.
You can import Go modules directly from GitHub, and the Go tooling protects you from some types of supply chain attacks using the public Go Proxy and Sum DB.
To specify parameters in Go, you need to use functions like jsii.Number and jsii.String, which return a pointer to the variable, because Go doesn't have a null-allowed type.
Here are the four prerequisites to get up to speed with CDK in Go:
- a good understanding of the AWS services you are about to use
- a basic understanding of AWS CloudFormation and how to troubleshoot its deployments
- a good understanding of the Go programming language (and tooling)
- a week or two of intensive practice
It takes a week or two of intensive practice to gain better velocity and build new muscle memory when writing CDK code in Go.
Go - A Recent Brown Bag Talk
I recently attended a CDK intro session at David Jones and Country Road group, where I presented a demo project written in Go. The organisation predominantly uses CloudFormation, Go, and Python, so I chose to demonstrate CDK with Go to remove the programming language barrier for my audience.
For more insights, see: Golang Go

The experience was a bit clumsy at first, especially with my rusty Go skills. I found myself having to use jsii functions to convert primitive data types, which made the experience less enjoyable.
To get up to speed with CDK, I listed 4 prerequisites: a good understanding of the AWS services you are about to use, a basic understanding of AWS CloudFormation and how to troubleshoot its deployments, a good understanding of the programming language (and tooling) you choose to write the CDK code, and a week or two of intensive practice.
Here are the 4 prerequisites in detail:
- a good understanding of the AWS services you are about to use
- a basic understanding of AWS CloudFormation and how to troubleshoot its deployments
- a good understanding of the programming language (and tooling) you choose to write the CDK code
- a week or two of intensive practice
Overall, writing CDK code in Go is close to writing it in TypeScript, with the VS Code IntelliSense working fairly well with CDK Go packages, and the CLI experience being identical.
Pascal Case in Module Names
In Go, public (out-of-package) calls can only be invoked with Pascal case.
Go's language specification requires this naming convention for public calls.
In the CDK module, field and method names are Pascal case, starting with a capital letter.
This is consistent with the Go language specification.
Field and method names in the CDK module are not in CamelCase, unlike in the TypeScript version.
In private (properly, a package) scope, Go code uses CamelCase.
I've noticed that following these naming conventions helps avoid confusion when working with Go modules.
Go Package Management
Go's package management system is a pro, especially when compared to the JavaScript ecosystem. It uses modules, packages, and multi-module workspaces in a way that's perfectly suited for the language.
You don't need a package manager and package repository to publish or reference CDK constructs with Go. This is because a construct is just a Go package in a Go module, which is easily reusable by Go's design.
Go modules can be imported directly from GitHub, and the Go tooling protects you from at least some types of supply chain attacks with the help of the public Go Proxy and Sum DB.
With TypeScript, you need to deal with npm or yarn, which is a hassle compared to Go's simplicity.
Comparison with TypeScript
When working with AWS CDK in Go, you'll notice some key differences compared to the TypeScript version.
The Go version has a unique function called env that returns an awscdk.Environment structure with Account and Region. This is a useful feature to know about.
One notable feature introduced in Go version 1.18 is Workspaces mode, which allows for easier multi-module development. This is a game-changer for larger projects.
For your interest: Golang Version Manager
Best Practices
As you start building your AWS CDK GoLang project, it's essential to follow best practices to ensure a smooth and efficient development process.
Always use the latest version of the AWS CDK GoLang toolkit to take advantage of the latest features and bug fixes. This will help you avoid compatibility issues and ensure your code is up-to-date.
Code organization is critical in large projects. Use modules and packages to keep your code structured and easy to maintain, just like in the example of the `stack.go` file where we defined a separate module for the stack resources.
By following these best practices, you'll be able to build robust and scalable AWS CDK GoLang applications that are easy to maintain and extend.
No Escape Hatch

CDK for Go doesn't currently support the "escape hatch" concept, which allows you to modify an L2 construct's underlying CFN Resource.
You have to create resources through L1 construct if you want to specify a parameter that can't be specified in L2 Construct.
The escape hatch is a way to access features that are supported by CloudFormation but not yet supported by CDK.
Unfortunately, the CDK for Go doesn't support this functionality yet, so you're stuck using L1 Construct for now.
You can follow progress on support for CDK escape hatches in Go on this GitHub issue.
The jsii.Get method can be used as a workaround, but it's deprecated, so use it at your own risk.
Snapshot Test
The snapshot test is a crucial part of CDK testing. You'll likely encounter it frequently.
This test generates a template from a stack and passes the JSONized version to cupaloy.SnapshotT to execute the snapshot. It's a straightforward yet effective way to verify your code's functionality.
The snapshot test is a great way to catch any unexpected changes in your code's output. By comparing the expected and actual snapshots, you can quickly identify issues and make necessary adjustments.
Gotchas
Debugging can be a real challenge, especially when you're stuck with cached state from a previous non-functional bootstrap binary. This can happen when you're working with CDK, and it's not uncommon to spend hours trying to figure out the issue.
To avoid this gotcha, make sure to destroy the current stack with `cdk destroy` as a first step. This will help you start fresh and ensure that you're not working with outdated or corrupted state.
Deleting the `/cdk.out` directory is also crucial, as it can contain cached data that's causing the problem. Simply deleting this directory will give you a clean slate and allow you to redeploy successfully.
Here's a quick summary of the steps to take:
- Destroy the current stack with `cdk destroy`.
- Delete the `/cdk.out` directory.
- Redeploy using `cdk deploy`.
By following these simple steps, you'll be able to overcome the cached state gotcha and get back to work on your CDK project.
Deploy and Test
Working in VSCode with the CDK module is a joy, with auto-completion/Intellisense working very well.
You can discover what you need by just hitting CTRL+SPACE and letting Intellisense guide you.
To list the stacks of your deployment, you can use a command to view its details.
Destroying your deployment is as simple as running a command to remove it.
For your interest: Golang Test Command
CDK Go Ecosystem
The CDK Go Ecosystem is a vibrant community of developers and contributors who are building and maintaining a wide range of libraries and tools for AWS CDK in Go.
One of the key benefits of the CDK Go Ecosystem is its ability to integrate with other AWS services, such as AWS Lambda and Amazon S3.
The CDK Go Ecosystem is designed to be highly extensible, allowing developers to easily add new features and functionality to their CDK applications.
Some of the most popular libraries in the CDK Go Ecosystem include the aws-cdk-go library, which provides a set of pre-built constructs for working with AWS services.
The CDK Go Ecosystem also includes a number of third-party libraries and tools, such as the cdk8s library, which provides support for Kubernetes.
Developers can use the CDK Go Ecosystem to build a wide range of applications, from simple web servers to complex enterprise systems.
The CDK Go Ecosystem is constantly evolving, with new libraries and tools being added all the time.
For your interest: New Relic and Aws
Working with Resources
To create a folder structure for your AWS CDK project, you'll need to create a Go module and empty Go files. This gives you a folder structure like `cdk-app` and `lambda-func`, which should be opened separately in VSCode to avoid issues with the Go tooling.
Having multiple Go modules in your project can be a bit of a challenge, but with Go 1.18, you can use the new multi-module workspaces feature by creating a `go.work` file in the project root. This allows you to open the project root dir in VSCode without causing issues.
A fresh viewpoint: Aws S3 Remove Folder
Change and Redeploy Go Lambda Logic
You can hot swap your Go Lambda logic with a single command, which can save you a lot of time.
This command is used when you make a small change to your code and don't need to redeploy your entire infrastructure.
As long as you've recompiled your Golang binary bootstrap, this command will swap out the code quickly for you.
In my experience, this process can be quite fast, taking only 27 seconds to complete.
Intriguing read: S3 Command Line Aws
Using Multiple Resources Together
When working with multiple resources, it's essential to understand how to integrate them seamlessly. In our case, we're creating a Lambda function that will be called echo, which will be a simple HTTP handler that returns information from the original HTTP request.
This Lambda function is wired to API Gateway, which allows it to handle GET requests on the / path. On line 32, we add a route to the API and make the Lambda function the handler for it.
We can also create a Cloudformation output to print the base URL of the API Gateway to the terminal after deployment. This is done by wiring a method from the API Gateway object to the Layer-1 construct CfnOutput.
The cdk-app and lambda-func folder should be opened separately in VSCode to avoid issues with the Go tooling. Alternatively, with Go 1.18, we can use the new multi-module workspaces feature by creating a go.work file in the project root.
A quick note: the new multi-module workspaces feature requires at least Go 1.18, which is currently in Beta.
Curious to learn more? Check out: Golang Rest
How It Works
AWS CDK is built on top of Layer-1 and Layer-2 constructs, which can be used to define custom constructs in any supported language. This allows developers to extend the functionality of CDK in a flexible and modular way.
Developers can define their own constructs on top of the existing ones, creating a hierarchy of constructs that can be used to build complex cloud infrastructure. These custom constructs are referred to as Layer-3 and above constructs.
The AWS CDK is designed to be extensible and customizable, enabling developers to create their own abstractions and frameworks on top of the core constructs.
Sits at CloudFormation Center
AWS CDK sits at the center of Cloudformation, making it a powerful tool for infrastructure provisioning. CDK isn't just a generator for Cloudformation stacks, but also interfaces with Cloudformation in the other direction.
CDK can source existing Cloudformation templates and work with them programmatically, making it a great migration and modernization tool. This feature is explored in more detail in another part of this article series.

You can compare your current codebase with what's running, thanks to CDK's ability to work with existing Cloudformation templates. This is especially useful for identifying areas that need improvement or updates.
CDK defines a higher abstraction layer than just Cloudformation stacks, which is why it's more than just a fancy generator for Cloudformation templates.
How JSII Works
JSII is a tool used to generate packages/modules for languages other than TypeScript.
These packages embed a JavaScript runtime in the target language, allowing them to execute the original TypeScript code.
In TypeScript, constructs are classes, but in other languages like Go, constructs are interface types with a factory function.
JSII is used to make Layer-1 and Layer-2 constructs available across multiple languages.
The code for a Lambda function in Go will be placed in a file called main.go, and its structure is different from the TypeScript version.
Core Concepts & How It Works
AWS CDK is built on top of two layers of abstraction: Layer-1 and Layer-2 constructs. Anyone can define their own constructs on top of these layers in supported languages, known as Layer-3 (and above) constructs.

CDK's core concept is to provide a way for developers to define infrastructure as code, making it easier to manage and version their infrastructure. This is achieved through a set of pre-built constructs that can be used to define resources such as AWS services and their dependencies.
These constructs are designed to be modular and reusable, allowing developers to build complex infrastructure stacks from smaller, more manageable pieces. This modularity also enables the creation of custom constructs that can be shared across teams and projects.
CDK's abstraction layers provide a clear separation of concerns, making it easier to understand and work with the underlying infrastructure. This separation also enables better testing and validation of the infrastructure code.
Featured Images: pexels.com

