Coder Social home page Coder Social logo

gerardodavidlopezcastillo / tf_alb-httpheaderquerystringredirects_public Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 24 KB

Create AWS Application Load Balancer Custom HTTP Header, 302 Redirects with Query String and Host Headers

Shell 7.83% HCL 92.17%
aws-alb http-redirect infrastructure-as-code query-string routing terraform

tf_alb-httpheaderquerystringredirects_public's Introduction

description
Create AWS Application Load Balancer Custom HTTP Header, 302 Redirects with Query String and Host Headers

AWS ALB Query String, Host Header Redirects and Custom Header Routing

Pre-requisites

  • You need a Registered Domain in AWS Route53 to implement this usecase
  • Copy your terraform-key.pem file to terraform-manifests/private-key folder

Step-01: Introduction

  • We are going to implement four AWS ALB Application HTTPS Listener Rules

  • Rule-1 and Rule-2 will outline the Custom HTTP Header based Routing

  • Rule-3 and Rule-4 will outline the HTTP Redirect using Query String and Host Header based rules

  • Rule-1: custom-header=my-app-1 should go to App1 EC2 Instances

  • Rule-2: custom-header=my-app-2 should go to App2 EC2 Instances

  • Rule-3: When Query-String, website=aws-eks redirect to https://www.linkedin.com/in/gdlopezcastillo/

  • Rule-4: When Host Header = azure-aks.devopsincloud.com, redirect to https://linktr.ee/gdlopezcastillo.

  • Understand about Priority feature for Rules priority = 2

Image

Image

Step-02: c10-02-ALB-application-loadbalancer.tf

  • Define different HTTPS Listener Rules for ALB Load Balancer

Step-02-01: Rule-1: Custom Header Rule for App-1

  • Rule-1: custom-header=my-app-1 should go to App1 EC2 Instances
        # Rule-1: myapp1-rule - custom-header=my-app-1 should go to App1 EC2 Instances
        myapp1-rule = {
          priority = 1
          actions = [{
            type = "weighted-forward"
            target_groups = [
              {
                target_group_key = "mytg1"
                weight           = 1
              }
            ]
            stickiness = {
              enabled  = true
              duration = 3600
            }
          }]
          conditions = [{
            http_header = {
              http_header_name = "custom-header"
              values           = ["app-1", "app1", "my-app-1"]
            }
          }]
        }# End of myapp1-rule

Step-02-02: Rule-2: Custom Header Rule for App-1

  • Rule-2: custom-header=my-app-2 should go to App2 EC2 Instances
        # Rule-2: myapp2-rule - custom-header=my-app-2 should go to App2 EC2 Instances    
        myapp2-rule = {
          priority = 2
          actions = [{
            type = "weighted-forward"
            target_groups = [
              {
                target_group_key = "mytg2"
                weight           = 1
              }
            ]
            stickiness = {
              enabled  = true
              duration = 3600
            }
          }]
          conditions = [{
            http_header = {
              http_header_name = "custom-header"
              values           =  ["app-2", "app2", "my-app-2"]
            }
          }]
        }# End of myapp2-rule Block
  

Step-02-03: Rule-3: Query String Redirect

  # Rule-3: When Query-String, website=aws-eks redirect to https://www.linkedin.com/in/gdlopezcastillo/
        # Rule-3: Query String Redirect Redirect Rule
        my-redirect-query = {
          priority = 3
          actions = [{
            type        = "redirect"
            status_code = "HTTP_302"
            host        = "stacksimplify.com"
            path        = "/aws-eks/"
            query       = ""
            protocol    = "HTTPS"
          }]

          conditions = [{
            query_string = {
              key   = "website"
              value = "aws-eks"
            }
          }]
        }# End of Rule-3 Query String Redirect Redirect Rule

Step-02-04: Rule-4: Host Header Redirect

  # Rule-4: When Host Header = azure-aks.devopsincloud.com, redirect to https://linktr.ee/gdlopezcastillo
        # Rule-4: Host Header Redirect
        my-redirect-hh = {
          priority = 4
          actions = [{
            type        = "redirect"
            status_code = "HTTP_302"
            host        = "stacksimplify.com"
            path        = "/azure-aks/azure-kubernetes-service-introduction/"
            query       = ""
            protocol    = "HTTPS"
          }]

          conditions = [{
            host_header = {
              values = ["azure-aks11.devopsincloud.com"]
            }
          }]
        }# Rule-4: Host Header Redirect 

Step-03: c12-route53-dnsregistration.tf

# DNS Registration 
## Default DNS
resource "aws_route53_record" "default_dns" {
  zone_id = data.aws_route53_zone.mydomain.zone_id 
  name    = "myapps11.devopsincloud.com"
  type    = "A"
  alias {
    name                   = module.alb.dns_name
    zone_id                = module.alb.zone_id
    evaluate_target_health = true
  }  
}

## Testing Host Header - Redirect to External Site from ALB HTTPS Listener Rules
resource "aws_route53_record" "app1_dns" {
  zone_id = data.aws_route53_zone.mydomain.zone_id 
  name    = "azure-aks11.devopsincloud.com"
  type    = "A"
  alias {
    name                   = module.alb.dns_name
    zone_id                = module.alb.zone_id
    evaluate_target_health = true
  }  
}

Step-04: Execute Terraform Commands

# Terraform Initialize
terraform init

# Terraform Validate
terraform validate

# Terraform Plan
terraform plan

# Terrform Apply
terraform apply -auto-approve

Step-06: Verify HTTP Header Based Routing (Rule-1 and Rule-2)

# Verify Rule-1 and Rule-2
https://myapps.devopsincloud.com
custom-header = my-app-1  - Should get the page from App1 
custom-header = my-app-2  - Should get the page from App2

Step-07: Verify Rule-3

# Verify Rule-3
https://myapps.devopsincloud.com/?website=aws-eks 
Observation: 
1. Should Redirect to https://www.linkedin.com/in/gdlopezcastillo/

Step-08: Verify Rule-4

# Verify Rule-4
http://azure-aks.devopsincloud.com
Observation: 
1. Should redirect to https://linktr.ee/gdlopezcastillo

Step-09: Clean-Up

# Destroy Resources
terraform destroy -auto-approve

# Delete Files
rm -rf .terraform*
rm -rf terraform.tfstate

References

tf_alb-httpheaderquerystringredirects_public's People

Contributors

gerardodavidlopezcastillo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.