Simplified Cloud Logging: Creating CloudWatch Log Groups Using Terraform - A Comprehensive Guide.

Simplified Cloud Logging: Creating CloudWatch Log Groups Using Terraform - A Comprehensive Guide.


4 min read

Table of contents

No heading

No headings in the article.


CloudWatch, a robust monitoring tool from Amazon Web Services (AWS), empowers users to collect and monitor data from diverse sources, including AWS resources and applications. Terraform, a widely embraced Infrastructure as Code (IAC) tool, simplifies the deployment and management of AWS infrastructure. This tutorial guides you through the deployment of CloudWatch using Terraform, demonstrating how to create, manage, and delete CloudWatch resources through,, and files.


  • Terraform must be installed on your system.

  • AWS account with CloudWatch permissions.

  • AWS account credentials should be configured.

Step 1: Set up Your Environment

To initiate the process, ensure you have an AWS account with the requisite permissions for creating CloudWatch resources. Additionally, install Terraform on your machine. Visit the official Terraform website and download the latest version.

Step 2: Define Your Resources in

In this step, we'll create the CloudWatch log group using Terraform. Begin by establishing a folder named cloudwatch_logs. Inside this folder, create Terraform configuration files:,, and Below is an example file. Copy and paste the provided code into your file:

provider "aws" {
  region = "us-east-1"

resource "aws_cloudwatch_log_group" "log_group" {
  name              = var.log_group_name
  retention_in_days = var.retention_days

resource "aws_cloudwatch_log_stream" "log_stream" {
  name           = "mahira-log-stream"
  log_group_name =

Continue building your Terraform configuration within the specified files to efficiently manage your CloudWatch resources. This foundation will enable you to easily scale and modify your AWS infrastructure as needed.

Step 3: Define Your Variables in

In this crucial step, we establish the variables that will be utilized to create the CloudWatch log group. Open the file and define the variables according to your specific needs. Here's an example file:

variable "log_group_name" {
  description = "Name of the CloudWatch Log Group"
  type        = string
  default     = "MyCloudWatchLogGroup"

variable "retention_days" {
  description = "Retention period for CloudWatch logs (in days)"
  type        = number
  default     = 7

Feel free to customize the variable names, descriptions, types, and default values to align with your particular use case.

This setup allows you to easily adjust and manage your CloudWatch log group by modifying these variables in a centralized manner, providing flexibility and efficiency in your infrastructure management with Terraform.

Step 4: Define Your Outputs in

In this step, we articulate the outputs of the Terraform module. This is particularly useful when there is a need to reference the CloudWatch log group in another module or resource. Here's an example file:

output "log_group_name" {
  description = "Name of the created CloudWatch Log Group"
  value       =

output "log_group_arn" {
  description = "ARN of the created CloudWatch Log Group"
  value       = aws_cloudwatch_log_group.example.arn

These outputs provide essential information about the created CloudWatch Log Group, such as its name and Amazon Resource Name (ARN). You can adapt and extend these outputs based on the specific requirements of your infrastructure.

By defining outputs, you establish a convenient way to reference and utilize information from your CloudWatch log group in subsequent Terraform modules or resources. This enhances the modularity and extensibility of your Terraform configuration.

Step 5: Initialize and Apply the Terraform Configuration

With your variables, resources, and outputs defined, proceed to initialize and apply the Terraform configuration. Open a terminal window, configure your AWS credentials, and navigate to your cloudwatch_logs folder. Run the following commands:

terraform init
terraform plan
terraform apply
  • The terraform init command initializes your Terraform environment.

  • The terraform plan command provides a preview of the changes to be made.

  • The terraform apply command executes the Terraform configuration, creating the CloudWatch log group.

Step 6: Verify the CloudWatch Log Group

Verify the successful creation of the CloudWatch log group. Log in to the AWS console, navigate to the CloudWatch service, and confirm the presence of the newly created log group.

By following these steps, you have effectively utilized Terraform to deploy a CloudWatch log group, streamlining the process of monitoring and collecting data from your AWS resources and applications. This setup enhances your infrastructure management with the power and flexibility of Infrastructure as Code.


In this tutorial, we have provided a comprehensive guide on deploying CloudWatch using Terraform. By leveraging the capabilities of,, and files, we demonstrated how to efficiently manage and automate the deployment of CloudWatch resources within your AWS infrastructure.

Terraform proves to be a powerful tool, enabling the automation of deployment, configuration, and management processes for CloudWatch resources. Through the utilization of Infrastructure as Code (IAC) principles, this approach not only saves time but also reduces the likelihood of errors in the deployment process.

By following this tutorial, you have gained valuable insights into the seamless integration of Terraform with CloudWatch, empowering you to enhance the efficiency of your AWS infrastructure management. This automation-driven approach lays the foundation for scalable, error-resistant, and easily maintainable CloudWatch deployments within your AWS environment.

I hope this helps, you!!

More such articles:

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!