» fastly_service_dynamic_snippet_content_v1

Defines content that represents blocks of VCL logic that is inserted into your service. This resource will populate the content of a dynamic snippet and allow it to be manged without the creation of a new service verison.

If Terraform is being used to populate the initial content of a dynamic snippet which you intend to manage via the API, then the lifecycle ignore_changes field can be used with the resource. An example of this configuration is provided below.

» Example Usage

Basic usage:

resource "fastly_service_v1" "myservice" {
 name = "snippet_test"

 domain {
   name    = "snippet.fastlytestdomain.com"
   comment = "snippet test"
 }

 backend {
   address = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
   name    = "AWS S3 hosting"
   port    = 80
 }

 dynamicsnippet {
   name     = "My Dynamic Snippet"
   type     = "recv"
   priority = 110
 }

 default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"

 force_destroy = true
}

resource "fastly_service_dynamic_snippet_content_v1" "my_dyn_content" {

 service_id = fastly_service_v1.myservice.id
 snippet_id = { for s in fastly_service_v1.myservice.dynamicsnippet : s.name => s.snippet_id }["My Dynamic Snippet"]

 content = "if ( req.url ) {\n set req.http.my-snippet-test-header = \"true\";\n}"

}

Multiple dynamic snippets:

resource "fastly_service_v1" "myservice" {
 name = "snippet_test"

 domain {
   name    = "snippet.fastlytestdomain.com"
   comment = "snippet test"
 }

 backend {
   address = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
   name    = "AWS S3 hosting"
   port    = 80
 }

 dynamicsnippet {
   name     = "My Dynamic Snippet One"
   type     = "recv"
   priority = 110
 }

 dynamicsnippet {
      name     = "My Dynamic Snippet Two"
      type     = "recv"
      priority = 110
    }

 default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"

 force_destroy = true
}

resource "fastly_service_dynamic_snippet_content_v1" "my_dyn_content_one" {

 service_id = fastly_service_v1.myservice.id
 snippet_id = { for s in fastly_service_v1.myservice.dynamicsnippet : s.name => s.snippet_id }["My Dynamic Snippet One"]

 content = "if ( req.url ) {\n set req.http.my-snippet-test-header-one = \"true\";\n}"

}

resource "fastly_service_dynamic_snippet_content_v1" "my_dyn_content_two" {

 service_id = fastly_service_v1.myservice.id
 snippet_id = { for s in fastly_service_v1.myservice.dynamicsnippet : s.name => s.snippet_id }["My Dynamic Snippet Two"]

 content = "if ( req.url ) {\n set req.http.my-snippet-test-header-two = \"true\";\n}"

}

» Supporting API dynamic snippet updates with ignore_changes

The following example demonstrates how the lifecycle ignore_changes field can be used to suppress updates against the content in a dynamic snippet. If, after your first deploy, the Fastly API is to be used to manage items in a dynamic snippet, then this will stop Terraform realigning the remote state with the initial content defined in your HCL.

...

resource "fastly_service_dynamic_snippet_content_v1" "my_dyn_content" {

  service_id = fastly_service_v1.myservice.id
  snippet_id = { for s in fastly_service_v1.myservice.dynamicsnippet : s.name => s.snippet_id }["My Dynamic Snippet"]

  content = "if ( req.url ) {\n set req.http.my-snippet-test-header = \"true\";\n}"

  lifecycle {
    ignore_changes = [content, ]
  }
}

» Argument Reference

The following arguments are supported:

  • service_id - (Required) The ID of the service that the dynamic snippet belongs to
  • snippet_id - (Required) The ID of the dynamic snippet that the content belong to
  • content - (Required) The VCL code that specifies exactly what the snippet does.

» Attributes Reference

» Import

This is an example of the import command being applied to the resource named fastly_service_dynamic_snippet_content_v1.content The resource ID is a combined value of the service_id and snippet_id separated by a forward slash.

$ terraform import fastly_service_dynamic_snippet_content_v1.content xxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxx

If Terraform is already managing remote content against a resource being imported then the user will be asked to remove it from the existing Terraform state. The following is an example of the Terraform state command to remove the resource named fastly_service_dynamic_snippet_content_v1.content from the Terraform state file.

$ terraform state rm fastly_service_dynamic_snippet_content_v1.content