Using VCL snippets

VCL snippets are short blocks of VCL logic you can include directly in your service configuration. They don't require you to create entire custom VCL files, though you can include snippets in custom VCL when necessary. Our guide about VCL snippets provides details about each type of snippet, including their limitations.

Creating VCL snippets

To create a VCL snippet via the Fastly control panel:

  1. Log in to the Fastly control panel.

  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.

  3. Click Edit configuration and then select the option to clone the active version.

  4. Click VCL.
  5. Click the VCL snippets tab.
  6. Click Add snippet.
  7. Using the Type controls, select the type of snippet you want to create:
    • Select Regular to create a regular VCL snippet, which will always require your current service version to be activated before changes to it will take effect.
    • Select Dynamic to create a dynamic VCL snippet, which will only require your service version to be activated once, at the time of creation. After activation, changes to the contents of the snippet will be applied immediately.
  8. In the Name field, enter an appropriate name (for example, Example Snippet).
  9. Using the Placement controls, select the location in which the snippet should be placed in your VCL as follows:
    • Select init to insert the snippet above all subroutines in your VCL.
    • Select within subroutine to insert the snippet within a specific subroutine and then select the specific subroutine from the Select subroutine menu.
    • Select none (advanced) to insert it manually in custom VCL.
  10. (Optional) In the Priority field, enter the order in which you want the snippet to execute. Lower numbers execute first.
  11. In the VCL editor area, enter the snippet of VCL logic to be inserted for your service version.
  12. Click Add to create the snippet.

Viewing snippets and their code

To view the snippets associated with a service:

  1. Log in to the Fastly control panel.

  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.

  3. Click Edit configuration and then select the option to clone the active version.

  4. Click VCL.
  5. Click the VCL snippets tab. The VCL snippets associated with your service appear on the left side of the tab.

The VCL snippets page includes a list of all snippets included in the VCL used by your service. They appear on the left side of the VCL snippets tab and are grouped by the VCL subroutine in which they appear and sorted by priority. A number appears next to the subroutine name indicating how many snippets exist within it.

To find a snippet when you have a long list of them associated with your service, consider the following:

  • Shorten the snippet list by clicking the carat (^) to the right of each subroutine group name.
  • Search for a specific snippet by clicking the search icon immediately above the list of snippets and then typing the snippet name in the search box that appears.

To view the code associated with a snippet once you've found it, click the snippet name and the code will appear to the right of the name.

Viewing a snippet in generated VCL

To view a specific snippet as it appears in generated VCL using the Fastly control panel:

  1. Log in to the Fastly control panel.

  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.

  3. Click Edit configuration and then select the option to clone the active version.

  4. Click VCL.
  5. Click the VCL snippets tab.
  6. Find the snippet you want in the list of snippets in the left column, click the name of the snippet you want to view.
  7. Click the three dots More icon to the right of the name of the snippet that you want to view and then select Show in generated VCL. The complete VCL for your service appears with that snippet highlighted.

Editing VCL snippets

To edit a snippet via the Fastly control panel, follow these steps.

  1. Regular snippets
  2. Dynamic snippets

To edit a regular VCL snippet, follow these steps.

  1. Log in to the Fastly control panel.

  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.

  3. Click Edit configuration and then select the option to clone the active version.

  4. Click VCL.
  5. Click the VCL snippets tab.
  6. Select the regular VCL snippet you want to edit.
  7. Click the pencil Pencil icon at the top right of the snippet's displayed code. The Edit snippet page appears.
  8. Update the snippet's settings or VCL as appropriate.
  9. Click Save to save your snippet changes.
  10. From the Activate menu, select Activate on Production to activate the changes on your service.

Manually including snippets in custom VCL

Snippets will not be rendered in VCL if you select none (advanced) for the snippet type in the Fastly control panel or specify a location of none for the type parameter in the API. This allows you to manually include snippets in custom VCL using the following syntax:

include "snippet::<snippet name>"

The same VCL Snippet can be included in custom VCL in as many places as needed. Check out our guide to working with custom VCL for additional details.

Deleting a VCL snippet

To delete a snippet via the Fastly control panel:

  1. Log in to the Fastly control panel.

  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.

  3. Click Edit configuration and then select the option to clone the active version.

  4. Click VCL.
  5. Click the three dots More icon to the right of the name of the snippet that you want to delete and then select Delete. A confirmation window appears.
  6. Click Delete.