-
Notifications
You must be signed in to change notification settings - Fork 27
Add support for FARGATE #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ desired_capacity_on_demand = 2 | |
ec2_key_name = "key-name" | ||
instance_type = "t2.micro" | ||
minimum_healthy_percent_webapp = 50 | ||
|
||
launch_type = "EC2" # can also be "FARGATE" | ||
## | ||
# This is a sample (public) Docker image from which can be accessed at https://github.com/docker-training/webapp | ||
# This sample image utilizes Flask and it's not RECOMMENDED to run it directly in production (performance degradation) | ||
|
@@ -20,7 +20,7 @@ webapp_docker_image_name = "training/webapp" | |
webapp_docker_image_tag = "latest" | ||
|
||
## | ||
# These variables are required, please fill it out with your environment outputs | ||
# These variables are required, please fill it out with your environment outputs or use remote state references | ||
## | ||
sg_webapp_albs_id = "sg-12345678" | ||
sg_webapp_instances_id = "sg-23456789" | ||
|
@@ -29,3 +29,4 @@ subnet_ids = "subnet-34567890,subnet-4567890a" | |
|
||
ecs_instance_profile = "arn:aws:iam::123456789012:instance-profile/tutorial-test_ecs_instance_profile" | ||
ecs_service_role = "tutorial-test_ecs_service_role" | ||
ecs_task_execution_role = "arn:aws:iam::123456789012:role/tutorial-test_ecs_task_execution_role" # only used with launch type FARGATE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ resource "aws_ecs_cluster" "webapp_cluster" { | |
|
||
/* ECS service definition */ | ||
resource "aws_ecs_service" "webapp_service" { | ||
count = "${var.launch_type == "FARGATE" ? 0 : 1}" | ||
name = "${var.name_prefix}_webapp_service" | ||
cluster = "${aws_ecs_cluster.webapp_cluster.id}" | ||
task_definition = "${aws_ecs_task_definition.webapp_definition.arn}" | ||
|
@@ -23,9 +24,51 @@ resource "aws_ecs_service" "webapp_service" { | |
} | ||
} | ||
|
||
resource "aws_ecs_service" "webapp_service_fargate" { | ||
count = "${var.launch_type == "FARGATE" ? 1 : 0}" | ||
name = "${var.name_prefix}_webapp_service" | ||
cluster = "${aws_ecs_cluster.webapp_cluster.id}" | ||
task_definition = "${aws_ecs_task_definition.webapp_definition_fargate.arn}" | ||
launch_type = "FARGATE" | ||
desired_count = "${var.count_webapp}" | ||
deployment_minimum_healthy_percent = "${var.minimum_healthy_percent_webapp}" | ||
iam_role = "${var.ecs_service_role}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With Fargate, we don't need to create
See: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using-service-linked-roles.html It might be good to put this URL in the For now, I think it's a good idea to put |
||
|
||
network_configuration { | ||
security_groups = ["${var.sg_webapp_instances_id}"] | ||
subnets = ["${split(",", var.subnet_ids)}"] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this example, we need to add
See samuelkarp answer here.
Terraform documentation: https://www.terraform.io/docs/providers/aws/r/ecs_service.html#assign_public_ip |
||
|
||
load_balancer { | ||
target_group_arn = "${aws_alb_target_group.webapp_tg.arn}" | ||
container_name = "webapp" | ||
container_port = 5000 | ||
} | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "aws_ecs_task_definition" "webapp_definition" { | ||
count = "${var.launch_type == "FARGATE" ? 0 : 1}" | ||
family = "${var.name_prefix}_webapp" | ||
container_definitions = "${data.template_file.task_webapp.rendered}" | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "aws_ecs_task_definition" "webapp_definition_fargate" { | ||
count = "${var.launch_type == "FARGATE" ? 1 : 0}" | ||
family = "${var.name_prefix}_webapp" | ||
container_definitions = "${data.template_file.task_webapp.rendered}" | ||
requires_compatibilities = ["FARGATE"] | ||
network_mode = "awsvpc" | ||
execution_role_arn = "${var.ecs_task_execution_role}" | ||
cpu = "512" | ||
memory = "1024" | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
desired_capacity_on_demand
,ec2_key_name
, andinstance_type
are also not needed for Fargate.And also, I guess it makes more sense to use
us-east-1
in theaws_region
example here (instead ofap-northeast-1
).