Skip to content

Instantly share code, notes, and snippets.

@rssnyder
Created April 23, 2025 16:29
Show Gist options
  • Save rssnyder/93580ec73a075a8689b2ef17239f541a to your computer and use it in GitHub Desktop.
Save rssnyder/93580ec73a075a8689b2ef17239f541a to your computer and use it in GitHub Desktop.
Create a Harness Delegate using K3s on an EC2 instance
terraform {
required_providers {
harness = {
source = "harness/harness"
}
aws = {
source = "hashicorp/aws"
}
}
}
variable "instance_type" {
type = string
default = "t3.small"
description = "Size of EC2 to provision. You will need at least 1vcpu and 2g memory"
}
variable "ami" {
type = string
description = "AMI for the instance"
}
variable "vpc_security_group_ids" {
type = list(string)
description = "Security groups to add to the instance"
}
variable "subnet_id" {
type = string
description = "Subnet to place the instance in"
}
variable "delegate_name" {
type = string
default = "k3s"
description = "Name for the Harness Delegate"
}
variable "delegate_docker_image" {
type = string
default = "Delegate image to use"
}
variable "harness_org_id" {
type = string
default = null
description = "Organization ID when creating an org or project level delegate"
}
variable "harness_project_id" {
type = string
default = null
description = "Project ID when creating a project level delegate"
}
locals {
userdata = <<EOF
#!/bin/bash
# setup k3s
curl -sfL https://get.k3s.io | sh -
# install helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# deploy delegate
helm repo add harness-delegate https://app.harness.io/storage/harness-download/delegate-helm-chart/
helm repo update harness-delegate
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
helm upgrade -i helm-delegate --namespace harness-delegate-ng --create-namespace \
harness-delegate/harness-delegate-ng \
--set delegateName=${var.delegate_name} \
--set accountId=${data.harness_platform_current_account.current.id} \
--set delegateToken=${harness_platform_delegatetoken.k3s.value} \
--set managerEndpoint=${data.harness_platform_current_account.current.endpoint} \
--set delegateDockerImage=${var.delegate_docker_image} \
--set replicas=1 --set memory=1024
EOF
}
data "harness_platform_current_account" "current" {}
resource "harness_platform_delegatetoken" "k3s" {
name = var.delegate_name
account_id = data.harness_platform_current_account.current.id
org_id = var.harness_org_id
project_id = var.harness_project_id
}
resource "aws_instance" "k3s" {
ami = var.ami
instance_type = var.instance_type
vpc_security_group_ids = var.vpc_security_group_ids
subnet_id = var.subnet_id
user_data = local.userdata
tags = {
Name = var.delegate_name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment