|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + coder = { |
| 4 | + source = "coder/coder" |
| 5 | + version = "0.2.1" |
| 6 | + } |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +variable "access_key" { |
| 11 | + description = <<EOT |
| 12 | +Create an AWS access key to provision resources with Coder: |
| 13 | +- https://console.aws.amazon.com/iam/home#/users |
| 14 | + |
| 15 | +AWS Access Key |
| 16 | +EOT |
| 17 | + sensitive = true |
| 18 | +} |
| 19 | + |
| 20 | +variable "secret_key" { |
| 21 | + description = <<EOT |
| 22 | +AWS Secret Key |
| 23 | +EOT |
| 24 | + sensitive = true |
| 25 | +} |
| 26 | + |
| 27 | +variable "region" { |
| 28 | + description = "What region should your workspace live in?" |
| 29 | + default = "us-east-1" |
| 30 | + validation { |
| 31 | + condition = contains(["us-east-1", "us-east-2", "us-west-1", "us-west-2"], var.region) |
| 32 | + error_message = "Invalid region!" |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +variable "disk_size" { |
| 37 | + description = "Specify your disk size (GiBs)" |
| 38 | + default = "20" |
| 39 | + type = number |
| 40 | + validation { |
| 41 | + condition = ( |
| 42 | + var.disk_size >= 8 && |
| 43 | + var.disk_size <= 256 |
| 44 | + ) |
| 45 | + error_message = "Disk size must be between 8 and 256." |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +provider "aws" { |
| 50 | + region = var.region |
| 51 | + access_key = var.access_key |
| 52 | + secret_key = var.secret_key |
| 53 | +} |
| 54 | + |
| 55 | +data "coder_workspace" "me" { |
| 56 | +} |
| 57 | + |
| 58 | +data "coder_agent_script" "dev" { |
| 59 | + arch = "amd64" |
| 60 | + auth = "aws-instance-identity" |
| 61 | + os = "linux" |
| 62 | +} |
| 63 | + |
| 64 | +data "aws_ami" "ubuntu" { |
| 65 | + most_recent = true |
| 66 | + filter { |
| 67 | + name = "name" |
| 68 | + values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] |
| 69 | + } |
| 70 | + filter { |
| 71 | + name = "virtualization-type" |
| 72 | + values = ["hvm"] |
| 73 | + } |
| 74 | + owners = ["099720109477"] # Canonical |
| 75 | +} |
| 76 | + |
| 77 | +resource "coder_agent" "dev" { |
| 78 | + count = data.coder_workspace.me.transition == "start" ? 1 : 0 |
| 79 | + instance_id = aws_instance.dev[0].id |
| 80 | +} |
| 81 | + |
| 82 | +locals { |
| 83 | + |
| 84 | + # User data is used to stop/start AWS instances. See: |
| 85 | + # https://github.com/hashicorp/terraform-provider-aws/issues/22 |
| 86 | + |
| 87 | + user_data_start = <<EOT |
| 88 | +Content-Type: multipart/mixed; boundary="//" |
| 89 | +MIME-Version: 1.0 |
| 90 | +
|
| 91 | +--// |
| 92 | +Content-Type: text/cloud-config; charset="us-ascii" |
| 93 | +MIME-Version: 1.0 |
| 94 | +Content-Transfer-Encoding: 7bit |
| 95 | +Content-Disposition: attachment; filename="cloud-config.txt" |
| 96 | +
|
| 97 | +#cloud-config |
| 98 | +cloud_final_modules: |
| 99 | +- [scripts-user, always] |
| 100 | +
|
| 101 | +--// |
| 102 | +Content-Type: text/x-shellscript; charset="us-ascii" |
| 103 | +MIME-Version: 1.0 |
| 104 | +Content-Transfer-Encoding: 7bit |
| 105 | +Content-Disposition: attachment; filename="userdata.txt" |
| 106 | +
|
| 107 | +#!/bin/bash |
| 108 | +sudo -E -u ubuntu sh -c '${data.coder_agent_script.dev.value}' |
| 109 | +--//-- |
| 110 | +EOT |
| 111 | + |
| 112 | + user_data_end = <<EOT |
| 113 | +Content-Type: multipart/mixed; boundary="//" |
| 114 | +MIME-Version: 1.0 |
| 115 | +
|
| 116 | +--// |
| 117 | +Content-Type: text/cloud-config; charset="us-ascii" |
| 118 | +MIME-Version: 1.0 |
| 119 | +Content-Transfer-Encoding: 7bit |
| 120 | +Content-Disposition: attachment; filename="cloud-config.txt" |
| 121 | +
|
| 122 | +#cloud-config |
| 123 | +cloud_final_modules: |
| 124 | +- [scripts-user, always] |
| 125 | +
|
| 126 | +--// |
| 127 | +Content-Type: text/x-shellscript; charset="us-ascii" |
| 128 | +MIME-Version: 1.0 |
| 129 | +Content-Transfer-Encoding: 7bit |
| 130 | +Content-Disposition: attachment; filename="userdata.txt" |
| 131 | +
|
| 132 | +#!/bin/bash |
| 133 | +sudo shutdown -h now |
| 134 | +--//-- |
| 135 | +EOT |
| 136 | +} |
| 137 | + |
| 138 | +resource "aws_instance" "dev" { |
| 139 | + ami = data.aws_ami.ubuntu.id |
| 140 | + availability_zone = "${var.region}a" |
| 141 | + instance_type = "t3.micro" |
| 142 | + count = 1 |
| 143 | + |
| 144 | + user_data = data.coder_workspace.me.transition == "start" ? local.user_data_start : local.user_data_end |
| 145 | + tags = { |
| 146 | + Name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}" |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments