Amazon Web Services (AWS) - Create Cloudwatch Alarm Log Group using Terraform
by
Jeremy Canfield |
Updated: September 19 2023
| Amazon Web Services (AWS) articles
Let's say you have the following files on your Terraform server.
├── required_providers.tf
├── cloudwatch (directory)
│ ├── log_groups.tf
│ ├── provider.tf
required_providers.tf will almost always have this.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.
provider "aws" {
alias = "default"
profile = "default"
region = "default"
}
And log_groups.tf could have the following.
resource "aws_cloudwatch_log_group" "my_log_group" {
name = "my_log_group"
tags = {
Name = "my_log_group"
}
}
You may need to reissue the terraform init command.
~]# terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
Terraform has been successfully initialized!
The terraform plan command can be used to see what Terraform will try to do.
~]$ terraform plan
Terraform will perform the following actions:
# aws_cloudwatch_log_group.my_log_group will be created
+ resource "aws_cloudwatch_log_group" "my_log_group" {
+ arn = (known after apply)
+ id = (known after apply)
+ name = "my_log_group"
+ name_prefix = (known after apply)
+ retention_in_days = 0
+ skip_destroy = false
+ tags = {
+ "Name" = "my_log_group"
}
+ tags_all = {
+ "Name" = "my_log_group"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
The terraform apply command can be used to create the log group.
terraform apply -auto-approve
Did you find this article helpful?
If so, consider buying me a coffee over at