Skip to content

iamtruptimane/provisioning-ESC-cluster-using-terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Provisioning ESC-cluster using terraform

In this project, you will provision an Amazon ECS Cluster into an existing Amazon VPC.

Project objectives:

  • Define a Terraform module that deploys Amazon ECS resources
  • Apply an Auto Scaling Group Policy to respond to ECS metrics
  • Deploy an Amazon ECS Cluster into an existing Amazon VPC using Terraform

Prerequisites

  • Amazon Elastic Container Service
  • Terraform

environment before

environment after

Existing Infrastructure

The following resources you should deployed before starting this project and it will be referenced in your ECS Cluster:

  • 1 Virtual Private Cloud
  • 2 Public Subnets
  • 2 Private Subnets
  • Public-facing Application Load Balancer
  • Internal-facing Application Load Balancer

Terraform configuration file

variables.tf and main.tf:

The variables.tf file defines the name, description, and expected data type for each variable referenced in the main.tf file.

terraform.tfvars file:

This file will include the actual values for each variable. These values have been retrieved from the existing infrastructure.

outputs.tf file:

The outputs.tf file defines the expected output values for the deployment. In this project, the CloudWatch Log Group names and the ECS Cluster ARN will be output after a successful deployment.

let's start the project!

Step 1: Configure Terraform AWS Credentials

In this step, you will access your IDE and configure Terraform with the AWS provider and credentials.

  1. open your IDE(example VScode) in your local machine.

  2. At the top of the IDE, click Terminal, then click New Terminal:

  3. Run the following commands to configure your AWS credentials:

aws configure set aws_access_key_id <Your_aws_access_key> &&
aws configure set aws_secret_access_key <Your_aws_secret_access_key> &&
aws configure set default.region us-west-2
  1. In the terminal, enter aws configure list to confirm your credentials have been set properly:
aws configure list

Step 2: create main.tf file

In this file add terraform block and provider block

provider "aws" {
  region = "us-west-2"
}

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.58.0"
    }
  }
  required_version = ">= 1.0"
}

# Data
data "aws_region" "current" {}

Step 3: create variables.tf file

add the following piece of code in the file

variable "app_name" {
  description = "Application Name"
  type        = string
}
variable "ecs_role_arn" {
  description = "IAM Role for ECS"
  type        = string
}
variable "ecs_services" {
  type = map(object({
    image          = string
    cpu            = number
    memory         = number
    container_port = number
    host_port      = number
    desired_count  = number
    is_public      = bool
    protocol       = string
    auto_scaling = object({
      max_capacity     = number
      min_capacity     = number
      cpu_threshold    = number
      memory_threshold = number
    })
  }))
}
var