description |
---|
Create AWS Application Load Balancer Custom HTTP Header, 302 Redirects with Query String and Host Headers |
- You need a Registered Domain in AWS Route53 to implement this usecase
- Copy your
terraform-key.pem
file toterraform-manifests/private-key
folder
-
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
- Define different HTTPS Listener Rules for ALB Load Balancer
- 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
- 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
- Rule-3: When Query-String, website=aws-eks redirect to https://www.linkedin.com/in/gdlopezcastillo/
# 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
- Rule-4: When Host Header = azure-aks.devopsincloud.com, redirect to https://linktr.ee/gdlopezcastillo
# 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
# 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
}
}
# Terraform Initialize
terraform init
# Terraform Validate
terraform validate
# Terraform Plan
terraform plan
# Terrform Apply
terraform apply -auto-approve
- Rest Clinets we can use
- https://restninja.io/
- https://www.webtools.services/online-rest-api-client
- https://reqbin.com/
# 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
- When Query-String, website=aws-eks redirect to https://www.linkedin.com/in/gdlopezcastillo/
# Verify Rule-3
https://myapps.devopsincloud.com/?website=aws-eks
Observation:
1. Should Redirect to https://www.linkedin.com/in/gdlopezcastillo/
- When Host Header = azure-aks.devopsincloud.com, redirect to https://linktr.ee/gdlopezcastillo
# Verify Rule-4
http://azure-aks.devopsincloud.com
Observation:
1. Should redirect to https://linktr.ee/gdlopezcastillo
# Destroy Resources
terraform destroy -auto-approve
# Delete Files
rm -rf .terraform*
rm -rf terraform.tfstate