mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
* Pass workspace owner email address to provisioner * Remove owner_email and owner_username fields from agent metadata * Add Git environment variables to example templates * Remove "owner_name" field from provisioner metadata, use username instead * Remove Git configuration from most templates, add documentation * Proofreading/typo fixes from @mafredri * Update example templates to latest version of terraform-provider-coder
158 lines
3.5 KiB
HCL
158 lines
3.5 KiB
HCL
terraform {
|
|
required_providers {
|
|
coder = {
|
|
source = "coder/coder"
|
|
version = "0.4.3"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Last updated 2022-05-31
|
|
# aws ec2 describe-regions | jq -r '[.Regions[].RegionName] | sort'
|
|
variable "region" {
|
|
description = "What region should your workspace live in?"
|
|
default = "us-east-1"
|
|
validation {
|
|
condition = contains([
|
|
"ap-northeast-1",
|
|
"ap-northeast-2",
|
|
"ap-northeast-3",
|
|
"ap-south-1",
|
|
"ap-southeast-1",
|
|
"ap-southeast-2",
|
|
"ca-central-1",
|
|
"eu-central-1",
|
|
"eu-north-1",
|
|
"eu-west-1",
|
|
"eu-west-2",
|
|
"eu-west-3",
|
|
"sa-east-1",
|
|
"us-east-1",
|
|
"us-east-2",
|
|
"us-west-1",
|
|
"us-west-2"
|
|
], var.region)
|
|
error_message = "Invalid region!"
|
|
}
|
|
}
|
|
|
|
variable "instance_type" {
|
|
description = "What instance type should your workspace use?"
|
|
default = "t3.micro"
|
|
validation {
|
|
condition = contains([
|
|
"t3.micro",
|
|
"t3.small",
|
|
"t3.medium",
|
|
"t3.large",
|
|
"t3.xlarge",
|
|
"t3.2xlarge",
|
|
], var.instance_type)
|
|
error_message = "Invalid instance type!"
|
|
}
|
|
}
|
|
|
|
provider "aws" {
|
|
region = var.region
|
|
}
|
|
|
|
data "coder_workspace" "me" {
|
|
}
|
|
|
|
data "aws_ami" "ubuntu" {
|
|
most_recent = true
|
|
filter {
|
|
name = "name"
|
|
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
|
|
}
|
|
filter {
|
|
name = "virtualization-type"
|
|
values = ["hvm"]
|
|
}
|
|
owners = ["099720109477"] # Canonical
|
|
}
|
|
|
|
resource "coder_agent" "dev" {
|
|
arch = "amd64"
|
|
auth = "aws-instance-identity"
|
|
os = "linux"
|
|
}
|
|
|
|
locals {
|
|
|
|
# User data is used to stop/start AWS instances. See:
|
|
# https://github.com/hashicorp/terraform-provider-aws/issues/22
|
|
|
|
user_data_start = <<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
|
|
sudo -u ${local.linux_user} sh -c '${coder_agent.dev.init_script}'
|
|
--//--
|
|
EOT
|
|
|
|
user_data_end = <<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]
|
|
|
|
--//
|
|
Content-Type: text/x-shellscript; charset="us-ascii"
|
|
MIME-Version: 1.0
|
|
Content-Transfer-Encoding: 7bit
|
|
Content-Disposition: attachment; filename="userdata.txt"
|
|
|
|
#!/bin/bash
|
|
sudo shutdown -h now
|
|
--//--
|
|
EOT
|
|
|
|
# Ensure Coder username is a valid Linux username
|
|
linux_user = lower(substr(data.coder_workspace.me.owner, 0, 32))
|
|
|
|
}
|
|
|
|
resource "aws_instance" "dev" {
|
|
ami = data.aws_ami.ubuntu.id
|
|
availability_zone = "${var.region}a"
|
|
instance_type = "${var.instance_type}"
|
|
|
|
user_data = data.coder_workspace.me.transition == "start" ? local.user_data_start : local.user_data_end
|
|
tags = {
|
|
Name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
|
|
# Required if you are using our example policy, see template README
|
|
Coder_Provisioned = "true"
|
|
}
|
|
}
|