Skip to content

Commit 3ffe7f5

Browse files
authored
feat(examples/templates): add aws vm devcontainer template (coder#11248)
* feat(examples/templates): add aws vm devcontainer template * Create README.md * add code-server * fix code-server * `make fmt` * Add files via upload * Update README.md * fix typo and persist workspace * always land in the repo directory
1 parent 97f7a35 commit 3ffe7f5

File tree

3 files changed

+283
-0
lines changed

3 files changed

+283
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
display_name: AWS EC2 (Devcontainer)
3+
description: Provision AWS EC2 VMs with a devcontainer as Coder workspaces
4+
icon: ../../../site/static/icon/aws.png
5+
maintainer_github: coder
6+
verified: true
7+
tags: [vm, linux, aws, persistent, devcontainer]
8+
---
9+
10+
# Remote Development on AWS EC2 VMs using a Devcontainer
11+
12+
Provision AWS EC2 VMs as [Coder workspaces](https://coder.com/docs/v2/latest) with this example template.
13+
![Architecture Diagram](./architecture.svg)
14+
15+
<!-- TODO: Add screenshot -->
16+
17+
## Prerequisites
18+
19+
### Authentication
20+
21+
By default, this template authenticates to AWS using the provider's default [authentication methods](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication-and-configuration).
22+
23+
The simplest way (without making changes to the template) is via environment variables (e.g. `AWS_ACCESS_KEY_ID`) or a [credentials file](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-format). If you are running Coder on a VM, this file must be in `/home/coder/aws/credentials`.
24+
25+
To use another [authentication method](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication), edit the template.
26+
27+
## Required permissions / policy
28+
29+
The following sample policy allows Coder to create EC2 instances and modify
30+
instances provisioned by Coder:
31+
32+
```json
33+
{
34+
"Version": "2012-10-17",
35+
"Statement": [
36+
{
37+
"Sid": "VisualEditor0",
38+
"Effect": "Allow",
39+
"Action": [
40+
"ec2:GetDefaultCreditSpecification",
41+
"ec2:DescribeIamInstanceProfileAssociations",
42+
"ec2:DescribeTags",
43+
"ec2:DescribeInstances",
44+
"ec2:DescribeInstanceTypes",
45+
"ec2:CreateTags",
46+
"ec2:RunInstances",
47+
"ec2:DescribeInstanceCreditSpecifications",
48+
"ec2:DescribeImages",
49+
"ec2:ModifyDefaultCreditSpecification",
50+
"ec2:DescribeVolumes"
51+
],
52+
"Resource": "*"
53+
},
54+
{
55+
"Sid": "CoderResources",
56+
"Effect": "Allow",
57+
"Action": [
58+
"ec2:DescribeInstanceAttribute",
59+
"ec2:UnmonitorInstances",
60+
"ec2:TerminateInstances",
61+
"ec2:StartInstances",
62+
"ec2:StopInstances",
63+
"ec2:DeleteTags",
64+
"ec2:MonitorInstances",
65+
"ec2:CreateTags",
66+
"ec2:RunInstances",
67+
"ec2:ModifyInstanceAttribute",
68+
"ec2:ModifyInstanceCreditSpecification"
69+
],
70+
"Resource": "arn:aws:ec2:*:*:instance/*",
71+
"Condition": {
72+
"StringEquals": {
73+
"aws:ResourceTag/Coder_Provisioned": "true"
74+
}
75+
}
76+
}
77+
]
78+
}
79+
```
80+
81+
## Architecture
82+
83+
This template provisions the following resources:
84+
85+
- AWS Instance
86+
87+
Coder uses `aws_ec2_instance_state` to start and stop the VM. This example template is fully persistent, meaning the full filesystem is preserved when the workspace restarts. See this [community example](https://github.com/bpmct/coder-templates/tree/main/aws-linux-ephemeral) of an ephemeral AWS instance.
88+
89+
> **Note**
90+
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
91+
92+
## code-server
93+
94+
`code-server` is installed via the [`code-server`](https://registry.coder.com/modules/code-server) registry module. For a list of all modules and templates pplease check [Coder Registry](https://registry.coder.com).

examples/templates/aws-devcontainer/architecture.svg

Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
aws = {
7+
source = "hashicorp/aws"
8+
}
9+
}
10+
}
11+
12+
module "aws_region" {
13+
source = "https://registry.coder.com/modules/aws-region"
14+
default = "us-east-1"
15+
}
16+
17+
data "coder_parameter" "instance_type" {
18+
name = "instance_type"
19+
display_name = "Instance type"
20+
description = "What instance type should your workspace use?"
21+
default = "t3.micro"
22+
mutable = false
23+
option {
24+
name = "2 vCPU, 1 GiB RAM"
25+
value = "t3.micro"
26+
}
27+
option {
28+
name = "2 vCPU, 2 GiB RAM"
29+
value = "t3.small"
30+
}
31+
option {
32+
name = "2 vCPU, 4 GiB RAM"
33+
value = "t3.medium"
34+
}
35+
option {
36+
name = "2 vCPU, 8 GiB RAM"
37+
value = "t3.large"
38+
}
39+
option {
40+
name = "4 vCPU, 16 GiB RAM"
41+
value = "t3.xlarge"
42+
}
43+
option {
44+
name = "8 vCPU, 32 GiB RAM"
45+
value = "t3.2xlarge"
46+
}
47+
}
48+
49+
provider "aws" {
50+
region = module.aws_region.value
51+
}
52+
53+
data "coder_workspace" "me" {
54+
}
55+
56+
data "aws_ami" "ubuntu" {
57+
most_recent = true
58+
filter {
59+
name = "name"
60+
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
61+
}
62+
filter {
63+
name = "virtualization-type"
64+
values = ["hvm"]
65+
}
66+
owners = ["099720109477"] # Canonical
67+
}
68+
69+
data "coder_parameter" "repo_url" {
70+
name = "repo_url"
71+
display_name = "Repository URL"
72+
default = "https://github.com/coder/envbuilder-starter-devcontainer"
73+
description = "Repository URL"
74+
mutable = true
75+
}
76+
77+
resource "coder_agent" "dev" {
78+
count = data.coder_workspace.me.start_count
79+
arch = "amd64"
80+
auth = "token"
81+
os = "linux"
82+
dir = "/workspaces/${trimsuffix(basename(data.coder_parameter.repo_url.value), ".git")}"
83+
connection_timeout = 0
84+
85+
metadata {
86+
key = "cpu"
87+
display_name = "CPU Usage"
88+
interval = 5
89+
timeout = 5
90+
script = "coder stat cpu"
91+
}
92+
metadata {
93+
key = "memory"
94+
display_name = "Memory Usage"
95+
interval = 5
96+
timeout = 5
97+
script = "coder stat mem"
98+
}
99+
}
100+
101+
module "code-server" {
102+
count = data.coder_workspace.me.start_count
103+
source = "https://registry.coder.com/modules/code-server"
104+
agent_id = coder_agent.dev[0].id
105+
}
106+
107+
locals {
108+
linux_user = "coder"
109+
user_data = <<-EOT
110+
Content-Type: multipart/mixed; boundary="//"
111+
MIME-Version: 1.0
112+
113+
--//
114+
Content-Type: text/cloud-config; charset="us-ascii"
115+
MIME-Version: 1.0
116+
Content-Transfer-Encoding: 7bit
117+
Content-Disposition: attachment; filename="cloud-config.txt"
118+
119+
#cloud-config
120+
cloud_final_modules:
121+
- [scripts-user, always]
122+
hostname: ${lower(data.coder_workspace.me.name)}
123+
users:
124+
- name: ${local.linux_user}
125+
sudo: ALL=(ALL) NOPASSWD:ALL
126+
shell: /bin/bash
127+
128+
--//
129+
Content-Type: text/x-shellscript; charset="us-ascii"
130+
MIME-Version: 1.0
131+
Content-Transfer-Encoding: 7bit
132+
Content-Disposition: attachment; filename="userdata.txt"
133+
134+
#!/bin/bash
135+
# Install Docker
136+
if ! command -v docker &> /dev/null
137+
then
138+
echo "Docker not found, installing..."
139+
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh 2>&1 >/dev/null
140+
usermod -aG docker ${local.linux_user}
141+
newgrp docker
142+
else
143+
echo "Docker is already installed."
144+
fi
145+
146+
# Start envbuilder
147+
docker run --rm \
148+
-v /home/${local.linux_user}/envbuilder:/workspaces \
149+
-e CODER_AGENT_TOKEN="${try(coder_agent.dev[0].token, "")}" \
150+
-e CODER_AGENT_URL="${data.coder_workspace.me.access_url}" \
151+
-e GIT_URL="${data.coder_parameter.repo_url.value}" \
152+
-e INIT_SCRIPT="echo ${base64encode(try(coder_agent.dev[0].init_script, ""))} | base64 -d | sh" \
153+
-e FALLBACK_IMAGE="codercom/enterprise-base:ubuntu" \
154+
ghcr.io/coder/envbuilder
155+
--//--
156+
EOT
157+
}
158+
159+
resource "aws_instance" "vm" {
160+
ami = data.aws_ami.ubuntu.id
161+
availability_zone = "${module.aws_region.value}a"
162+
instance_type = data.coder_parameter.instance_type.value
163+
root_block_device {
164+
volume_size = 30
165+
}
166+
167+
user_data = local.user_data
168+
tags = {
169+
Name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
170+
# Required if you are using our example policy, see template README
171+
Coder_Provisioned = "true"
172+
}
173+
lifecycle {
174+
ignore_changes = [ami]
175+
}
176+
}
177+
178+
resource "aws_ec2_instance_state" "vm" {
179+
instance_id = aws_instance.vm.id
180+
state = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
181+
}

0 commit comments

Comments
 (0)