From c8d11d021e645c7fc5eed38efaaad930413d4c3b Mon Sep 17 00:00:00 2001 From: Eric Paulsen Date: Tue, 6 Sep 2022 16:11:52 -0500 Subject: [PATCH 1/5] add: ECS example template --- examples/templates/ecs-container/README.md | 93 ++++++++++++++++++++++ examples/templates/ecs-container/main.tf | 0 2 files changed, 93 insertions(+) create mode 100644 examples/templates/ecs-container/README.md create mode 100644 examples/templates/ecs-container/main.tf diff --git a/examples/templates/ecs-container/README.md b/examples/templates/ecs-container/README.md new file mode 100644 index 0000000000000..acaa9fb79630d --- /dev/null +++ b/examples/templates/ecs-container/README.md @@ -0,0 +1,93 @@ +--- +name: Develop in an ECS-hosted container +description: Get started with Linux development on AWS ECS. +tags: [cloud, aws] +--- + +# aws-ecs + +This is a sample template for running a Coder workspace on ECS. + +## Required permissions / policy + +The following sample policy allows Coder to create EC2 instances and modify +instances provisioned by Coder: + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "VisualEditor0", + "Effect": "Allow", + "Action": [ + "ec2:GetDefaultCreditSpecification", + "ec2:DescribeIamInstanceProfileAssociations", + "ec2:DescribeTags", + "ec2:CreateTags", + "ec2:RunInstances", + "ec2:DescribeInstanceCreditSpecifications", + "ec2:DescribeImages", + "ec2:ModifyDefaultCreditSpecification", + "ec2:DescribeVolumes" + ], + "Resource": "*" + }, + { + "Sid": "CoderResources", + "Effect": "Allow", + "Action": [ + "ec2:DescribeInstances", + "ec2:DescribeInstanceAttribute", + "ec2:UnmonitorInstances", + "ec2:TerminateInstances", + "ec2:StartInstances", + "ec2:StopInstances", + "ec2:DeleteTags", + "ec2:MonitorInstances", + "ec2:CreateTags", + "ec2:RunInstances", + "ec2:ModifyInstanceAttribute", + "ec2:ModifyInstanceCreditSpecification" + ], + "Resource": "arn:aws:ec2:*:*:instance/*", + "Condition": { + "StringEquals": { + "aws:ResourceTag/Coder_Provisioned": "true" + } + } + } + ] +} +``` + +Additionally, the `AmazonEC2ContainerServiceforEC2Role` managed policy should be +attached to the container instance IAM role, otherwise you will receive an error +when creating the ECS cluster. + +This is represented as the `iam_instance_role` argument of the `launch_template` +resource. Please see the [AWS documentation for configuring this instance role](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html#instance-iam-role-verify). + +## Architecture + +This workspace is built using the following AWS resources: + +- Launch template - this defines the EC2 instance(s) to host the container +- Auto-scaling group - EC2 auto-scaling group configuration +- ECS cluster - logical grouping of containers to be run in ECS +- Capacity provider - ECS-specific resource that ties in the auto-scaling group +- Task definition - the container definition, includes the image, command, volume(s) +- ECS service - manages the task definition + +## User data + +This template includes a two-part user data configuration, represented as the +`cloudinit_config` data source. There is an ECS-specific user data definition, +which is required for the EC2 instances to join the ECS cluster. Additionally, the +Coder user data (defined in the `locals` block) is needed to stop/start the instance(s). + +## code-server + +`code-server` is installed via the `startup_script` argument in the `coder_agent` +resource block. The `coder_app` resource is defined to access `code-server` through +the dashboard UI over `localhost:13337`. diff --git a/examples/templates/ecs-container/main.tf b/examples/templates/ecs-container/main.tf new file mode 100644 index 0000000000000..e69de29bb2d1d From 49e1bd5b921491908004135e30d7665c47af0034 Mon Sep 17 00:00:00 2001 From: Eric Paulsen Date: Tue, 6 Sep 2022 16:20:24 -0500 Subject: [PATCH 2/5] fix: empty main.tf --- examples/templates/ecs-container/main.tf | 250 +++++++++++++++++++++++ 1 file changed, 250 insertions(+) diff --git a/examples/templates/ecs-container/main.tf b/examples/templates/ecs-container/main.tf index e69de29bb2d1d..ae0509665cf89 100644 --- a/examples/templates/ecs-container/main.tf +++ b/examples/templates/ecs-container/main.tf @@ -0,0 +1,250 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 4.28" + } + coder = { + source = "coder/coder" + version = "~> 0.4.9" + } + cloudinit = { + source = "hashicorp/cloudinit" + version = "2.2.0" + } + } +} + +# required for multi-user data config +provider "cloudinit" {} + +# configure AWS provider with creds present on Coder server host +provider "aws" { + region = "us-east-1" + shared_config_files = ["/home/coder/.aws/config"] + shared_credentials_files = ["/home/coder/.aws/credentials"] +} + +# data "template_file" "user-data-start" { +# template = file("${path.module}/config/user-data-start.sh") +# } +# +# data "template_file" "user-data-end" { +# template = file("${path.module}/config/user-data-end.sh") +# } + +locals { + + # User data is used to stop/start AWS instances. See: + # https://github.com/hashicorp/terraform-provider-aws/issues/22 + + user_data_start = < Date: Tue, 6 Sep 2022 16:25:29 -0500 Subject: [PATCH 3/5] cleanup --- examples/templates/ecs-container/main.tf | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/examples/templates/ecs-container/main.tf b/examples/templates/ecs-container/main.tf index ae0509665cf89..a9b8e35f10ccf 100644 --- a/examples/templates/ecs-container/main.tf +++ b/examples/templates/ecs-container/main.tf @@ -25,14 +25,6 @@ provider "aws" { shared_credentials_files = ["/home/coder/.aws/credentials"] } -# data "template_file" "user-data-start" { -# template = file("${path.module}/config/user-data-start.sh") -# } -# -# data "template_file" "user-data-end" { -# template = file("${path.module}/config/user-data-end.sh") -# } - locals { # User data is used to stop/start AWS instances. See: @@ -112,7 +104,7 @@ resource "aws_launch_template" "coder-oss-ubuntu" { user_data = data.cloudinit_config.main.rendered } -# provision auto-scaling group to host ECS task defintions +# provision auto-scaling group to host ECS task definitions resource "aws_autoscaling_group" "main" { name = "coder-ecs-auto-scaling-group" min_size = 1 @@ -206,7 +198,7 @@ resource "aws_ecs_task_definition" "workspace" { } ]) - # workspace persistent volume defintion + # workspace persistent volume definition volume { name = "home-dir" From 70f9537eccc9fa927a095ad36bef29da2792659c Mon Sep 17 00:00:00 2001 From: Eric Paulsen Date: Wed, 7 Sep 2022 10:50:28 -0500 Subject: [PATCH 4/5] rm: cluster & compute --- examples/templates/ecs-container/README.md | 74 +--------- examples/templates/ecs-container/main.tf | 162 ++------------------- 2 files changed, 15 insertions(+), 221 deletions(-) diff --git a/examples/templates/ecs-container/README.md b/examples/templates/ecs-container/README.md index acaa9fb79630d..8ca7a2fc79d14 100644 --- a/examples/templates/ecs-container/README.md +++ b/examples/templates/ecs-container/README.md @@ -6,86 +6,16 @@ tags: [cloud, aws] # aws-ecs -This is a sample template for running a Coder workspace on ECS. - -## Required permissions / policy - -The following sample policy allows Coder to create EC2 instances and modify -instances provisioned by Coder: - -```json -{ - "Version": "2012-10-17", - "Statement": [ - { - "Sid": "VisualEditor0", - "Effect": "Allow", - "Action": [ - "ec2:GetDefaultCreditSpecification", - "ec2:DescribeIamInstanceProfileAssociations", - "ec2:DescribeTags", - "ec2:CreateTags", - "ec2:RunInstances", - "ec2:DescribeInstanceCreditSpecifications", - "ec2:DescribeImages", - "ec2:ModifyDefaultCreditSpecification", - "ec2:DescribeVolumes" - ], - "Resource": "*" - }, - { - "Sid": "CoderResources", - "Effect": "Allow", - "Action": [ - "ec2:DescribeInstances", - "ec2:DescribeInstanceAttribute", - "ec2:UnmonitorInstances", - "ec2:TerminateInstances", - "ec2:StartInstances", - "ec2:StopInstances", - "ec2:DeleteTags", - "ec2:MonitorInstances", - "ec2:CreateTags", - "ec2:RunInstances", - "ec2:ModifyInstanceAttribute", - "ec2:ModifyInstanceCreditSpecification" - ], - "Resource": "arn:aws:ec2:*:*:instance/*", - "Condition": { - "StringEquals": { - "aws:ResourceTag/Coder_Provisioned": "true" - } - } - } - ] -} -``` - -Additionally, the `AmazonEC2ContainerServiceforEC2Role` managed policy should be -attached to the container instance IAM role, otherwise you will receive an error -when creating the ECS cluster. - -This is represented as the `iam_instance_role` argument of the `launch_template` -resource. Please see the [AWS documentation for configuring this instance role](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html#instance-iam-role-verify). +This is a sample template for running a Coder workspace on ECS. It assumes there +is a pre-existing ECS cluster with EC2-based compute to host the workspace. ## Architecture This workspace is built using the following AWS resources: -- Launch template - this defines the EC2 instance(s) to host the container -- Auto-scaling group - EC2 auto-scaling group configuration -- ECS cluster - logical grouping of containers to be run in ECS -- Capacity provider - ECS-specific resource that ties in the auto-scaling group - Task definition - the container definition, includes the image, command, volume(s) - ECS service - manages the task definition -## User data - -This template includes a two-part user data configuration, represented as the -`cloudinit_config` data source. There is an ECS-specific user data definition, -which is required for the EC2 instances to join the ECS cluster. Additionally, the -Coder user data (defined in the `locals` block) is needed to stop/start the instance(s). - ## code-server `code-server` is installed via the `startup_script` argument in the `coder_agent` diff --git a/examples/templates/ecs-container/main.tf b/examples/templates/ecs-container/main.tf index a9b8e35f10ccf..262b1a63c9800 100644 --- a/examples/templates/ecs-container/main.tf +++ b/examples/templates/ecs-container/main.tf @@ -8,155 +8,18 @@ terraform { source = "coder/coder" version = "~> 0.4.9" } - cloudinit = { - source = "hashicorp/cloudinit" - version = "2.2.0" - } } } -# required for multi-user data config -provider "cloudinit" {} +variable "ecs-cluster" { + description = "Input the ECS cluster ARN to host the workspace" + default = "" +} # configure AWS provider with creds present on Coder server host provider "aws" { - region = "us-east-1" - shared_config_files = ["/home/coder/.aws/config"] - shared_credentials_files = ["/home/coder/.aws/credentials"] -} - -locals { - - # User data is used to stop/start AWS instances. See: - # https://github.com/hashicorp/terraform-provider-aws/issues/22 - - user_data_start = < Date: Thu, 8 Sep 2022 10:20:38 -0500 Subject: [PATCH 5/5] set CPU & memory vars Co-authored-by: Ben Potter --- examples/templates/ecs-container/main.tf | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/templates/ecs-container/main.tf b/examples/templates/ecs-container/main.tf index 262b1a63c9800..7c5a882ce4576 100644 --- a/examples/templates/ecs-container/main.tf +++ b/examples/templates/ecs-container/main.tf @@ -15,6 +15,13 @@ variable "ecs-cluster" { description = "Input the ECS cluster ARN to host the workspace" default = "" } +variable "cpu" { + default = "1024" +} + +variable "memory" { + default = "2048" +} # configure AWS provider with creds present on Coder server host provider "aws" { @@ -27,8 +34,8 @@ resource "aws_ecs_task_definition" "workspace" { family = "coder" requires_compatibilities = ["EC2"] - cpu = 1024 - memory = 2048 + cpu = var.cpu + memory = var.memory container_definitions = jsonencode([ { name = "coder-workspace-${data.coder_workspace.me.id}"