CDK your way to the perfect infrastructure

By Cezar Al Ashkar
Published on: June 27, 2024

As far as any developer or cloud architect knows, there are three main ways of deploying infrastructure. The first way, via the console, is the easiest yet the most time-consuming. The second way, which is through the CLI, has more granular control and can be very challenging. Last but not least, deploying using code is every developers dream.

What is IaC?

Infrastructure as code is your ticket to impressing your manager, your tech-y crush, or your colleagues. Instead of taking note of everything you’re deploying, keeping track of your service permissions, navigating through 30 maybe 40 tabs on your browser/console to make sure you have done everything right (based on a true story), you can rest easy with your IaC.

Services.

If you have never heard of IaC before, you might be wondering what services are commonly used to deploy your infrastructure.

On AWS, the main IaC service is CloudFormation (CF), where templates are written in either JSON or YAML, then deployed through the console or CLI. CloudFormation has some cool features like:

  • Stack Sets: which is a feature that allows you to create, update, and delete AWS CloudFormation stacks in multiple AWS accounts and multiple AWS regions within each AWS account, to change sets, which are generated by CloudFormation by comparing your stack with the changes you submitted.
  • CloudFormation Drift (my favorite feature): Discover what service had a configuration change after deploying your infrastructure, this would help you make sure your template is still within the rails, and would protect you from some of the “i wanted to know what this does” interns.

Terraform is as a 3rd party service that highlights “cloud-agnostic development”. Therefore, learning Terraform can land you a job in companies using any cloud platform and not only AWS.

Next up is my favorite service of all, the AWS Cloud Development Kit (CDK).

Development Kit, Cloud Development Kit.

As a passionate developer, I was swinging my legs back and forth when i discovered CDK. All the possibilities of leveraging this tool swirled through my mind.

AWS created CDK, that uses CloudFormation in its backend, and gave developers every reason to be comfortable with IaC.

Are you uncomfortable with JSON or YAML or declarative code in general? Go ahead, write IaC in TypeScript, JavaScript, Python, Java, C#, . Net, and Go. Finding it hard to start learning? Here you go, take this cornucopia of documentation, CDK Documentation.

Do you fear that your application might fall behind because of changes to AWS services? AWS CDK is regularly updated by AWS to reflect new services and features.

Now to delve into the details of some fascinating CDK commands and features:

  • CDK synth: Use this command to change your code from whatever language you’re using to YAML so it can be deployed on CloudFormation.
  • CDK deploy: Warning! Use this command in a development environment only! The following code, if applicable to your templates, deliberately introduces drift into your CloudFormation stacks in order to speed up deployments:

				
					CDK deploy --hotswap
				
			
  • The CDK CLI: It will use AWS service APIs to directly make the changes if possible; otherwise, it will fall back to performing a full CloudFormation deployment.
  • CDK watch: This command brings a similar concept to the table. With this, each save to your code, your stack is deployed. In the case where auto-save is on, your infrastructure is up-to-date almost all the time as CDK is ‘always watching’.

Wrapping up.

In conclusion, cloud infrastructure deployment offers a spectrum of methods catering to various levels of expertise and convenience. Whether navigating the AWS console, mastering the CLI, or leveraging the power of Infrastructure as Code, each approach presents its unique set of advantages and challenges.

Prominent IaC tools and services such as AWS CloudFormation, Terraform, and AWS Cloud Development Kit (CDK) each bring distinct benefits to the table. CloudFormation offers robust features that facilitate infrastructure management across multiple AWS accounts and regions. Terraform’s cloud-agnostic nature and powerful HCL language make it an invaluable tool for multi-cloud environments. Additionally, CDK empowers developers to write infrastructure code in familiar programming languages, bridging the gap between development and operations.

As a developer you truly are free to use whatever you want, but try using CDK, as i assure you, if you haven’t tried it yet, you’ll be surprised with how familiar it feels.

Tags

Related articles

Digico Solutions