This repository contains instructions and necessary configuration files to manage TFC resources using Kubernetes Operator.
For more details, check this official page.
It expects you already have a Kubernetes cluster and kubectl
installed.
If you don't have one, follow this instructions to create a EKS cluster in AWS Cloud.
$ helm repo add hashicorp https://helm.releases.hashicorp.com
$ helm repo update
This will create a new namespace 'tfc-operator-system'
helm install \
demo hashicorp/terraform-cloud-operator \
--version 2.0.0-beta8 \
--namespace tfc-operator-system \
--create-namespace
Get TFC Team token and store it as a Kubernetes Secret
$ kubectl create secret generic tfc-operator --from-literal=token=<TFC-Team-Token>
This can be later used as the Environment variables for the TFC workspace
$ kubectl create secret generic aws-access-id \
--from-literal=AWS_ACCESS_KEY_ID=<AWS_ACCESS_KEY_ID_GOES_HERE>
$ kubectl create secret generic aws-secret-access-key \
--from-literal=AWS_SECRET_ACCESS_KEY=<AWS_SECRET_ACCESS_KEY_GOES_HERE>
Update the demo-workspace.yaml
file with necessary Terraform variables and Environment variables
Run the following command to create a TFC Workspace
$ kubectl apply -f demo-workspace.yaml
demo.module.yaml
file contains necessary configuration to issue a Plan and Apply- This takes module name and version as the input along with the necessary variables
Run this following command
$ kubectl patch module demo-module --type=merge --patch '{"spec": {"restartedAt": "'`date -u -Iseconds`'"}}'
- Add the below content to demo-agent.yaml
- Update the organization name in the configuration file
apiVersion: app.terraform.io/v1alpha2
kind: AgentPool
metadata:
name: demo-agent-pool
spec:
name: agent-pool-demo
organization: <tfc-org-name>
token:
secretKeyRef:
name: tfc-operator
key: token
agentDeployment:
replicas: 3
spec:
containers:
- name: tfc-agent
image: "hashicorp/tfc-agent:latest"
agentTokens:
- name: demo-agent
Run the following command to perform Kubernetes apply
$ kubectl apply -f demo-agent.yaml
Now you should be able to see the pods running in the default
namespace using the follwing command.
$ kubectl get pods
You should also be able to see the Agentpool and Agents in the TFC GUI under,
Workspace -> Settings -> Agents
Run this command to delete Agentpool and Agents
$ kubectl delete -f demo-agent.yaml
- tfk8s is a tool that makes it easier to work with the Terraform Kubernetes Provider.
- It is used to migrate existing YAML manifests and use them with Terraform without having to convert YAML to HCL manually.
Install tfk8s
go install github.com/jrhouston/tfk8s@latest
Initialize Terraform to get Kubernetes provider.
$ terraform init
Create execution plan.
$ terraform plan
Execute the actions proposed in a Terraform plan.
$ terraform apply
You should also be able to see the Agentpool and Agents in the TFC GUI under,
Workspace -> Settings -> Agents
Delete AgentPool and Agents
$ terraform destroy