
Let's say you have the following files on your Terraform server.
├── required_providers.tf
├── elastic_container_services (directory)
│ ├── services (directory)
│ ├── ├── fargate (directory)
│ ├── ├── ├── provider.tf
│ ├── ├── ├── task_definition.tf
required_providers.tf will almost always have this.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.
provider "aws" {
alias = "default"
profile = "default"
region = "default"
}
This assumes you have already:
- Created an Elastic Container Service (ECS) Cluster using Terraform
- Created an Elastic Container Service (ECS) Fargate Task Definition using Terraform
An ECS deployment can be:
- EC2 - apps run in EC2 instance
- FARGATE - serverless (no EC2 instances)
services.tf could have something like this.
resource "aws_ecs_service" "ecs-flask-fargate-service" {
name = "flask-fargate-service"
launch_type = "FARGATE"
cluster = aws_ecs_cluster.my-ecs-cluster.id
task_definition = aws_ecs_task_definition.flask-fargate-task-definition.id
desired_count = 1
load_balancer {
target_group_arn = aws_lb_target_group.ecs-application-load-balancer-target-group.arn
container_name = aws_ecs_task_definition.flask-fargate-task-definition.id
container_port = 80
}
network_configuration {
subnets = [data.aws_subnets.subnets.ids[0],data.aws_subnets.subnets.ids[1]]
security_groups = [data.aws_security_group.ecs-security-group.id]
assign_public_ip = true
}
lifecycle {
ignore_changes = [task_definition, desired_count]
}
}
You may need to reissue the terraform init command.
~]# terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
Terraform has been successfully initialized!
The terraform plan command can be used to see what Terraform will try to do.
~]$ terraform plan
Terraform will perform the following actions:
# aws_ecs_service.ecs-flask-fargate-service will be created
+ resource "aws_ecs_service" "ecs-flask-fargate-service" {
+ cluster = "arn:aws:ecs:us-east-2:123456789012:cluster/my-ecs-cluster"
+ deployment_maximum_percent = 200
+ deployment_minimum_healthy_percent = 100
+ desired_count = 1
+ enable_ecs_managed_tags = false
+ enable_execute_command = false
+ iam_role = (known after apply)
+ id = (known after apply)
+ launch_type = "FARGATE"
+ name = "flask-fargate-service"
+ platform_version = (known after apply)
+ scheduling_strategy = "REPLICA"
+ tags_all = (known after apply)
+ task_definition = "flask"
+ triggers = (known after apply)
+ wait_for_steady_state = false
+ load_balancer {
+ container_name = "flask"
+ container_port = 80
+ target_group_arn = "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/ecs-target-group/94d249d96f071434"
}
+ network_configuration {
+ assign_public_ip = false
+ security_groups = [
+ "sg-0e01a484310ec2f8e",
]
+ subnets = [
+ "subnet-000368eea6348dcd8",
+ "subnet-00a2efcf89006a953",
]
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
The terraform apply command can be used to create, update or delete the resource.
terraform apply -auto-approve
The aws ecs list-clusters command can be used to list your Elastic Container Service (ECS) Clusters.
~]$ aws ecs list-clusters
{
"clusterArns": [
"arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster"
]
}
The aws ecs list-services command can be used to list the ECS Services that have been created. Something like this should be returned.
]$ aws ecs list-services --cluster arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster
{
"serviceArns": [
"arn:aws:ecs:us-east-1:123456789012:service/my-ecs-cluster/flask-fargate-service"
]
}
And the Service should be listed in the AWS ECS console.
Did you find this article helpful?
If so, consider buying me a coffee over at