diff --git a/Keycloak/README.md b/Keycloak/README.md new file mode 100644 index 0000000..560cca7 --- /dev/null +++ b/Keycloak/README.md @@ -0,0 +1,68 @@ +# Keycloak Server Automation with Terraform + +Terraform configurations for automating the deployment of a Keycloak server on AWS. The deployment script provisions an EC2 instance with Keycloak installed and configured, using specified AWS resources and settings. + +## Prerequisites + +Before you begin, ensure you have the following: + +- **Terraform**: Make sure Terraform is installed on your local machine. You can download it from [Terraform's official website](https://www.terraform.io/downloads.html). + + +## Configuration + +1. Clone the Repository + + ``` + git clone + cd + ``` + +2. Update `terraform.tfvars` + + Edit the terraform.tfvars file with your specific AWS and Keycloak configurations: + +4. Initialize Terraform + + Run the following command to initialize Terraform. This will download the necessary provider plugins: + + ``` + terraform init + ``` +6. Plan the Deployment + + Create an execution plan to review the resources that Terraform will create or modify: + ``` + terraform plan + ``` +7. Apply the Configuration + + Apply the Terraform configuration to create the resources: + ``` + terraform apply + ``` + Confirm the action by typing `yes` when prompted. + +## Keycloak Access +Once the deployment is complete, you can access your Keycloak server using the provided domain. +~~~ +Admin URL: https://prefix-keycloak.test.rancher.space +Admin Username: admin +Admin Password: The password specified in `terraform.tfvars`. +~~~ + +Cleanup: + +To remove the resources created by Terraform, run: +``` +terraform destroy +``` +Confirm the action by typing `yes` when prompted. + +# Important Notice + +This deployment is intended for internal use only and is not suitable for production environments or customer deployments. It is provided as-is, without any warranties or guarantees. There is no official support provided by SUSE for this deployment. + +# Additional Information + +Feel free to customize the content further based on your specific project details and preferences. \ No newline at end of file diff --git a/Keycloak/cloud-init.sh b/Keycloak/cloud-init.sh new file mode 100644 index 0000000..086ad6e --- /dev/null +++ b/Keycloak/cloud-init.sh @@ -0,0 +1,50 @@ +#!/bin/bash +apt update -y +apt install docker* -y +systemctl enable --now docker.service +apt install certbot -y + + +# Request Certificate. +certbot certonly --non-interactive --standalone -d ${keycloak_server_name} --agree-tos -m ${email} + +# Set up Keycloak certificates directory +mkdir -p /opt/keycloak/certs +cp /etc/letsencrypt/live/${keycloak_server_name}/fullchain.pem /opt/keycloak/certs +cp /etc/letsencrypt/live/${keycloak_server_name}/privkey.pem /opt/keycloak/certs +chmod 755 /opt/keycloak/certs +chmod 644 /opt/keycloak/certs/* + + +cat < /opt/keycloak/keycloak.yml +version: '3' +services: + keycloak: + image: quay.io/keycloak/keycloak:latest + container_name: keycloak + restart: always + ports: + - 80:8080 + - 443:8443 + volumes: + - ./certs/fullchain.pem:/etc/x509/https/tls.crt + - ./certs/privkey.pem:/etc/x509/https/tls.key + environment: + - KEYCLOAK_ADMIN=admin + - KEYCLOAK_ADMIN_PASSWORD=${keycloak_password} + - KC_HOSTNAME=${keycloak_server_name} + - KC_HTTPS_CERTIFICATE_FILE=/etc/x509/https/tls.crt + - KC_HTTPS_CERTIFICATE_KEY_FILE=/etc/x509/https/tls.key + command: + - start-dev +EOF + + + +# Install docker compose +curl -SL https://github.com/docker/compose/releases/download/${docker_compose_version}/docker-compose-linux-x86_64 -o /usr/bin/docker-compose +chmod +x /usr/bin/docker-compose + +# Start Keycloak with Docker Compose +cd /opt/keycloak +docker-compose -f /opt/keycloak/keycloak.yml up diff --git a/Keycloak/main.tf b/Keycloak/main.tf new file mode 100644 index 0000000..d46e526 --- /dev/null +++ b/Keycloak/main.tf @@ -0,0 +1,80 @@ +provider "aws" { + region = var.region + access_key = var.aws_access_key + secret_key = var.aws_secret_key +} + +data "aws_route53_zone" "selected" { + name = var.aws_domain + private_zone = false +} + +data "template_file" "keycloak" { + template = file("cloud-init.sh") + vars = { + keycloak_server_name = "${var.instance_suffix}-keycloak.${var.aws_domain}" + keycloak_password = var.keycloak_password + docker_compose_version = var.docker_compose_version + email = var.email + } +} + +resource "aws_instance" "keycloak" { + ami = var.ami_id + instance_type = var.instance_type + subnet_id = var.subnet_id + vpc_security_group_ids = var.security_group_ids + key_name = var.key_name + + associate_public_ip_address = true + + user_data = data.template_file.keycloak.rendered + + tags = { + Name = "${var.instance_suffix}-keycloak" + } +} + +resource "aws_route53_record" "dns" { + zone_id = data.aws_route53_zone.selected.zone_id + name = "${var.instance_suffix}-keycloak" + type = "A" + ttl = 300 + records = [aws_instance.keycloak.public_ip] +} + +# check the keycloak server rediness and print the status + +resource "null_resource" "keycloak_readiness_check" { + provisioner "local-exec" { + command = <