Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

Amazon EventBridge Schedule to Amazon Elastic Container Service

Created with SnapAmazon EventBridge SchedulerAmazon Elastic Container Service (ECS)

Creates an EventBridge schedule to invoke an Amazon Elastic Container Service task every 5 minutes

This pattern demonstrates how to invoke an Amazon Elastic Container Service (ECS) task using EventBridge Scheduler and deployed with Terraform.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.64.0"
    }
  }
}

locals{
  region              = "REPLACE_ME_WITH_AWS_REGION"
  container_image     = "amazon/amazon-ecs-sample"
  vpc_cidr            = "REPLACE_ME_WITH_AWS_VPC_CIDRS"
  availability_zones  = "REPLACE_ME_WITH_AWS_AVAILABILITY_ZONES"
  public_subnets      = "REPLACE_ME_WITH_AWS_PUBLIC_SUBNETS"
  private_subnets     = "REPLACE_ME_WITH_AWS_PRIVATE_SUBNETS"
}

provider "aws" {
  region = "${local.region}"

}

### EventBridge schedule setup ###
resource "aws_scheduler_schedule" "serverlessland-eb-ecs-invoke-schedule" {
      name = "serverlessland-eb-ecs-invoke-schedule"
      flexible_time_window {
      mode = "OFF"
  }
    schedule_expression = "rate(5 minute)"
    target {
      arn = aws_ecs_cluster.serverlessland-ecs-test-cluster.arn
      role_arn = aws_iam_role.serverlessland-eventbridge-invoke-ecs-role.arn
      ecs_parameters {
          task_count = 1
          task_definition_arn = aws_ecs_task_definition.serverlessland-ecs-task-definition.arn
          launch_type = "FARGATE"
    
          network_configuration {
                subnets          = aws_subnet.prod-subnet-public-1.*.id
                assign_public_ip = true
                security_groups = [aws_security_group.prod-sg.id]

   }
  }
  }
}

output "ScheduleTargetTask" {
  value = aws_ecs_cluster.serverlessland-ecs-test-cluster.arn
  description = "The ARN of the ecs task being invoked from EventBridge Scheduler"
}
output "ScheduleName" {
  value = aws_scheduler_schedule.serverlessland-eb-ecs-invoke-schedule.name
  description = "Name of the EventBridge Schedule"
}


### VPC, Subnets and SG ###
resource "aws_vpc" "prod-vpc" {
    cidr_block = local.vpc_cidr
    enable_dns_support = "true" #gives you an internal domain name
    enable_dns_hostnames = "true" #gives you an internal host name
    instance_tenancy = "default"    
    
    tags = {
        Name = "prod-vpc"
    }
}

resource "aws_subnet" "prod-subnet-public-1" {
    vpc_id = "${aws_vpc.prod-vpc.id}"
    count = length(local.public_subnets)
    cidr_block =  element(local.public_subnets,count.index)
    availability_zone = element(local.availability_zones,count.index)
    map_public_ip_on_launch = "true" //it makes this a public subnet
    depends_on = [aws_internet_gateway.aws-igw]
    tags = {
        Name = "prod-subnet-public-1"
        Environment = "prod"
    }
}

resource "aws_subnet" "prod-subnet-private-1" {
    vpc_id = "${aws_vpc.prod-vpc.id}"
    count = length(local.private_subnets)
    cidr_block =  element(local.private_subnets,count.index)
    availability_zone = element(local.availability_zones,count.index)
    depends_on = [aws_internet_gateway.aws-igw]
    tags = {
        Name = "prod-subnet-private-1"
        Environment = "prod"
    }
}

resource "aws_security_group" "prod-sg" {
  name   = "prod-sg"
  vpc_id = aws_vpc.prod-vpc.id
   ingress {
    from_port       = 0
    to_port         = 0
    protocol        = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    
  }
  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }
}

### Networking ###
resource "aws_internet_gateway" "aws-igw" {
  vpc_id = aws_vpc.prod-vpc.id
  tags = {
    Name        = "prod-igw"
    Environment = "prod"
    }

}


resource "aws_route_table" "public" {
  vpc_id = aws_vpc.prod-vpc.id

  tags = {
    Name        = "prod-routing-table-public"
    Environment = "prod"
    
  }
}

resource "aws_route" "public" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.aws-igw.id
}

resource "aws_route_table_association" "public" {
  count          = length(local.public_subnets) 
  subnet_id      = element(aws_subnet.prod-subnet-public-1.*.id, count.index)
  route_table_id = aws_route_table.public.id
}


### ECS Cluster ###
resource "aws_ecs_cluster" "serverlessland-ecs-test-cluster" {
  name = "serverlessland-ecs-test-cluster"
  depends_on = [aws_internet_gateway.aws-igw]
  setting {
    name  = "containerInsights"
    value = "enabled"
  }
}

### ECS Task Definition ###
resource "aws_ecs_task_definition" "serverlessland-ecs-task-definition" {
  family                   = "serverlessland-ecs-task-definition"
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  cpu                      = 1024
  memory                   = 2048
  task_role_arn            = aws_iam_role.serverlessland-ecs-task-role.arn
  execution_role_arn       = aws_iam_role.serverlessland-ecs-task-execution-role.arn
  container_definitions    = <

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/eventbridge-schedule-ecs-terraform

Deploy

See the GitHub repo for detailed deployment instructions.


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the stack: terraform destroy

Rajdeep Banerjee

Presented by Rajdeep Banerjee

AWS Sr. Solutions Architect