diff --git a/2-terraform-boundary/README.md b/2-terraform-boundary/README.md new file mode 100644 index 0000000..2f942f3 --- /dev/null +++ b/2-terraform-boundary/README.md @@ -0,0 +1,181 @@ + +# Part 2: Say Hello to terrafom and AWS + +## Background + +Our Hello app is already running with following limitations +1. **Unreliable Infrastructure**: Single-instance services with hardcoded IPs. +2. **No release Package**: Code is deployed directly by compiling code. This limits the Portability, Env Consistencey, Faster Deployment, Scalability, Isolation, Enhanced Security, Version Controlled... +3. **Lack of High Availability**: Only one instance of each service. +4. **Limited Scalability**: Hardcoded IPs makes the setup extremly difficult to `scale-up`, `scale-down`. +5. **No Fault Tolerance**: Services may fail without recovery mechanisms. +6. **Insecure Secret Management**: Secrets are hardcoded and not securely handled. +7. **Rigid Deployment**: Fixed configurations with minimal flexibility. + + +## Overview +We will be targetting to solve the problem of unreliable infra by deploying this app in AWS cloud. + +The other big Challenge with using cloud is Infrastructure Management. +#### List if infrastructure items +- Auto selecting the latest ubuntu image. +- Creating Security Group with ingress and egress defined. +- 2 AWS Instance to host the application with docker installed +- One private key to SSH the two AWS Instance +- Inject the environment variable `TF_VAR_dockerhub_id` into Response Service +- Configuring and intalling necessary applications. +- (We will do it manually) Auto running the application + +# Proposal +AWS Cloud platformn provides a reliable infrasture but there are lot of componets and configurations to manage manually. Terraform is popular IAC platform to manage the infrastructure as a code. + +--- + +## Infrastructure on AWS + +### 1. **Understanding Terraform** +Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define, provision, and manage cloud infrastructure using declarative configuration files. Terraform is cloud-agnostic and can manage infrastructure for major providers like AWS, Azure, GCP, etc., as well as on-prem solutions. + +Here’s a breakdown of the three main files often used in a Terraform project: + +1. **main.tf** +This is the core file where you define the infrastructure resources. It includes the provider configuration, resource blocks, and possibly some modules. It essentially describes what infrastructure you want. + +2. **variables.tf** +This file is used to declare variables that can be referenced in the main.tf file. Variables allow for flexible and reusable configurations. + +3. **output.tf** +This file defines outputs that Terraform will display after applying the configuration. Outputs are useful for retrieving information about created resources. + +### 2. **Setup and AWS Auth** +```bash +cd 2-terraform +``` + +Open a terminal and run below commands in sequence +```bash + +# Set up Docker Hub credentials +export TF_VAR_dockerhub_id= +curl -L https://hub.docker.com/v2/orgs/$TF_VAR_dockerhub_id | jq +# make sure you see your account information in resposne + +# set the AWS credentials from doormat (Note: These credentials are short lived hence you may need to redo this steps) +export AWS_ACCESS_KEY_ID=REDACTED +export AWS_SECRET_ACCESS_KEY=REDACTED +export AWS_SESSION_TOKEN=REDACTED + +``` + +### 3. **Spinning up the Infrastructure** + +```bash +terraform init +terraform apply + +``` + +Sample Outputs: +``` +env = <> /etc/environment + echo "export TF_VAR_dockerhub_id=${var.dockerhub_id}" | sudo tee --append /home/ubuntu/.bashrc + + systemctl start docker + docker run -d --name 'response_service' -p 6060:6060 ${var.dockerhub_id}/responseservice:latest + EOF + + tags = merge( + { + "Name" = "minion-chat-response-service" + } + ) + + vpc_security_group_ids = [aws_security_group.minion_chat_security_group.id] +} + +resource "tls_private_key" "pk" { + algorithm = "RSA" + rsa_bits = 4096 +} + +resource "aws_key_pair" "minion-key" { + key_name = "minion-key" + public_key = tls_private_key.pk.public_key_openssh +} + +resource "local_file" "minion-key" { + content = tls_private_key.pk.private_key_pem + filename = "./minion-key.pem" + file_permission = "0400" +} diff --git a/2-terraform-boundary/outputs.tf b/2-terraform-boundary/outputs.tf new file mode 100644 index 0000000..5eb4019 --- /dev/null +++ b/2-terraform-boundary/outputs.tf @@ -0,0 +1,9 @@ +output "response_service_private_ip" { + value = "${aws_instance.response_service.private_ip}" +} + +output "ssh_boundary_worker" { + value = < boundary_output.log 2>&1 & + +cd /home/ubuntu/ +sudo apt-get update -y && sudo apt-get install -y jq unzip +wget -q "$(curl -fsSL "https://api.releases.hashicorp.com/v1/releases/boundary/latest?license_class=enterprise" | jq -r '.builds[] | select(.arch == "amd64" and .os == "linux") | .url')" +unzip -o *.zip +echo "Boundary setup completed" + +echo "Starting boundary server" +sudo ./boundary server -config="/home/ubuntu/pki-worker.hcl" > boundary_output.log 2>&1 & +echo "Boundary server started" \ No newline at end of file