This project uses Terraform to create a GKE cluster, and deploy a sample application to it. It also uses modular Terraform structure and is powered by Terratest for IaC tests.
- Google Cloud Platform account
- Terraform
- Terratest
- Google Cloud SDK
- kubectl
-
Clone this repository and navigate to the project
environments/develop
directory. -
Set up a service account for Terraform to use, and download the JSON key file. You can do this by following the instructions in the Google Cloud Platform documentation.
NOTE: Ensure your service account has these roles or for test you can bind owner role to your service account(NOT recommended in production!)
- compute.networks.create
- container.
- Export the path to the JSON key file as an environment variable:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json
Also, you can use credentials
key of google provider to address your json file from your local system. It is commented in providers.tf
for you.
- Initialize the Terraform modules:
terraform init
-
Customize the
environments/your-env/terraform.tfvars
file to your needs. You can change the region, zone, cluster name, and other configuration options here. -
Provision the GKE cluster and apply resources using Terraform:
terraform plan
terraform apply
- (Optional) This project will create a kubeconfig file in your environment directory. You can connect to your kubernetes cluster api-server via kubectl by exporting this variable:
export KUBECONFIG=/path-to-your-kubeconfig-file
# exp: get k8s nodes
kubectl get nodes
Also, you can get your cluster kubeconfig by gcloud command:
gcloud container clusters get-credentials <cluster_name> --region <google_region>
It will run your terraform on specific GCP project and checks some part of your Iac to realize they are working correctly. The tests are written using Gruntwork's Terratest library containing:
- Bringing your terraform code up using terraform apply
- Verify desired kubernetes nodes are up and ready.
- Checks your application service type is
LoadBalancer
- Expected 200 http response code and content on your application service.
- Destroy all the project using terraform destroy
To Run the tests follow these steps:
- Navigate to the
test
directory - Initialize your GO module using these commands:
export GO111MODULE=auto
go mod init test
go mod tidy
- Run your tests by:
go test -v -timeout 40m
Wait to tests run. Next you should see PASS
as a result of your tests.
--- PASS: TestTerraformGcpHelloWorldExample (1870.07s)
PASS
ok test 1870.081s
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.