Skip to content

Instantly share code, notes, and snippets.

@jmlrt
Created December 7, 2018 14:00
Show Gist options
  • Save jmlrt/180bde0ab1a1ce239a642b94ffd707e6 to your computer and use it in GitHub Desktop.
Save jmlrt/180bde0ab1a1ce239a642b94ffd707e6 to your computer and use it in GitHub Desktop.
ECS Fargate sample topology
provider "aws" {
region = "${var.region}"
}
resource "aws_ecs_cluster" "ecs_cluster" {
name = "${var.cluster_name}"
}
resource "aws_ecs_task_definition" "task" {
family = "${var.service_name}"
container_definitions = "${data.template_file.task_def.rendered}"
cpu = "${var.cpu}"
memory = "${var.memory}"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
execution_role_arn = "${var.iam_role_arn}"
}
resource "aws_ecs_service" "service" {
name = "${var.service_name}"
cluster = "${aws_ecs_cluster.ecs_cluster.name}"
task_definition = "${aws_ecs_task_definition.task.arn}"
desired_count = 1
launch_type = "FARGATE"
network_configuration {
assign_public_ip = "true"
security_groups = ["${var.security_group}"]
subnets = ["${var.subnet}"]
}
}
data "template_file" "task_def" {
template = "${file("task_def.json")}"
vars {
name = "${var.service_name}"
image = "${var.image}"
port = "${var.port}"
}
}
[
{
"name": "${name}",
"image": "${image}",
"networkMode": "awsvpc",
"portMappings": [
{
"containerPort": ${port},
"hostPort": ${port}
}
]
}
]
variable "region" {
default = "us-east-1"
}
variable "cluster_name" {
default = "test"
}
variable "service_name" {
default = "test"
}
variable "cpu" {
default = 256
}
variable "memory" {
default = 512
}
variable "image" {}
variable "port" {}
# AWS REQUIRED RESOURCES
variable "iam_role_arn" {}
variable "security_group" {}
variable "subnet" {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment