Deployment example in the Terraform using AWS ECS.

 



Here is a sample Terraform configuration file for deploying an AWS Elastic Container Service (ECS) cluster and task definition:



provider "aws" {

  region = "us-west-2"

}


resource "aws_ecs_cluster" "example" {

  name = "example-ecs-cluster"

}


resource "aws_ecs_task_definition" "example" {

  family = "example-task-definition"


  container_definitions = jsonencode([

    {

      name: "example-container",

      image: "nginx:latest",

      memory: 128,

      portMappings: [

        {

          containerPort: 80,

          hostPort: 80

        }

      ]

    }

  ])


  requires_compatibilities = ["FARGATE"]

}


resource "aws_ecs_service" "example" {

  name            = "example-service"

  task_definition = aws_ecs_task_definition.example.arn

  cluster         = aws_ecs_cluster.example.id

  desired_count   = 1


  launch_type = "FARGATE"


  network_configuration {

    assign_public_ip = true


    security_groups = [

      aws_security_group.example.id

    ]


    subnets = aws_subnet.private.*.id

  }

}


resource "aws_security_group" "example" {

  name        = "example-security-group"

  description = "Allow HTTP traffic"


  ingress {

    from_port   = 80

    to_port     = 80

    protocol    = "tcp"

    cidr_blocks = ["0.0.0.0/0"]

  }

}


resource "aws_vpc" "example" {

  cidr_block = "10.0.0.0/16"

}


resource "aws_subnet" "private" {

  count = 2


  cidr_block = "10.0.${count.index + 1}.0/24"

  vpc_id     = aws_vpc.example.id

}



This example creates an ECS cluster, task definition, and service using Fargate launch type. The service is configured to use a security group that allows incoming HTTP traffic and is assigned public IP addresses in two private subnets within a VPC.

You can run terraform apply to create the ECS cluster, task definition, and service, and Terraform will output the changes it made to your infrastructure. To delete the resources, you can run terraform destroy.


Deployment example in the Terraform using AWS ECS. Deployment example   in the  Terraform using AWS ECS. Reviewed by Mukesh Jha on 8:32 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.