In this project, you will provision an Amazon ECS Cluster into an existing Amazon VPC.
- 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
- Amazon Elastic Container Service
- Terraform
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
The variables.tf file defines the name, description, and expected data type for each variable referenced in the main.tf file.
This file will include the actual values for each variable. These values have been retrieved from the existing infrastructure.
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!
In this step, you will access your IDE and configure Terraform with the AWS provider and credentials.
-
open your IDE(example VScode) in your local machine.
-
At the top of the IDE, click Terminal, then click New Terminal:
-
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
- In the terminal, enter aws configure list to confirm your credentials have been set properly:
aws configure list
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" {}
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