Skip to content

Commit

Permalink
Enhance README and add Terraform module for EKS cluster deployment wi…
Browse files Browse the repository at this point in the history
…th automated GitHub Actions workflows; include tests for workflow triggers and validation.
  • Loading branch information
arnol377 committed Apr 8, 2025
1 parent a3e4476 commit c2fd5b3
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 1 deletion.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Tests

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]

jobs:
terraform-test:
name: Terraform Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.7.0"

- name: Run Terraform Tests
working-directory: ./examples/basic
run: terraform test
env:
TF_VAR_github_token: mock-token

python-test:
name: Python Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest requests
- name: Run Python Tests
working-directory: ./scripts
run: pytest -v test_trigger_workflow.py
163 changes: 163 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,165 @@
# terraform-eks-deployment

Terraform module for EKS cluster deployment and configuration

## Overview

This module creates a new GitHub repository for your EKS cluster based on the template-eks-cluster repository. It sets up all necessary configuration files and triggers automated workflows for cluster deployment.

## Prerequisites

- GitHub token with repository and workflow permissions
- Python 3.x installed on the machine running Terraform
- Access to GitHub Enterprise (if using enterprise version)

## Usage

```hcl
module "eks_deployment" {
source = "path/to/terraform-eks-deployment"
name = "my-eks-cluster"
organization = "my-org"
environment = "production"
region = "us-east-1"
cluster_config = {
cluster_name = "prod-eks-01"
account_name = "prod-account"
aws_account_id = "123456789012"
aws_profile = "prod-profile"
environment_abbr = "prod"
vpc_name = "prod-vpc"
vpc_domain_name = "prod.example.com"
}
github_token = "your-github-token"
github_server_url = "https://github.mycompany.com" # Optional, for GitHub Enterprise
}
```

## Workflow Automation

### Overview

The module automatically triggers GitHub Actions workflows in your newly created repository to:
1. Install Python requirements
2. Execute Terragrunt operations for cluster management

### Workflow Sequence

1. **Repository Creation**: The module creates a new repository from the template-eks-cluster template
2. **Initial Configuration**: Configuration files are generated based on your inputs
3. **Requirements Installation**: A workflow is triggered to install Python dependencies
4. **Cluster Planning**: A terragrunt plan workflow is automatically triggered

### Available Workflows

Your new repository will have these workflows available:

1. **Install Requirements** (`install-requirements.yml`)
- Triggered automatically on repository creation
- Installs all Python dependencies from requirements.txt

2. **Terragrunt Cluster Operations** (`terragrunt-cluster-build.yml`)
- Supports plan, apply, and destroy operations
- Can be triggered manually or via API
- Includes safety checks and approvals

### Triggering Workflows

The workflows can be triggered in two ways:

1. **Automatic Triggering**
- On repository creation, the module automatically triggers:
1. Requirements installation
2. Initial cluster plan

2. **Manual Triggering**
- Via GitHub UI:
1. Go to Actions tab
2. Select desired workflow
3. Click "Run workflow"
4. Fill in parameters

3. **API Triggering**
- Use GitHub's API to trigger workflows:
```bash
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/OWNER/REPO/dispatches" \
-d '{
"event_type": "cluster-plan",
"client_payload": {
"environment": "dev",
"region": "us-east-1",
"cluster_dir": "my-cluster",
"auto_approve": false
}
}'
```

### Supported Events

The following event types are supported for workflow triggers:

- `install-requirements`: Install Python dependencies
- `cluster-plan`: Preview cluster changes
- `cluster-apply`: Apply cluster changes
- `cluster-destroy`: Destroy cluster

### Required Secrets

The following secrets must be configured in your repository:

- `AWS_ROLE_ARN`: ARN of the AWS role to assume
- `GITHUB_TOKEN`: GitHub token with workflow permissions

## Module Configuration

### Required Variables

- `name`: Repository name
- `organization`: GitHub organization name
- `environment`: Deployment environment
- `region`: AWS region
- `cluster_config`: Cluster configuration object
- `github_token`: GitHub token for workflow operations

### Optional Variables

- `github_server_url`: GitHub Enterprise server URL
- `template_repo_org`: Organization containing the template repository
- `enable_modules`: Map of modules to enable in the cluster

For more configuration options, see the variables.tf file.

## Outputs

- `repository_url`: URL of the created repository
- `ssh_clone_url`: SSH clone URL of the repository

## Security Considerations

1. **GitHub Token**: Use a token with minimal required permissions
2. **AWS Role**: Use role-based access with least privilege
3. **Auto-approve**: Use with caution in production environments
4. **Environment Protection**: Configure branch protection rules

## Troubleshooting

Common issues and solutions:

1. **Workflow Trigger Failures**
- Check GitHub token permissions
- Verify GitHub Enterprise URL (if applicable)
- Check network connectivity

2. **Python Requirements**
- Ensure requirements.txt exists in template repository
- Check Python version compatibility

3. **AWS Authentication**
- Verify AWS role ARN
- Check AWS credentials configuration
47 changes: 47 additions & 0 deletions examples/basic/basic.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variables {
github_token = "mock-token"
}

provider "github" {
owner = "my-org"
token = "mock-token"
base_url = "https://github.mycompany.com/api/v3"
}

run "verify_repository_config" {
command = plan

assert {
condition = module.eks_deployment.github_repo.name == "eks-test-cluster"
error_message = "Repository name does not match expected value"
}

assert {
condition = module.eks_deployment.github_repo.description == "EKS Cluster Configuration for dev-eks-01"
error_message = "Repository description does not match expected value"
}

assert {
condition = module.eks_deployment.github_repo.visibility == "private"
error_message = "Repository visibility should be private"
}
}

run "verify_module_defaults" {
command = plan

assert {
condition = length(module.eks_deployment.enable_modules.*.cert_manager) > 0
error_message = "cert-manager module should be enabled"
}

assert {
condition = length(module.eks_deployment.enable_modules.*.prometheus) > 0
error_message = "prometheus module should be enabled"
}

assert {
condition = length(module.eks_deployment.enable_modules.*.grafana) > 0
error_message = "grafana module should be enabled"
}
}
39 changes: 39 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
provider "github" {
# Configuration expected from environment variables:
# GITHUB_TOKEN
# GITHUB_OWNER (optional)
}

module "eks_deployment" {
source = "../../"

name = "eks-test-cluster"
organization = "my-org"
environment = "dev"
region = "us-east-1"

template_repo_org = "my-org"
github_token = var.github_token
github_server_url = "https://github.mycompany.com"

cluster_config = {
cluster_name = "dev-eks-01"
account_name = "dev-account"
aws_account_id = "123456789012"
aws_profile = "dev-profile"
environment_abbr = "dev"
vpc_name = "dev-vpc"
vpc_domain_name = "dev.example.com"
}

enable_modules = {
cert_manager = true
prometheus = true
grafana = true
}

versions = {
cluster_version = "1.27"
eks_module_version = "20.33.1"
}
}
50 changes: 50 additions & 0 deletions examples/basic/validation.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
variables {
github_token = "mock-token"
}

provider "github" {
owner = "my-org"
token = "mock-token"
base_url = "https://github.mycompany.com/api/v3"
}

# Test invalid cluster version
run "invalid_cluster_version" {
command = plan

variables {
versions = {
cluster_version = "1.26" # Test outdated version
}
}

expect_failures = [
var.versions
]
}

# Test missing required variables
run "missing_required_vars" {
command = plan

variables {
name = null # Required variable
}

expect_failures = [
var.name
]
}

# Test invalid environment name
run "invalid_environment" {
command = plan

variables {
environment = "invalid" # Should be dev, staging, or prod
}

expect_failures = [
var.environment
]
}
5 changes: 5 additions & 0 deletions examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "github_token" {
description = "GitHub token for API operations"
type = string
sensitive = true
}
32 changes: 32 additions & 0 deletions examples/basic/workflow_trigger.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variables {
github_token = "mock-token"
}

mock_provider "github" {
mock_resource "github_repository_dispatch" {
defaults = {
result = {
status_code = 204
}
}

assert "verify_workflow_payload" {
condition = self.input.event_type == "cluster-plan" &&
self.input.client_payload.environment == "dev" &&
self.input.client_payload.region == "us-east-1" &&
self.input.client_payload.cluster_dir == "dev-eks-01"
error_message = "Invalid workflow dispatch payload"
}
}
}

run "verify_workflow_trigger" {
command = plan

# This test verifies that the workflow trigger mechanism works
# without actually making API calls
assert {
condition = true
error_message = "Workflow trigger test failed"
}
}
Loading

0 comments on commit c2fd5b3

Please sign in to comment.