<p>I'm experimenting with spinning up a Varnish instance to speed up a slow but static endpoint of a service. The service is already running in a Kubernetes cluster so I'm basing the setup on the official docker image and following the advice of baking the VCL file into the image:</p>
<pre><code>FROM varnish:6.2

COPY default.vcl /etc/varnish/
</code></pre>
<p>I'm starting with a small amount of configuration in <code>default.vcl</code>:</p>
<pre><code>vcl 4.0;

backend default {
        .host = &quot;172.17.0.1:8018&quot;; # IP for local experimentation
}

# only cache '/v1/xyz/...'
sub vcl_recv {
    if (req.url ~ &quot;^/v1/xyz/&quot;) {
        return (hash);
    }
    return (pass);
}
</code></pre>
<p>I would like to be able to declare the target backend in the deployment file, either using env vars or cli args.</p>
<p>The <code>-b</code> flag seemed perfect for the job, but fails with <code>Error: Only one of -b or -f can be specified</code>.</p>
<p>And using <code>std.getenv</code> in the <code>backend</code> block doesn't work either:</p>
<pre><code>import std;

backend default {
        .host = std.getenv(&quot;VARNISH_TAGET_HOST&quot;);
}
</code></pre>
<p>results in</p>
<pre><code>Expected CSTR got 'std'
(program line 369), at
('/etc/varnish/default.vcl' Line 6 Pos 17)
        .host = std.getenv(&quot;VARNISH_TAGET_HOST&quot;);
----------------###------------------------------
</code></pre>
<p>Is there some way (not including <code>sed</code>-like hacks) by which I can configure the backend without hardcoding it into the VCL?</p>

<h2>Varnish Enterprise has dynamic backends</h2>
<p><em>Varnish Cache</em>, the open source version of Varnish, only allows static backend definitions.</p>
<p>The only way you can define backends <em>on-the-fly</em>, is by using <em>Varnish Enterprise</em>, the commercial version of the software.</p>
<p>See <a href="https://docs.varnish-software.com/varnish-cache-plus/vmods/goto/" rel="nofollow noreferrer">https://docs.varnish-software.com/varnish-cache-plus/vmods/goto/</a> for more information about the <em>dynamic backends</em> feature.</p>
<h2>Why -b &amp; -f cannot be combined</h2>
<p>Apparently the <code>-b</code> parameter is a shorthand for the following command:</p>
<pre><code>varnishadm vcl.inline boot &lt;&lt; EOF
vcl 4.1;

backend default {
    .host = &quot;&lt;addr&gt;&quot;;
}
EOF
</code></pre>
<p>So in fact <code>-b</code> already creates and loads <em>VCL</em> in the background, which makes this option mutually exclusive with <code>-f</code></p>
