CDKTF and AWS ECS Baby steps

Rafael Costa
4 min readFeb 23, 2024

--

Infrastructure as Code (IaC) is a key practice in the DevOps world, which brings significant benefits:

  • Consistency: IaC helps in maintaining consistency across environments, reducing the chances of any discrepancies.
  • Speed: IaC allows rapid provisioning and deployment of infrastructure, which can significantly reduce the time to market for software products.
  • Cost-Efficiency: With IaC, you can manage and utilize resources more effectively, leading to cost savings.
  • Risk Reduction: IaC enables version control for infrastructure setup, which can help in easy rollback and reduces the risk of downtime.

One of the tools that enable IaC is Terraform. However, Terraform uses a declarative language, which can be a bit challenging to use. This is where CDK for Terraform (CDKTF) comes in. CDKTF is an open-source software development framework to define cloud infrastructure and provision it through HashiCorp’s Terraform.

CDKTF allows you to use familiar procedural programming languages to define and provision cloud infrastructure. It brings the power of familiar programming languages to the Terraform ecosystem, improving efficiency, reducing errors, and improving the overall developer experience.

In this guide, we will put these concepts into practice by creating a project using CDKTF for Golang and AWS ECS.

Project Idea

We going to create a simple project to masterise the knowledge about CDKTF. With this idea in mind, we will be using CDKTF Golang to create an AWS structure with ECS (Elastic Container Service) to deploy a simple nginx container. This approach follows AWS best practices, ensuring a robust and efficient deployment following this diagram.

Figure 1

You can find the complete project in this repository.

What is ECS?

ECS, or Elastic Container Service, is a highly scalable, high-performance container orchestration service that supports Docker containers and allows you to easily run and scale containerized applications on AWS. ECS eliminates the need for you to install, operate, and scale your own cluster management infrastructure, providing you with a seamless deployment experience.

Some of the advantages of ECS over other approaches include:

  • Deep AWS integration: ECS works seamlessly with other AWS services such as ECR, RDS, and ELB.
  • Scalability: With ECS, you can scale your applications in minutes with high efficiency.
  • Security: ECS ensures that your applications are secure and meet compliance requirements.

Get started

Before running this project, you need to configure your environment. Here are the steps:

git clone https://github.com/ralvescosta/cdktf-hello-world
  • Get the pre-built AWS go files
cdktf get
  • Now you need to update the keys AWS_REGION, and TERRAFORM_CLOUD_ORGANIZATION, int the .env file, based on you AWS account and Terraform integration.
  • Now you can run the CDKTF plan to check if everything is right in place
GO_ENV=staging cdkrf plan
  • With everything configured we can run the deploy command and in the AWS console check the Application Load Balance DNS to access the Nginx welcome page.
GO_ENV=staging cdktf deploy

If everything went well you will see in the Terraform cloud all the resources that was configured.

Figure 2

Be careful at this point, because in this project there are two NAT Gateways, and these are expensive resources in AWS, so pay attention in your bill limits.

Conclusion

Terraform is a powerful tool to help us create and maintain our infrastructure with cloud code and we can use these benefits with the convenience of our programming languages ​​through CDKTF.

CDKTF can be software engineers’ best friend as it provides a way to automate cloud infrastructure in an easy and declarative way.

I hope everyone enjoyed the project, see you soon.

--

--

Rafael Costa

Backend Engineer— [GoLang] [Rust] [IoT] [IIoT] [NodeJs]