99.9% robbed from https://github.com/admingeekz/varnish-wordpress
You will need an AMI running Varnish Enterprise 6.
This comes with vmod_goto
,
which will allow your Varnish cache to connect to a dynamic backend, such as an
Application Load Balancer. You can use this to front
an auto-scaling group stuffed with (stateless) Wordpress servers.
You'll also need a Launch Configuration
which replaces the string {{ALB_HOSTNAME}}
with the DNS name of the load balancer fronting your Wordpress ASG.
Consider the following architecture:
+------------------------------+
0 | Application load balancer |
+------------------------------+
|
+------------------------------+
1 | Varnish auto-scaling group |
+------------------------------+
|
+------------------------------+
2 | Application load balancer |
+------------------------------+
|
+------------------------------+
3 | Wordpress auto-scaling group |
+------------------------------+
The Varnish auto-scaling group will need to be configured to accept traffic from the listener at 0, and to forward traffic to the listener at 2, so each instance will need to launch with a configuration like the following:
"VarnishLaunchConfig": {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"configSets" : {
"varnish_install" : ["configure_varnish"]
},
"configure_varnish" : {
"files" : {
"/etc/varnish/default.vcl" : {
"source" : "https://raw.githubusercontent.com/porcospino/varnish-wordpress/master/default.vcl",
"context" : { "ALB_HOSTNAME" : { "Fn::GetAtt" : [ "PrivateLoadBalancer", "DNSName" ] } },
}
},
"commands" : {
"01enable_varnish" : {
"command" : "systemctl enable varnish.service"
},
"02restart_varnish" : {
"command" : "systemctl restart varnish.service"
}
}
}
}
}
},
"PrivateLoadBalancer" : {
"Type" : "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties" : {
"Scheme": "internal"
[ ... ]