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.
Basic usage:
resource "fastly_service_vcl" "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" "my_dyn_content" {
for_each = {
for d in fastly_service_vcl.myservice.dynamicsnippet : d.name => d if d.name == "My Dynamic Snippet"
}
service_id = fastly_service_vcl.myservice.id
snippet_id = each.value.snippet_id
content = "if ( req.url ) {\n set req.http.my-snippet-test-header = \"true\";\n}"
}
Multiple dynamic snippets:
resource "fastly_service_vcl" "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" "my_dyn_content_one" {
for_each = {
for d in fastly_service_vcl.myservice.dynamicsnippet : d.name => d if d.name == "My Dynamic Snippet One"
}
service_id = fastly_service_vcl.myservice.id
snippet_id = each.value.snippet_id
content = "if ( req.url ) {\n set req.http.my-snippet-test-header-one = \"true\";\n}"
}
resource "fastly_service_dynamic_snippet_content" "my_dyn_content_two" {
for_each = {
for d in fastly_service_vcl.myservice.dynamicsnippet : d.name => d if d.name == "My Dynamic Snippet Two"
}
service_id = fastly_service_vcl.myservice.id
snippet_id = each.value.snippet_id
content = "if ( req.url ) {\n set req.http.my-snippet-test-header-two = \"true\";\n}"
}
for_each
attributes were not available in Terraform before 0.12.6, however, users can still use for
expressions to achieve
similar behaviour as seen in the example below.
For scenarios such as adding a Dynamic Snippet to a service and at the same time, creating the Dynamic Snippets (fastly_service_dynamic_snippet_content
)
resource, Terraform will not calculate implicit dependencies correctly on for
expressions. This will result in index lookup
problems and the execution will fail.
For those scenarios, it's recommended to split the changes into two distinct steps:
dynamicsnippet
block to the fastly_service_vcl
and apply the changesfastly_service_dynamic_snippet_content
resource with the for
expressions to the HCL and apply the changesUsage:
resource "fastly_service_vcl" "myservice" {
#...
dynamicsnippet {
name = "My Dynamic Snippet"
type = "recv"
priority = 110
}
#...
}
resource "fastly_service_dynamic_snippet_content" "my_dyn_content" {
service_id = fastly_service_vcl.myservice.id
snippet_id = {for s in fastly_service_vcl.myservice.dynamicsnippet : s.name => s.snippet_id}["My Dynamic Snippet"]
content = "if ( req.url ) {\n set req.http.my-snippet-test-header = \"true\";\n}"
}
manage_snippets
if the state of the snippets driftsBy default the user is opted out from reapplying the original changes if the snippets are managed externally.
The following example demonstrates how the manage_snippets
field can be used to reapply the changes defined in the HCL if the state of the snippets drifts.
When the value is explicitly set to 'true', Terraform will keep the original changes and discard any other changes made under this resource outside of Terraform.
#...
resource "fastly_service_dynamic_snippet_content" "my_dyn_content" {
for_each = {
for d in fastly_service_vcl.myservice.dynamicsnippet : d.name => d if d.name == "My Dynamic Snippet"
}
service_id = fastly_service_vcl.myservice.id
snippet_id = each.value.snippet_id
manage_snippets = true
content = "if ( req.url ) {\n set req.http.my-snippet-test-header = \"true\";\n}"
}
This is an example of the import command being applied to the resource named fastly_service_dynamic_snippet_content.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.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.content
from the Terraform state file.
$ terraform state rm fastly_service_dynamic_snippet_content.content
content
(String) The VCL code that specifies exactly what the snippet doesservice_id
(String) The ID of the service that the dynamic snippet belongs tosnippet_id
(String) The ID of the dynamic snippet that the content belong tomanage_snippets
(Boolean) Whether to reapply changes if the state of the snippets drifts, i.e. if snippets are managed externallyid
(String) The ID of this resource.