Initialization
Whenever you create new Terraform project you will need to initialize it first. \ You will need to setup your provider (in this case AWS)
Work directory & files
Create following files:
main.tf
. It will have all basic configuration for your project.variables.tf
file. This file will hold all variables.ecs.tf
file. This file will have the ECS part of your project.alb.tf
file. This file will have the Application LoadBalancer configuration.security_groups.tf
file. This will have all AWS Security Groups that will be used in ECS and ALB.iam.tf
file. IAM will have the roles needed for ECS to run and be able to retrieve images from ECR.output.tf
file. This file will have all the data that Terraform will output when it finished running.
Terraform configuration
Usually provider and Terraform configuration are done in your main.tf
file.
- Tell Terraform to use AWS provider version 3.27
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 3.27" } } }
- Set minimum required Terraform version to
0.14.9
. This should go to theterraform
block.required_version = ">= 0.14.9"
Provider configuration
- Create AWS provider
provider "aws" { }
- Assign these values to your provider configuration block:
profile = "default"
region = "eu-west-1"
Initializing Terraform
We need to initialize Terraform to download all required modules and packages to be used. You need to re-initialize every time you touch one of the modules or change versions etc...
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 3.27"...
- Installing hashicorp/aws v3.38.0...
- Installed hashicorp/aws v3.38.0 (self-signed, key ID 34365D9472D7468F)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Help
terraform/main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "default"
region = "eu-west-1"
}