Skip to content

Commit 70f9537

Browse files
committed
rm: cluster & compute
1 parent deeaba6 commit 70f9537

File tree

2 files changed

+15
-221
lines changed

2 files changed

+15
-221
lines changed

examples/templates/ecs-container/README.md

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,16 @@ tags: [cloud, aws]
66

77
# aws-ecs
88

9-
This is a sample template for running a Coder workspace on ECS.
10-
11-
## Required permissions / policy
12-
13-
The following sample policy allows Coder to create EC2 instances and modify
14-
instances provisioned by Coder:
15-
16-
```json
17-
{
18-
"Version": "2012-10-17",
19-
"Statement": [
20-
{
21-
"Sid": "VisualEditor0",
22-
"Effect": "Allow",
23-
"Action": [
24-
"ec2:GetDefaultCreditSpecification",
25-
"ec2:DescribeIamInstanceProfileAssociations",
26-
"ec2:DescribeTags",
27-
"ec2:CreateTags",
28-
"ec2:RunInstances",
29-
"ec2:DescribeInstanceCreditSpecifications",
30-
"ec2:DescribeImages",
31-
"ec2:ModifyDefaultCreditSpecification",
32-
"ec2:DescribeVolumes"
33-
],
34-
"Resource": "*"
35-
},
36-
{
37-
"Sid": "CoderResources",
38-
"Effect": "Allow",
39-
"Action": [
40-
"ec2:DescribeInstances",
41-
"ec2:DescribeInstanceAttribute",
42-
"ec2:UnmonitorInstances",
43-
"ec2:TerminateInstances",
44-
"ec2:StartInstances",
45-
"ec2:StopInstances",
46-
"ec2:DeleteTags",
47-
"ec2:MonitorInstances",
48-
"ec2:CreateTags",
49-
"ec2:RunInstances",
50-
"ec2:ModifyInstanceAttribute",
51-
"ec2:ModifyInstanceCreditSpecification"
52-
],
53-
"Resource": "arn:aws:ec2:*:*:instance/*",
54-
"Condition": {
55-
"StringEquals": {
56-
"aws:ResourceTag/Coder_Provisioned": "true"
57-
}
58-
}
59-
}
60-
]
61-
}
62-
```
63-
64-
Additionally, the `AmazonEC2ContainerServiceforEC2Role` managed policy should be
65-
attached to the container instance IAM role, otherwise you will receive an error
66-
when creating the ECS cluster.
67-
68-
This is represented as the `iam_instance_role` argument of the `launch_template`
69-
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).
9+
This is a sample template for running a Coder workspace on ECS. It assumes there
10+
is a pre-existing ECS cluster with EC2-based compute to host the workspace.
7011

7112
## Architecture
7213

7314
This workspace is built using the following AWS resources:
7415

75-
- Launch template - this defines the EC2 instance(s) to host the container
76-
- Auto-scaling group - EC2 auto-scaling group configuration
77-
- ECS cluster - logical grouping of containers to be run in ECS
78-
- Capacity provider - ECS-specific resource that ties in the auto-scaling group
7916
- Task definition - the container definition, includes the image, command, volume(s)
8017
- ECS service - manages the task definition
8118

82-
## User data
83-
84-
This template includes a two-part user data configuration, represented as the
85-
`cloudinit_config` data source. There is an ECS-specific user data definition,
86-
which is required for the EC2 instances to join the ECS cluster. Additionally, the
87-
Coder user data (defined in the `locals` block) is needed to stop/start the instance(s).
88-
8919
## code-server
9020

9121
`code-server` is installed via the `startup_script` argument in the `coder_agent`

examples/templates/ecs-container/main.tf

Lines changed: 13 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -8,155 +8,18 @@ terraform {
88
source = "coder/coder"
99
version = "~> 0.4.9"
1010
}
11-
cloudinit = {
12-
source = "hashicorp/cloudinit"
13-
version = "2.2.0"
14-
}
1511
}
1612
}
1713

18-
# required for multi-user data config
19-
provider "cloudinit" {}
14+
variable "ecs-cluster" {
15+
description = "Input the ECS cluster ARN to host the workspace"
16+
default = ""
17+
}
2018

2119
# configure AWS provider with creds present on Coder server host
2220
provider "aws" {
23-
region = "us-east-1"
24-
shared_config_files = ["/home/coder/.aws/config"]
25-
shared_credentials_files = ["/home/coder/.aws/credentials"]
26-
}
27-
28-
locals {
29-
30-
# User data is used to stop/start AWS instances. See:
31-
# https://github.com/hashicorp/terraform-provider-aws/issues/22
32-
33-
user_data_start = <<EOT
34-
Content-Type: multipart/mixed; boundary="//"
35-
MIME-Version: 1.0
36-
37-
--//
38-
Content-Type: text/cloud-config; charset="us-ascii"
39-
MIME-Version: 1.0
40-
Content-Transfer-Encoding: 7bit
41-
Content-Disposition: attachment; filename="cloud-config.txt"
42-
43-
#cloud-config
44-
cloud_final_modules:
45-
- [scripts-user, always]
46-
hostname: ${lower(data.coder_workspace.me.name)}
47-
48-
--//
49-
Content-Type: text/x-shellscript; charset="us-ascii"
50-
MIME-Version: 1.0
51-
Content-Transfer-Encoding: 7bit
52-
Content-Disposition: attachment; filename="userdata.txt"
53-
54-
#!/bin/bash
55-
sudo -u ubuntu sh -c '${coder_agent.coder.init_script}'
56-
--//--
57-
EOT
58-
59-
user_data_end = <<EOT
60-
Content-Type: multipart/mixed; boundary="//"
61-
MIME-Version: 1.0
62-
--//
63-
Content-Type: text/cloud-config; charset="us-ascii"
64-
MIME-Version: 1.0
65-
Content-Transfer-Encoding: 7bit
66-
Content-Disposition: attachment; filename="cloud-config.txt"
67-
#cloud-config
68-
cloud_final_modules:
69-
- [scripts-user, always]
70-
--//
71-
Content-Type: text/x-shellscript; charset="us-ascii"
72-
MIME-Version: 1.0
73-
Content-Transfer-Encoding: 7bit
74-
Content-Disposition: attachment; filename="userdata.txt"
75-
#!/bin/bash
76-
sudo shutdown -h now
77-
--//--
78-
EOT
79-
}
80-
81-
# user data definitions, for both ECS & Coder
82-
data "cloudinit_config" "main" {
83-
base64_encode = true
84-
gzip = false
85-
86-
part {
87-
content = data.coder_workspace.me.transition == "start" ? local.user_data_start : local.user_data_end
88-
}
89-
# required for the EC2 instances to join the ECS cluster
90-
part {
91-
content = "#!/bin/bash\necho ECS_CLUSTER=${aws_ecs_cluster.main.name} >> /etc/ecs/ecs.config"
92-
}
93-
}
94-
95-
# set launch template for EC2 auto-scaling group
96-
resource "aws_launch_template" "coder-oss-ubuntu" {
97-
name_prefix = "coder-oss"
98-
# ECS optimized AMI - https://github.com/awsdocs/amazon-ecs-developer-guide/blob/master/doc_source/ecs-optimized_AMI.md
99-
image_id = "ami-03f8a7b55051ae0d4"
100-
instance_type = "t2.medium"
101-
iam_instance_profile {
102-
arn = "arn:aws:iam::816024705881:instance-profile/coder-oss-ecs-role"
103-
}
104-
user_data = data.cloudinit_config.main.rendered
105-
}
106-
107-
# provision auto-scaling group to host ECS task definitions
108-
resource "aws_autoscaling_group" "main" {
109-
name = "coder-ecs-auto-scaling-group"
110-
min_size = 1
111-
max_size = 1
112-
desired_capacity = 1
113-
availability_zones = ["us-east-1a"]
114-
protect_from_scale_in = true
115-
116-
launch_template {
117-
id = aws_launch_template.coder-oss-ubuntu.id
118-
version = "$Latest"
119-
}
120-
121-
tag {
122-
key = "AmazonECSManaged"
123-
value = true
124-
propagate_at_launch = true
125-
}
126-
}
127-
128-
# create AWS ECS cluster
129-
resource "aws_ecs_cluster" "main" {
130-
name = "coder-oss-ecs"
131-
132-
setting {
133-
name = "containerInsights"
134-
value = "enabled"
135-
}
136-
}
137-
138-
# create capacity provider & tie in autoscaling group
139-
resource "aws_ecs_capacity_provider" "main" {
140-
name = "coder-oss-capacity-provider"
141-
142-
auto_scaling_group_provider {
143-
auto_scaling_group_arn = aws_autoscaling_group.main.arn
144-
managed_termination_protection = "ENABLED"
145-
146-
managed_scaling {
147-
maximum_scaling_step_size = 1000
148-
minimum_scaling_step_size = 1
149-
status = "ENABLED"
150-
target_capacity = 10
151-
}
152-
}
153-
}
154-
155-
# attach capacity provider to cluster
156-
resource "aws_ecs_cluster_capacity_providers" "main" {
157-
cluster_name = aws_ecs_cluster.main.name
158-
159-
capacity_providers = [aws_ecs_capacity_provider.main.name]
21+
shared_config_files = ["$HOME/.aws/config"]
22+
shared_credentials_files = ["$HOME/.aws/credentials"]
16023
}
16124

16225
# coder workspace, created as an ECS task definition
@@ -168,7 +31,7 @@ resource "aws_ecs_task_definition" "workspace" {
16831
memory = 2048
16932
container_definitions = jsonencode([
17033
{
171-
name = "coder-workspace-1"
34+
name = "coder-workspace-${data.coder_workspace.me.id}"
17235
image = "codercom/enterprise-base:ubuntu"
17336
cpu = 1024
17437
memory = 2048
@@ -184,7 +47,7 @@ resource "aws_ecs_task_definition" "workspace" {
18447
mountPoints = [
18548
{
18649
# the name of the volume to mount
187-
sourceVolume = "home-dir"
50+
sourceVolume = "home-dir-${data.coder_workspace.me.id}"
18851
# path on the container to mount the volume at
18952
containerPath = "/home/coder"
19053
}
@@ -200,7 +63,7 @@ resource "aws_ecs_task_definition" "workspace" {
20063

20164
# workspace persistent volume definition
20265
volume {
203-
name = "home-dir"
66+
name = "home-dir-${data.coder_workspace.me.id}"
20467

20568
docker_volume_configuration {
20669
# "shared" ensures that the disk is persisted upon workspace restart
@@ -212,10 +75,11 @@ resource "aws_ecs_task_definition" "workspace" {
21275
}
21376

21477
resource "aws_ecs_service" "workspace" {
215-
name = "workspace"
216-
cluster = aws_ecs_cluster.main.id
78+
name = "workspace-${data.coder_workspace.me.id}"
79+
cluster = var.ecs-cluster
21780
task_definition = aws_ecs_task_definition.workspace.arn
218-
desired_count = 1
81+
# scale the service to zero when the workspace is stopped
82+
desired_count = data.coder_workspace.me.start_count
21983
}
22084

22185
data "coder_workspace" "me" {}

0 commit comments

Comments
 (0)