Open In App

ECS Deployment Using Terraform

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: AWS ECS and Terraform

ECS (Elastic Container Service) is a fully-managed service offered by AWS (Amazon Web Services) that allows users to run, scale, and manage Docker containers. It provides a highly scalable and secure environment for running containerized applications and integrates with other AWS services such as Elastic Load Balancing and Amazon RDS. ECS also supports using Fargate, a serverless compute engine for containers that eliminate the need to manage the underlying EC2 instances.

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It can be used to manage a variety of infrastructure resources, including virtual machines, DNS entries, and databases. Terraform uses a simple, human-readable language called HashiCorp Configuration Language (HCL) to describe infrastructure as code. This allows users to define and provision infrastructure resources in a repeatable and version-controlled manner.

Steps to Deploy ECS using Terraform

Pre-requisite: Install Terraform

Step 1: Create a folder to keep all your terraform files.

Step 2: Create provider.tf, iam.tf, service.tf , vpc.tf, ecr.tf, ecs.tf, service.tf, task-definition.tf

provider.tf

 

vpc.tf

 

iam.tf

 

task-definition.tf

 

service.tf

 

ecr.tf

 

ecs.tf

 

Step 3: Initialize the terraform

$ terraform init

terraform init is a command used in Terraform, a tool for building, changing, and versioning infrastructure safely and efficiently. It is the first command that should be run after writing a Terraform configuration or cloning an existing one.

It initializes a working directory containing Terraform configuration files. It is used to download and install provider plugins, and also to set up the backend for storing the state. It should be run in the same directory where the Terraform configuration files are located.

terraform init

 

Step 4: Run the plan command to check the changes

$ terraform plan

terraform plan is a command in Terraform that shows you what changes will be made to your infrastructure before you actually make those changes. It generates an execution plan that describes the actions that will be taken and the estimated cost of those actions.

You can use the plan command to see what Terraform will do before actually applying the changes, which can be useful for catching errors and understanding the impact of your changes before they are made.

terraform plan

 

Step 5: Create and Update the Infrastructure

$ terraform apply

terraform apply is a command in Terraform that is used to create or update infrastructure according to the Terraform configuration files. It takes the execution plan generated by the terraform plan command and applies the changes to the actual infrastructure.

The apply command reads the configuration files, prompts for any necessary input, and then creates or updates the specified resources. It also updates the state file with the new resource information. It’s important to note that the apply command will make changes to your infrastructure, so you should use caution and review the plan before running it.

You can also use terraform apply -auto-approve this flag will automatically approve all changes, skipping the confirmation prompt.

terraform apply

 

cluster output

 

ecs-service

 

Step 6: Now delete the resources

$ terraform destroy

terraform destroy is a command in Terraform that is used to delete the resources that were created by Terraform. It takes the Terraform configuration files as input and compares the current state of your infrastructure with the state defined in the configuration files. It then generates a plan that describes the changes that need to be made to delete the resources.

The destroy command will prompt for confirmation before making any changes to your infrastructure. Once you confirm, it will delete the specified resources and update the state file to reflect the changes.

It’s important to use caution when running the destroy command, as it will permanently delete resources and data. It’s recommended to review the plan before running it and also to use a version control system to track the changes made by terraform destroy.

You can also use terraform destroy -auto-approve this flag will automatically approve all changes, skipping the confirmation prompt.

terraform destroy

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads