MOPS Speed Run

jrhicks

Jeffrey Hicks

Posted on December 21, 2023

MOPS Speed Run

Create AWS Member Account

From Root Organization as Root User

  1. From AWS Organizations. Add an AWS account.

  2. From IAM Identity Center > Multi-account permissions > AWS Accounts. Select new account from organizational structure and "Assign users or groups".

  3. From IAM Identity Center > Settings. Navigate to Identity source and note the AWS access portal URL: https://############.awsapps.com/start

Start Code Repo

Get Templates

git clone https://github.com/jrhicks/mops
mv mops prod
cd prod
rm -rf .git
Enter fullscreen mode Exit fullscreen mode

Connect to Newly Created Github Repo

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/YourCompany/prod.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Scaffold IaC

make hygen-cli
make config
code .mops.js
make mops
Enter fullscreen mode Exit fullscreen mode

Setup AWS CLI

The AWS Identity center allows a single user to access multiple accounts. First we install the aws-cli then use it to configure a profile for the newly created account.

make aws-cli
make profile
make login
Enter fullscreen mode Exit fullscreen mode

Terraform Cluster

Terraform a VPC, EKS cluster, and a variety of resources useful for running web applications in EKS.

make tf-cli
make cluster
Enter fullscreen mode Exit fullscreen mode

Operationalize Cluster

Create a Personal Access Token in Github.

Bootstrap GitOps, Install k8s infrastructure, k8s add-ons, and k8s monitoring.

make kube-cli
make flux-cli
git add; git commit -m "k8s cluster"; git push
make flux
Enter fullscreen mode Exit fullscreen mode

Check Results

make dashboard
Enter fullscreen mode Exit fullscreen mode

Image description

Delegate Name Server

Lookup the nameservers for the terraformed Hosted Zone and configure your DNS to use these Name Servers.

make nameservers
Enter fullscreen mode Exit fullscreen mode

Copy output into DNS Provider's Management console.

Image description

Scaffold App's Infrastructure (As Code)

  • AWS Infrastructure (ECR, S3, ACM, SG)
  • K8s Infrastructure (Namespace, Deployments, Service, PodMonitor, Dashboards, Secrets)
  • Flux Infrastructure (Kustomization, Github Snippets, Automations)
make app
Enter fullscreen mode Exit fullscreen mode

Link App into FluxCD by adding generated resources to Kustomizations (FluxCD's Custom Resource For Specifying GitOps Functionality)

  • k8s/apps/base/kustomization.yaml

  • k8s/apps/namespaces/kustomization.yaml

Deploy via GitOps

git add .
git commit -m "my-app infrastructure as code"
git push
Enter fullscreen mode Exit fullscreen mode

Bypass Wait and Trigger Sync

make fsync
Enter fullscreen mode Exit fullscreen mode

Build CI/CD Pipeline

Configure Github to Push to ECR

  • Copy snippets from ./snippets/my-app/ci.yaml to your ci.yaml for your app.

  • Generate AWS Access Keys

make my-app-gh-keys
Enter fullscreen mode Exit fullscreen mode
  • Copy keys to Github. Navigate to repository > Settings > Security > Secrets & Variables > Actions > Repository Secrets

Image description

  • Trigger build
git add .
git commit -m "chore update ci/cd"
git push
Enter fullscreen mode Exit fullscreen mode

Configure Automations

Configure EKS to work with ECR

Deploy Secrets referenced by FluxCD Image Repository Resource

make ecr_credentials_renew
Enter fullscreen mode Exit fullscreen mode

Deploy Secrets to share with Github

make flux-gh-deploy-keys
Enter fullscreen mode Exit fullscreen mode

Paste into Github Repo > Settings > Security > Deploy Keys > Add Deploy Key

[x] Enable Write Access.

Image description

Attach Database

Create an RDS database threw AWS Web Console, set VPC and Security Group.

Generate username and password input into web console, and note them to add to your Rails' encrypted credentials.

make rds-password
Enter fullscreen mode Exit fullscreen mode

Configure Rails

Get AWS Credentials.

make my-app-s3-keys
Enter fullscreen mode Exit fullscreen mode

Load S3 & RDS credentials into Rails encrypted credentials.

cd ..; cd my-rails-app # Go over to App Repo
EDITOR="code --wait" bin/rails credentials:edit
Enter fullscreen mode Exit fullscreen mode
production:
  database:
    username: XXXXXXXX
    password: XXXXXXXXXXXXXXX
  aws:
    access_key_id: XXXXXXXXXXX
    secret_access_key: XXXXXXXXXXX/XXXX
Enter fullscreen mode Exit fullscreen mode

Edit config/storage_production.yaml

amazon_production:
  service: S3
  access_key_id: <%= Rails.application.credentials.production&.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.production&.dig(:aws, :secret_access_key) %>
  region: us-east-1
  bucket: platform-staging-booking-pro-bucket
Enter fullscreen mode Exit fullscreen mode

Edit config/database.yml

production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  database: your_database_name
  username: <%= Rails.application.credentials.dig(:production, :database, :username) %>
  password: <%= Rails.application.credentials.dig(:production, :database, :password) %>
  host: [check aws web console]
  port: 5432
Enter fullscreen mode Exit fullscreen mode

Grab the Rails Master Key from ENV or config/credentials.yml.enc (which is typically kept private via .gitignore) and add this single Rails Maser Key to K8s as a secret.

cd ..; cd prod # Go back to Infrastructure Repo
make my-app-rails-master-key-secret
Enter fullscreen mode Exit fullscreen mode

Setup Rails to Work Behind a Terminating Load Balancer, edit config/production.rb

config.assume_ssl = true
Enter fullscreen mode Exit fullscreen mode

Configure Rails Metrics

Locate the generated snippets in /snippets/your-app and update the following files in your Rails app:

  • /config/initializers/yabeda_prometheus.rb

  • config.ru

  • Gemfile

From your IaC Repo, link the Monitor and Dashboard's into

  • k8s/monitoring/configs/kustomization.yaml

After you deploy the updates, you can view the new Rails Dashboards:

make dashboard
Enter fullscreen mode Exit fullscreen mode

Image description

💖 💪 🙅 🚩
jrhicks
Jeffrey Hicks

Posted on December 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related