Practice Lab 2: Implementation of cloudwatch

  • Module - 3
  • I have been facing a lot of errors when trying to create the dashboard for the RDS database

4.1 - CloudWatch Showcase

Open a new terminal and go back to the terraform folder:

cd ~/project/terraform

Open the main.tf file from the terraform folder. Uncomment the module named monitoring (lines 12 to 19). Save changes to the file.

Then, in the terraform/modules/monitoring folder, open the cloudwatch.tf file and uncomment only the resource "aws_cloudwatch_dashboard" "rds_dashboard" (lines 1 to 94). You can use hotkeys Ctrl+/ or Cmd+/. Save changes to the file.

In this resource, there is a key named dashboard_body; this is where you define the position, size and content of the dashboard elements. Inspect this key, you will find some widgets of text type, like the title of the dashboard, and other widgets of metric type. Between the metrics that you are going to monitor you can find the CPU Utilization, Free Storage Space, Read/Write IOPS and the number of Database Connections in your RDS. Those metrics are shown as an average over a window of 30 seconds. In your terminal, deploy your new resources with the following commands:

terraform init
terraform plan
terraform apply

After, I got the latest version, I tried to apply the terraform commands, but it didn’t work.

2 - Infrastructure deployment using Terraform

The first step in this lab will be setting up the infrastructure using Terraform. In your terminal, run the following command:

source scripts/setup.sh

Change the working directory to the terraform folder:

cd terraform

and then, run the initialization command:

terraform init

Execute the following command to generate the execution plan:

terraform plan

To deploy infrastructure, run the command

terraform apply

When I applied terraform apply, the terminal closed with exit code 1.

Looking forward to hearing from you.

Best,
Van.

I believe there is an instruction for when that happens, to disable some feature, can you look upwards at the beginning of the Lab script!

1 Like

Fix: “resource already exists” errors in C2_W3_Lab_2 (CloudWatch)

Symptom
terraform apply fails with:

  • InvalidGroup.Duplicate: security group 'de-c2w3lab2-bastion-host-sg' already exists

  • DBSubnetGroupAlreadyExists: 'de-c2w3lab2-db-subnet-group' already exists

The terminal may crash before you can read these errors, just showing that the terminal closed with exit code 1.
**
Why**
AWS still has resources from a previous run, but your current Terraform state doesn’t track them.

You can import those resources into state (best practice) or delete them in AWS and re-apply.


Option A — Import the existing resources (recommended)

These commands are safe one-liners (no subshells) and won’t crash the terminal.

# 0) Go to the lab terraform dir
cd ~/project/terraform

# 1) See what Terraform already tracks (often it's empty or partial)
terraform state list

1) Find existing resource IDs in AWS

# Security group (shows ID + VPC; if multiple rows, pick the VPC used by the lab)
aws ec2 describe-security-groups \
  --filters Name=group-name,Values=de-c2w3lab2-bastion-host-sg \
  --query 'SecurityGroups[].{Id:GroupId,Vpc:VpcId,Name:GroupName}' \
  --output table

# DB subnet group (name is the identifier)
aws rds describe-db-subnet-groups \
  --db-subnet-group-name de-c2w3lab2-db-subnet-group \
  --query 'DBSubnetGroups[0].DBSubnetGroupName' \
  --output text

2) Import them into Terraform state

Replace sg-XXXXXXXX with the ID you saw in the table above.

terraform import 'module.bastion_host.aws_security_group.bastion_host' sg-XXXXXXXX
terraform import 'module.bastion_host.aws_db_subnet_group.database' de-c2w3lab2-db-subnet-group

3) Apply

terraform plan
terraform apply -auto-approve


Option B — Delete the orphaned resources, then apply

# Show existing SGs (so you grab the right ID)
aws ec2 describe-security-groups \
  --filters Name=group-name,Values=de-c2w3lab2-bastion-host-sg \
  --query 'SecurityGroups[].{Id:GroupId,Vpc:VpcId}' \
  --output table

# Delete the SG (replace sg-XXXXXXXX)
aws ec2 delete-security-group --group-id sg-XXXXXXXX

# Delete the DB subnet group
aws rds delete-db-subnet-group --db-subnet-group-name de-c2w3lab2-db-subnet-group

# Re-apply the lab infra
cd ~/project/terraform
terraform apply -auto-approve


Notes & tips

  • If VS Code pops “terminal exited with code 1”, that’s just Terraform exiting non-zero after an error—it’s not a terminal bug.

  • If your SG query returns multiple rows, pick the one in the VPC shown in your terraform plan output.

  • If you prefer to avoid all this, you can also rename the resources in the Terraform module (e.g., add -yourinitials) and apply—but import/delete keeps the names consistent with the lab.

1 Like

Hi gent,
I would like to check the lab instructions, but I have updated the system with latest version since it showed the same error.

Hi Colin,

This is amazing! Thanks for the detailed instructions. I think I’ve found the solution. Here are scenarios in which I faced errors

Have a wonderful day.

Many Thanks,
Van.