mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore(examples/templates/aws-devcontainer): update to use envbuilder provider` (#14831)
* chore(examples/templates/aws-devcontainer): update to use envbuilder provider * fix(devcontainer-gcp): set builder image properly
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
"persistent",
|
"persistent",
|
||||||
"devcontainer"
|
"devcontainer"
|
||||||
],
|
],
|
||||||
"markdown": "\n# Remote Development on AWS EC2 VMs using a Devcontainer\n\nProvision AWS EC2 VMs as [Coder workspaces](https://coder.com/docs) with this example template.\n\n\n\u003c!-- TODO: Add screenshot --\u003e\n\n## Prerequisites\n\n### Authentication\n\nBy 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).\n\nThe 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`.\n\nTo use another [authentication method](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication), edit the template.\n\n## Required permissions / policy\n\nThe following sample policy allows Coder to create EC2 instances and modify\ninstances provisioned by Coder:\n\n```json\n{\n\t\"Version\": \"2012-10-17\",\n\t\"Statement\": [\n\t\t{\n\t\t\t\"Sid\": \"VisualEditor0\",\n\t\t\t\"Effect\": \"Allow\",\n\t\t\t\"Action\": [\n\t\t\t\t\"ec2:GetDefaultCreditSpecification\",\n\t\t\t\t\"ec2:DescribeIamInstanceProfileAssociations\",\n\t\t\t\t\"ec2:DescribeTags\",\n\t\t\t\t\"ec2:DescribeInstances\",\n\t\t\t\t\"ec2:DescribeInstanceTypes\",\n\t\t\t\t\"ec2:CreateTags\",\n\t\t\t\t\"ec2:RunInstances\",\n\t\t\t\t\"ec2:DescribeInstanceCreditSpecifications\",\n\t\t\t\t\"ec2:DescribeImages\",\n\t\t\t\t\"ec2:ModifyDefaultCreditSpecification\",\n\t\t\t\t\"ec2:DescribeVolumes\"\n\t\t\t],\n\t\t\t\"Resource\": \"*\"\n\t\t},\n\t\t{\n\t\t\t\"Sid\": \"CoderResources\",\n\t\t\t\"Effect\": \"Allow\",\n\t\t\t\"Action\": [\n\t\t\t\t\"ec2:DescribeInstanceAttribute\",\n\t\t\t\t\"ec2:UnmonitorInstances\",\n\t\t\t\t\"ec2:TerminateInstances\",\n\t\t\t\t\"ec2:StartInstances\",\n\t\t\t\t\"ec2:StopInstances\",\n\t\t\t\t\"ec2:DeleteTags\",\n\t\t\t\t\"ec2:MonitorInstances\",\n\t\t\t\t\"ec2:CreateTags\",\n\t\t\t\t\"ec2:RunInstances\",\n\t\t\t\t\"ec2:ModifyInstanceAttribute\",\n\t\t\t\t\"ec2:ModifyInstanceCreditSpecification\"\n\t\t\t],\n\t\t\t\"Resource\": \"arn:aws:ec2:*:*:instance/*\",\n\t\t\t\"Condition\": {\n\t\t\t\t\"StringEquals\": {\n\t\t\t\t\t\"aws:ResourceTag/Coder_Provisioned\": \"true\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t]\n}\n```\n\n## Architecture\n\nThis template provisions the following resources:\n\n- AWS Instance\n\nCoder 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.\n\n\u003e **Note**\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n## code-server\n\n`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).\n"
|
"markdown": "\n# Remote Development on AWS EC2 VMs using a Devcontainer\n\nProvision AWS EC2 VMs as [Coder workspaces](https://coder.com/docs) with this example template.\n\n\n\u003c!-- TODO: Add screenshot --\u003e\n\n## Prerequisites\n\n### Authentication\n\nBy 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).\n\nThe 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`.\n\nTo use another [authentication method](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication), edit the template.\n\n## Required permissions / policy\n\nThe following sample policy allows Coder to create EC2 instances and modify\ninstances provisioned by Coder:\n\n```json\n{\n\t\"Version\": \"2012-10-17\",\n\t\"Statement\": [\n\t\t{\n\t\t\t\"Sid\": \"VisualEditor0\",\n\t\t\t\"Effect\": \"Allow\",\n\t\t\t\"Action\": [\n\t\t\t\t\"ec2:GetDefaultCreditSpecification\",\n\t\t\t\t\"ec2:DescribeIamInstanceProfileAssociations\",\n\t\t\t\t\"ec2:DescribeTags\",\n\t\t\t\t\"ec2:DescribeInstances\",\n\t\t\t\t\"ec2:DescribeInstanceTypes\",\n\t\t\t\t\"ec2:CreateTags\",\n\t\t\t\t\"ec2:RunInstances\",\n\t\t\t\t\"ec2:DescribeInstanceCreditSpecifications\",\n\t\t\t\t\"ec2:DescribeImages\",\n\t\t\t\t\"ec2:ModifyDefaultCreditSpecification\",\n\t\t\t\t\"ec2:DescribeVolumes\"\n\t\t\t],\n\t\t\t\"Resource\": \"*\"\n\t\t},\n\t\t{\n\t\t\t\"Sid\": \"CoderResources\",\n\t\t\t\"Effect\": \"Allow\",\n\t\t\t\"Action\": [\n\t\t\t\t\"ec2:DescribeInstanceAttribute\",\n\t\t\t\t\"ec2:UnmonitorInstances\",\n\t\t\t\t\"ec2:TerminateInstances\",\n\t\t\t\t\"ec2:StartInstances\",\n\t\t\t\t\"ec2:StopInstances\",\n\t\t\t\t\"ec2:DeleteTags\",\n\t\t\t\t\"ec2:MonitorInstances\",\n\t\t\t\t\"ec2:CreateTags\",\n\t\t\t\t\"ec2:RunInstances\",\n\t\t\t\t\"ec2:ModifyInstanceAttribute\",\n\t\t\t\t\"ec2:ModifyInstanceCreditSpecification\"\n\t\t\t],\n\t\t\t\"Resource\": \"arn:aws:ec2:*:*:instance/*\",\n\t\t\t\"Condition\": {\n\t\t\t\t\"StringEquals\": {\n\t\t\t\t\t\"aws:ResourceTag/Coder_Provisioned\": \"true\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t]\n}\n```\n\n## Architecture\n\nThis template provisions the following resources:\n\n- AWS Instance\n\nCoder 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.\n\n\u003e **Note**\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n## Caching\n\nTo speed up your builds, you can use a container registry as a cache.\nWhen creating the template, set the parameter `cache_repo` to a valid Docker repository in the form `host.tld/path/to/repo`.\n\nSee the [Envbuilder Terraform Provider Examples](https://github.com/coder/terraform-provider-envbuilder/blob/main/examples/resources/envbuilder_cached_image/envbuilder_cached_image_resource.tf/) for a more complete example of how the provider works.\n\n\u003e [!NOTE] We recommend using a registry cache with authentication enabled.\n\u003e To allow Envbuilder to authenticate with a registry cache hosted on ECR, specify an IAM instance\n\u003e profile that has read and write access to the given registry. For more information, see the\n\u003e [AWS documentation](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html).\n\u003e\n\u003e Alternatively, you can specify the variable `cache_repo_docker_config_path`\n\u003e with the path to a Docker config `.json` on disk containing valid credentials for the registry.\n\n## code-server\n\n`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).\n"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "aws-linux",
|
"id": "aws-linux",
|
||||||
|
|||||||
@@ -89,6 +89,21 @@ Coder uses `aws_ec2_instance_state` to start and stop the VM. This example templ
|
|||||||
> **Note**
|
> **Note**
|
||||||
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
|
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
|
||||||
|
|
||||||
|
## Caching
|
||||||
|
|
||||||
|
To speed up your builds, you can use a container registry as a cache.
|
||||||
|
When creating the template, set the parameter `cache_repo` to a valid Docker repository in the form `host.tld/path/to/repo`.
|
||||||
|
|
||||||
|
See the [Envbuilder Terraform Provider Examples](https://github.com/coder/terraform-provider-envbuilder/blob/main/examples/resources/envbuilder_cached_image/envbuilder_cached_image_resource.tf/) for a more complete example of how the provider works.
|
||||||
|
|
||||||
|
> [!NOTE] We recommend using a registry cache with authentication enabled.
|
||||||
|
> To allow Envbuilder to authenticate with a registry cache hosted on ECR, specify an IAM instance
|
||||||
|
> profile that has read and write access to the given registry. For more information, see the
|
||||||
|
> [AWS documentation](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html).
|
||||||
|
>
|
||||||
|
> Alternatively, you can specify the variable `cache_repo_docker_config_path`
|
||||||
|
> with the path to a Docker config `.json` on disk containing valid credentials for the registry.
|
||||||
|
|
||||||
## code-server
|
## code-server
|
||||||
|
|
||||||
`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).
|
`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).
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ terraform {
|
|||||||
aws = {
|
aws = {
|
||||||
source = "hashicorp/aws"
|
source = "hashicorp/aws"
|
||||||
}
|
}
|
||||||
|
envbuilder = {
|
||||||
|
source = "coder/envbuilder"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14,6 +17,45 @@ module "aws_region" {
|
|||||||
default = "us-east-1"
|
default = "us-east-1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provider "aws" {
|
||||||
|
region = module.aws_region.value
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "cache_repo" {
|
||||||
|
default = ""
|
||||||
|
description = "(Optional) Use a container registry as a cache to speed up builds. Example: host.tld/path/to/repo."
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "cache_repo_docker_config_path" {
|
||||||
|
default = ""
|
||||||
|
description = "(Optional) Path to a docker config.json containing credentials to the provided cache repo, if required. This will depend on your Coder setup. Example: `/home/coder/.docker/config.json`."
|
||||||
|
sensitive = true
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "iam_instance_profile" {
|
||||||
|
default = ""
|
||||||
|
description = "(Optional) Name of an IAM instance profile to assign to the instance."
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
data "coder_workspace" "me" {}
|
||||||
|
data "coder_workspace_owner" "me" {}
|
||||||
|
|
||||||
|
data "aws_ami" "ubuntu" {
|
||||||
|
most_recent = true
|
||||||
|
filter {
|
||||||
|
name = "name"
|
||||||
|
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
|
||||||
|
}
|
||||||
|
filter {
|
||||||
|
name = "virtualization-type"
|
||||||
|
values = ["hvm"]
|
||||||
|
}
|
||||||
|
owners = ["099720109477"] # Canonical
|
||||||
|
}
|
||||||
|
|
||||||
data "coder_parameter" "instance_type" {
|
data "coder_parameter" "instance_type" {
|
||||||
name = "instance_type"
|
name = "instance_type"
|
||||||
display_name = "Instance type"
|
display_name = "Instance type"
|
||||||
@@ -46,25 +88,26 @@ data "coder_parameter" "instance_type" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
provider "aws" {
|
data "coder_parameter" "fallback_image" {
|
||||||
region = module.aws_region.value
|
default = "codercom/enterprise-base:ubuntu"
|
||||||
|
description = "This image runs if the devcontainer fails to build."
|
||||||
|
display_name = "Fallback Image"
|
||||||
|
mutable = true
|
||||||
|
name = "fallback_image"
|
||||||
|
order = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
data "coder_workspace" "me" {
|
data "coder_parameter" "devcontainer_builder" {
|
||||||
}
|
description = <<-EOF
|
||||||
data "coder_workspace_owner" "me" {}
|
Image that will build the devcontainer.
|
||||||
|
Find the latest version of Envbuilder here: https://ghcr.io/coder/envbuilder
|
||||||
data "aws_ami" "ubuntu" {
|
Be aware that using the `:latest` tag may expose you to breaking changes.
|
||||||
most_recent = true
|
EOF
|
||||||
filter {
|
display_name = "Devcontainer Builder"
|
||||||
name = "name"
|
mutable = true
|
||||||
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
|
name = "devcontainer_builder"
|
||||||
}
|
default = "ghcr.io/coder/envbuilder:latest"
|
||||||
filter {
|
order = 4
|
||||||
name = "virtualization-type"
|
|
||||||
values = ["hvm"]
|
|
||||||
}
|
|
||||||
owners = ["099720109477"] # Canonical
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data "coder_parameter" "repo_url" {
|
data "coder_parameter" "repo_url" {
|
||||||
@@ -75,6 +118,175 @@ data "coder_parameter" "repo_url" {
|
|||||||
mutable = true
|
mutable = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data "coder_parameter" "ssh_pubkey" {
|
||||||
|
name = "ssh_pubkey"
|
||||||
|
display_name = "SSH Public Key"
|
||||||
|
default = ""
|
||||||
|
description = "(Optional) Add an SSH public key to the `coder` user's authorized_keys. Useful for troubleshooting. You may need to add a security group to the instance."
|
||||||
|
mutable = false
|
||||||
|
}
|
||||||
|
|
||||||
|
data "local_sensitive_file" "cache_repo_dockerconfigjson" {
|
||||||
|
count = var.cache_repo_docker_config_path == "" ? 0 : 1
|
||||||
|
filename = var.cache_repo_docker_config_path
|
||||||
|
}
|
||||||
|
|
||||||
|
data "aws_iam_instance_profile" "vm_instance_profile" {
|
||||||
|
count = var.iam_instance_profile == "" ? 0 : 1
|
||||||
|
name = var.iam_instance_profile
|
||||||
|
}
|
||||||
|
|
||||||
|
# Be careful when modifying the below locals!
|
||||||
|
locals {
|
||||||
|
# TODO: provide a way to pick the availability zone.
|
||||||
|
aws_availability_zone = "${module.aws_region.value}a"
|
||||||
|
linux_user = "coder"
|
||||||
|
# Name the container after the workspace and owner.
|
||||||
|
container_name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
|
||||||
|
# The devcontainer builder image is the image that will build the devcontainer.
|
||||||
|
devcontainer_builder_image = data.coder_parameter.devcontainer_builder.value
|
||||||
|
# We may need to authenticate with a registry. If so, the user will provide a path to a docker config.json.
|
||||||
|
docker_config_json_base64 = try(data.local_sensitive_file.cache_repo_dockerconfigjson[0].content_base64, "")
|
||||||
|
# The envbuilder provider requires a key-value map of environment variables. Build this here.
|
||||||
|
envbuilder_env = {
|
||||||
|
# ENVBUILDER_GIT_URL and ENVBUILDER_CACHE_REPO will be overridden by the provider
|
||||||
|
# if the cache repo is enabled.
|
||||||
|
"ENVBUILDER_GIT_URL" : data.coder_parameter.repo_url.value,
|
||||||
|
# The agent token is required for the agent to connect to the Coder platform.
|
||||||
|
"CODER_AGENT_TOKEN" : try(coder_agent.dev.0.token, ""),
|
||||||
|
# The agent URL is required for the agent to connect to the Coder platform.
|
||||||
|
"CODER_AGENT_URL" : data.coder_workspace.me.access_url,
|
||||||
|
# The agent init script is required for the agent to start up. We base64 encode it here
|
||||||
|
# to avoid quoting issues.
|
||||||
|
"ENVBUILDER_INIT_SCRIPT" : "echo ${base64encode(try(coder_agent.dev[0].init_script, ""))} | base64 -d | sh",
|
||||||
|
"ENVBUILDER_DOCKER_CONFIG_BASE64" : try(data.local_sensitive_file.cache_repo_dockerconfigjson[0].content_base64, ""),
|
||||||
|
# The fallback image is the image that will run if the devcontainer fails to build.
|
||||||
|
"ENVBUILDER_FALLBACK_IMAGE" : data.coder_parameter.fallback_image.value,
|
||||||
|
# The following are used to push the image to the cache repo, if defined.
|
||||||
|
"ENVBUILDER_CACHE_REPO" : var.cache_repo,
|
||||||
|
"ENVBUILDER_PUSH_IMAGE" : var.cache_repo == "" ? "" : "true",
|
||||||
|
# You can add other required environment variables here.
|
||||||
|
# See: https://github.com/coder/envbuilder/?tab=readme-ov-file#environment-variables
|
||||||
|
}
|
||||||
|
# If we have a cached image, use the cached image's environment variables. Otherwise, just use
|
||||||
|
# the environment variables we've defined above.
|
||||||
|
docker_env_input = try(envbuilder_cached_image.cached.0.env_map, local.envbuilder_env)
|
||||||
|
# Convert the above to the list of arguments for the Docker run command.
|
||||||
|
# The startup script will write this to a file, which the Docker run command will reference.
|
||||||
|
docker_env_list_base64 = base64encode(join("\n", [for k, v in local.docker_env_input : "${k}=${v}"]))
|
||||||
|
# Builder image will either be the builder image parameter, or the cached image, if cache is provided.
|
||||||
|
builder_image = try(envbuilder_cached_image.cached[0].image, data.coder_parameter.devcontainer_builder.value)
|
||||||
|
# User data to start the workspace.
|
||||||
|
user_data = <<-EOT
|
||||||
|
Content-Type: multipart/mixed; boundary="//"
|
||||||
|
MIME-Version: 1.0
|
||||||
|
|
||||||
|
--//
|
||||||
|
Content-Type: text/cloud-config; charset="us-ascii"
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
Content-Disposition: attachment; filename="cloud-config.txt"
|
||||||
|
|
||||||
|
#cloud-config
|
||||||
|
cloud_final_modules:
|
||||||
|
- [scripts-user, always]
|
||||||
|
hostname: ${lower(data.coder_workspace.me.name)}
|
||||||
|
users:
|
||||||
|
- name: ${local.linux_user}
|
||||||
|
sudo: ALL=(ALL) NOPASSWD:ALL
|
||||||
|
shell: /bin/bash
|
||||||
|
ssh_authorized_keys:
|
||||||
|
- "${data.coder_parameter.ssh_pubkey.value}"
|
||||||
|
|
||||||
|
--//
|
||||||
|
Content-Type: text/x-shellscript; charset="us-ascii"
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
Content-Disposition: attachment; filename="userdata.txt"
|
||||||
|
|
||||||
|
#!/bin/bash
|
||||||
|
# Install Docker
|
||||||
|
if ! command -v docker &> /dev/null
|
||||||
|
then
|
||||||
|
echo "Docker not found, installing..."
|
||||||
|
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh 2>&1 >/dev/null
|
||||||
|
usermod -aG docker ${local.linux_user}
|
||||||
|
newgrp docker
|
||||||
|
else
|
||||||
|
echo "Docker is already installed."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Set up Docker credentials
|
||||||
|
mkdir -p "/home/${local.linux_user}/.docker"
|
||||||
|
if [ -n "${local.docker_config_json_base64}" ]; then
|
||||||
|
# Write the Docker config JSON to disk if it is provided.
|
||||||
|
printf "%s" "${local.docker_config_json_base64}" | base64 -d | tee "/home/${local.linux_user}/.docker/config.json"
|
||||||
|
else
|
||||||
|
# Assume that we're going to use the instance IAM role to pull from the cache repo if we need to.
|
||||||
|
# Set up the ecr credential helper.
|
||||||
|
apt-get update -y && apt-get install -y amazon-ecr-credential-helper
|
||||||
|
mkdir -p .docker
|
||||||
|
printf '{"credsStore": "ecr-login"}' | tee "/home/${local.linux_user}/.docker/config.json"
|
||||||
|
fi
|
||||||
|
chown -R ${local.linux_user}:${local.linux_user} "/home/${local.linux_user}/.docker"
|
||||||
|
|
||||||
|
# Write the container env to disk.
|
||||||
|
printf "%s" "${local.docker_env_list_base64}" | base64 -d | tee "/home/${local.linux_user}/env.txt"
|
||||||
|
|
||||||
|
# Start envbuilder
|
||||||
|
sudo -u coder docker run \
|
||||||
|
--rm \
|
||||||
|
--net=host \
|
||||||
|
-h ${lower(data.coder_workspace.me.name)} \
|
||||||
|
-v /home/${local.linux_user}/envbuilder:/workspaces \
|
||||||
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||||
|
--env-file /home/${local.linux_user}/env.txt \
|
||||||
|
${local.builder_image}
|
||||||
|
--//--
|
||||||
|
EOT
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check for the presence of a prebuilt image in the cache repo
|
||||||
|
# that we can use instead.
|
||||||
|
resource "envbuilder_cached_image" "cached" {
|
||||||
|
count = var.cache_repo == "" ? 0 : data.coder_workspace.me.start_count
|
||||||
|
builder_image = local.devcontainer_builder_image
|
||||||
|
git_url = data.coder_parameter.repo_url.value
|
||||||
|
cache_repo = var.cache_repo
|
||||||
|
extra_env = local.envbuilder_env
|
||||||
|
}
|
||||||
|
|
||||||
|
# This is useful for debugging the startup script. Left here for reference.
|
||||||
|
# resource local_file "startup_script" {
|
||||||
|
# content = local.user_data
|
||||||
|
# filename = "${path.module}/user_data.txt"
|
||||||
|
# }
|
||||||
|
|
||||||
|
resource "aws_instance" "vm" {
|
||||||
|
ami = data.aws_ami.ubuntu.id
|
||||||
|
availability_zone = local.aws_availability_zone
|
||||||
|
instance_type = data.coder_parameter.instance_type.value
|
||||||
|
iam_instance_profile = try(data.aws_iam_instance_profile.vm_instance_profile[0].name, null)
|
||||||
|
root_block_device {
|
||||||
|
volume_size = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
user_data = local.user_data
|
||||||
|
tags = {
|
||||||
|
Name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
|
||||||
|
# Required if you are using our example policy, see template README
|
||||||
|
Coder_Provisioned = "true"
|
||||||
|
}
|
||||||
|
lifecycle {
|
||||||
|
ignore_changes = [ami]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_ec2_instance_state" "vm" {
|
||||||
|
instance_id = aws_instance.vm.id
|
||||||
|
state = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
|
||||||
|
}
|
||||||
|
|
||||||
resource "coder_agent" "dev" {
|
resource "coder_agent" "dev" {
|
||||||
count = data.coder_workspace.me.start_count
|
count = data.coder_workspace.me.start_count
|
||||||
arch = "amd64"
|
arch = "amd64"
|
||||||
@@ -99,85 +311,38 @@ resource "coder_agent" "dev" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "coder_metadata" "info" {
|
||||||
|
count = data.coder_workspace.me.start_count
|
||||||
|
resource_id = coder_agent.dev[0].id
|
||||||
|
item {
|
||||||
|
key = "ami"
|
||||||
|
value = aws_instance.vm.ami
|
||||||
|
}
|
||||||
|
item {
|
||||||
|
key = "availability_zone"
|
||||||
|
value = local.aws_availability_zone
|
||||||
|
}
|
||||||
|
item {
|
||||||
|
key = "instance_type"
|
||||||
|
value = data.coder_parameter.instance_type.value
|
||||||
|
}
|
||||||
|
item {
|
||||||
|
key = "ssh_pubkey"
|
||||||
|
value = data.coder_parameter.ssh_pubkey.value
|
||||||
|
}
|
||||||
|
item {
|
||||||
|
key = "repo_url"
|
||||||
|
value = data.coder_parameter.repo_url.value
|
||||||
|
}
|
||||||
|
item {
|
||||||
|
key = "devcontainer_builder"
|
||||||
|
value = data.coder_parameter.devcontainer_builder.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module "code-server" {
|
module "code-server" {
|
||||||
count = data.coder_workspace.me.start_count
|
count = data.coder_workspace.me.start_count
|
||||||
source = "https://registry.coder.com/modules/code-server"
|
source = "https://registry.coder.com/modules/code-server"
|
||||||
|
version = "1.0.18"
|
||||||
agent_id = coder_agent.dev[0].id
|
agent_id = coder_agent.dev[0].id
|
||||||
}
|
}
|
||||||
|
|
||||||
locals {
|
|
||||||
linux_user = "coder"
|
|
||||||
user_data = <<-EOT
|
|
||||||
Content-Type: multipart/mixed; boundary="//"
|
|
||||||
MIME-Version: 1.0
|
|
||||||
|
|
||||||
--//
|
|
||||||
Content-Type: text/cloud-config; charset="us-ascii"
|
|
||||||
MIME-Version: 1.0
|
|
||||||
Content-Transfer-Encoding: 7bit
|
|
||||||
Content-Disposition: attachment; filename="cloud-config.txt"
|
|
||||||
|
|
||||||
#cloud-config
|
|
||||||
cloud_final_modules:
|
|
||||||
- [scripts-user, always]
|
|
||||||
hostname: ${lower(data.coder_workspace.me.name)}
|
|
||||||
users:
|
|
||||||
- name: ${local.linux_user}
|
|
||||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
|
||||||
shell: /bin/bash
|
|
||||||
|
|
||||||
--//
|
|
||||||
Content-Type: text/x-shellscript; charset="us-ascii"
|
|
||||||
MIME-Version: 1.0
|
|
||||||
Content-Transfer-Encoding: 7bit
|
|
||||||
Content-Disposition: attachment; filename="userdata.txt"
|
|
||||||
|
|
||||||
#!/bin/bash
|
|
||||||
# Install Docker
|
|
||||||
if ! command -v docker &> /dev/null
|
|
||||||
then
|
|
||||||
echo "Docker not found, installing..."
|
|
||||||
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh 2>&1 >/dev/null
|
|
||||||
usermod -aG docker ${local.linux_user}
|
|
||||||
newgrp docker
|
|
||||||
else
|
|
||||||
echo "Docker is already installed."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Start envbuilder
|
|
||||||
docker run --rm \
|
|
||||||
-h ${lower(data.coder_workspace.me.name)} \
|
|
||||||
-v /home/${local.linux_user}/envbuilder:/workspaces \
|
|
||||||
-e CODER_AGENT_TOKEN="${try(coder_agent.dev[0].token, "")}" \
|
|
||||||
-e CODER_AGENT_URL="${data.coder_workspace.me.access_url}" \
|
|
||||||
-e GIT_URL="${data.coder_parameter.repo_url.value}" \
|
|
||||||
-e INIT_SCRIPT="echo ${base64encode(try(coder_agent.dev[0].init_script, ""))} | base64 -d | sh" \
|
|
||||||
-e FALLBACK_IMAGE="codercom/enterprise-base:ubuntu" \
|
|
||||||
ghcr.io/coder/envbuilder
|
|
||||||
--//--
|
|
||||||
EOT
|
|
||||||
}
|
|
||||||
|
|
||||||
resource "aws_instance" "vm" {
|
|
||||||
ami = data.aws_ami.ubuntu.id
|
|
||||||
availability_zone = "${module.aws_region.value}a"
|
|
||||||
instance_type = data.coder_parameter.instance_type.value
|
|
||||||
root_block_device {
|
|
||||||
volume_size = 30
|
|
||||||
}
|
|
||||||
|
|
||||||
user_data = local.user_data
|
|
||||||
tags = {
|
|
||||||
Name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
|
|
||||||
# Required if you are using our example policy, see template README
|
|
||||||
Coder_Provisioned = "true"
|
|
||||||
}
|
|
||||||
lifecycle {
|
|
||||||
ignore_changes = [ami]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resource "aws_ec2_instance_state" "vm" {
|
|
||||||
instance_id = aws_instance.vm.id
|
|
||||||
state = data.coder_workspace.me.transition == "start" ? "running" : "stopped"
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -172,6 +172,9 @@ locals {
|
|||||||
# The startup script will write this to a file, which the Docker run command will reference.
|
# The startup script will write this to a file, which the Docker run command will reference.
|
||||||
docker_env_list_base64 = base64encode(join("\n", [for k, v in local.docker_env_input : "${k}=${v}"]))
|
docker_env_list_base64 = base64encode(join("\n", [for k, v in local.docker_env_input : "${k}=${v}"]))
|
||||||
|
|
||||||
|
# Builder image will either be the builder image parameter, or the cached image, if cache is provided.
|
||||||
|
builder_image = try(envbuilder_cached_image.cached[0].image, data.coder_parameter.devcontainer_builder.value)
|
||||||
|
|
||||||
# The GCP VM needs a startup script to set up the environment and start the container. Defining this here.
|
# The GCP VM needs a startup script to set up the environment and start the container. Defining this here.
|
||||||
# NOTE: make sure to test changes by uncommenting the local_file resource at the bottom of this file
|
# NOTE: make sure to test changes by uncommenting the local_file resource at the bottom of this file
|
||||||
# and running `terraform apply` to see the generated script. You should also run shellcheck on the script
|
# and running `terraform apply` to see the generated script. You should also run shellcheck on the script
|
||||||
@@ -214,7 +217,7 @@ locals {
|
|||||||
-v /home/${local.linux_user}/envbuilder:/workspaces \
|
-v /home/${local.linux_user}/envbuilder:/workspaces \
|
||||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||||
--env-file /home/${local.linux_user}/env.txt \
|
--env-file /home/${local.linux_user}/env.txt \
|
||||||
${data.coder_parameter.devcontainer_builder.value}
|
${local.builder_image}
|
||||||
META
|
META
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,7 +315,7 @@ resource "coder_agent" "dev" {
|
|||||||
module "code-server" {
|
module "code-server" {
|
||||||
count = data.coder_workspace.me.start_count
|
count = data.coder_workspace.me.start_count
|
||||||
source = "registry.coder.com/modules/code-server/coder"
|
source = "registry.coder.com/modules/code-server/coder"
|
||||||
version = "1.0.17"
|
version = "1.0.18"
|
||||||
agent_id = coder_agent.dev[0].id
|
agent_id = coder_agent.dev[0].id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user