Create a Fargate Service with Terraform

Create a Fargate Service with Terraform

In this post we will use Terraform to create a Fargate Cluster and deploy a service to it. You can follow the video or continue reading.


Create Terraform project

Create a new directory and create a file named in it. Copy/Paste this code in the file. You may adjust the region and availability_zones.

provider "aws" {
    region = "eu-central-1"

module "base-network" {
    source                                      = "cn-terraform/networking/aws"
    version                                     = "2.0.7"
    name_preffix                                = "test-networking"
    vpc_cidr_block                              = ""
    availability_zones                          = ["eu-central-1a", "eu-central-1b"]
    public_subnets_cidrs_per_availability_zone  = ["", ""]
    private_subnets_cidrs_per_availability_zone = ["", ""]

module "ecs-fargate" {
    source  = "cn-terraform/ecs-fargate/aws"
    version = "2.0.17"
    name_preffix        = "test"
    vpc_id              = module.base-network.vpc_id
    container_image     = "nginx"
    container_name      = "test"
    public_subnets_ids  = module.base-network.public_subnets_ids
    private_subnets_ids = module.base-network.private_subnets_ids
    lb_enable_https = false

Create a second file named and copy/paste this lines to it.

output "loadbalancer-address" {
    value = "${module.ecs-fargate.aws_lb_lb_dns_name}"

Initialize Terraform

terraform init

This will install and initialize the AWS Provider.


The AWS resources we create are not free. Be sure to clean them up at the end!

terraform apply

This will create a VPC with public and private subnets. A Fargate Cluster. A load balancer. A Fargate Task and Service with one nginx container. At the end you will see

Apply complete! Resources: 41 added, 0 changed, 0 destroyed.


loadbalancer-address =


You may have to wait some seconds until the nginx service is ready. Then copy the Loadbalancer address from the outputs section above and test the service with:


You should see the default nginx page

<!DOCTYPE html>
<title>Welcome to nginx!</title>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href=""></a>.<br/>
Commercial support is available at
<a href=""></a>.</p>

<p><em>Thank you for using nginx.</em></p>


To delete all resources created use:

terraform destroy


In this post we created a Fargate cluster and deployed a service to it.

comments powered by Disqus