-
Notifications
You must be signed in to change notification settings - Fork 881
add: ECS example template #3915
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
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. 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: | ||
|
||
- Task definition - the container definition, includes the image, command, volume(s) | ||
- ECS service - manages the task definition | ||
|
||
## 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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 4.28" | ||
} | ||
coder = { | ||
source = "coder/coder" | ||
version = "~> 0.4.9" | ||
} | ||
} | ||
} | ||
|
||
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" { | ||
shared_config_files = ["$HOME/.aws/config"] | ||
shared_credentials_files = ["$HOME/.aws/credentials"] | ||
} | ||
|
||
# coder workspace, created as an ECS task definition | ||
resource "aws_ecs_task_definition" "workspace" { | ||
family = "coder" | ||
|
||
requires_compatibilities = ["EC2"] | ||
cpu = var.cpu | ||
memory = var.memory | ||
container_definitions = jsonencode([ | ||
{ | ||
name = "coder-workspace-${data.coder_workspace.me.id}" | ||
image = "codercom/enterprise-base:ubuntu" | ||
cpu = 1024 | ||
memory = 2048 | ||
essential = true | ||
user = "coder" | ||
command = ["sh", "-c", coder_agent.coder.init_script] | ||
environment = [ | ||
{ | ||
"name" = "CODER_AGENT_TOKEN" | ||
"value" = coder_agent.coder.token | ||
} | ||
] | ||
mountPoints = [ | ||
{ | ||
# the name of the volume to mount | ||
sourceVolume = "home-dir-${data.coder_workspace.me.id}" | ||
# path on the container to mount the volume at | ||
containerPath = "/home/coder" | ||
} | ||
] | ||
portMappings = [ | ||
{ | ||
containerPort = 80 | ||
hostPort = 80 | ||
} | ||
] | ||
} | ||
]) | ||
|
||
# workspace persistent volume definition | ||
volume { | ||
name = "home-dir-${data.coder_workspace.me.id}" | ||
|
||
docker_volume_configuration { | ||
# "shared" ensures that the disk is persisted upon workspace restart | ||
scope = "shared" | ||
autoprovision = true | ||
driver = "local" | ||
} | ||
} | ||
} | ||
|
||
resource "aws_ecs_service" "workspace" { | ||
name = "workspace-${data.coder_workspace.me.id}" | ||
cluster = var.ecs-cluster | ||
task_definition = aws_ecs_task_definition.workspace.arn | ||
# scale the service to zero when the workspace is stopped | ||
desired_count = data.coder_workspace.me.start_count | ||
} | ||
|
||
data "coder_workspace" "me" {} | ||
|
||
resource "coder_agent" "coder" { | ||
arch = "amd64" | ||
auth = "token" | ||
os = "linux" | ||
dir = "/home/coder" | ||
startup_script = <<EOT | ||
#!/bin/bash | ||
# install and start code-server | ||
curl -fsSL https://code-server.dev/install.sh | sh | tee code-server-install.log | ||
code-server --auth none --port 13337 | tee code-server-install.log & | ||
EOT | ||
} | ||
|
||
resource "coder_app" "code-server" { | ||
agent_id = coder_agent.coder.id | ||
name = "code-server" | ||
icon = "/icon/code.svg" | ||
url = "http://localhost:13337?folder=/home/coder" | ||
relative_path = true | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We're currently using this to indicate whether this is a developer-level variable or template-wide variable. I assume the intended behavior is that the admin picks which cluster workspaces are deploying to?
I also suggest not setting a default value since it will skip it on
coder templates create
Some docs: https://coder.com/docs/coder-oss/latest/templates#parameters