<aside> πŸ€”

Before we create any Terraform files, here is my planned proejct file structure (hierarchy) for the Terraform project - single environment:

terraform-aws/
β”œβ”€β”€ main.tf	  	  	  	  # calling modules
β”œβ”€β”€ providers.tf	  	  	# declaring different providers (AWS, ...)
β”œβ”€β”€ variables.tf	  	  	# inputs (regions, CIDRs, ...)
β”œβ”€β”€ locals.tf	  	  	    # constants (naming conventions, tags, ...)
β”œβ”€β”€ outputs.tf
β”œβ”€β”€ ...
└── modules/
	  β”œβ”€β”€ **vpc/**
	  β”‚   β”œβ”€β”€ main.tf
	  β”‚   β”œβ”€β”€ variables.tf
	  β”œβ”€β”€ **ecr/**
	  β”‚   β”œβ”€β”€ main.tf
	  β”‚   └── outputs.tf
	  └── **...*other modules*/**
	      β”œβ”€β”€ ...
	      └── ...

</aside>

<aside> ⚠️

With Terraform, you can create new ECR repositories directly with resource "aws_ecr_repository", but once the repositories are not empty (there are images inside), you cannot delete them unless you have force_delete = true

β†’ In this project, I’ll create the ECR repos in the console/CLI instead separately, then reference them in the Terraform project with data "aws_ecr_repository" instead

</aside>

# (Later for every module created, we will need to initialize the Terraform project again. We have already init as instructed above, no need to init now)
terraform init

# Validate and Terraform will print out a success or error message
terraform validate

# Format and Terraform will print out the names of files it modifies
terraform fmt

# Preview the changes Terraform will make to your infrastructure
terraform plan -out tf.plan
# (Optionally if you want to check the plan with a text file)
terraform show -no-color tf.plan > tfplan.txt

# After the plan is verified and ready, apply the configuration
terraform apply "tf.plan"

image.png