-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6289fc9
commit 0cca293
Showing
7 changed files
with
605 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,8 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
terraform-* | ||
vendor | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
# terraform-provider-text | ||
Allows for the storing off arbitrary text in the state file | ||
|
||
# Installation | ||
No special configuration is needed. | ||
``` | ||
provider text {} | ||
``` | ||
|
||
# Usage | ||
At its simplest, just hardcode some text. | ||
``` | ||
resource text important_note { | ||
content = "" | ||
} | ||
``` | ||
then if someone changes it then it will show up in the plan: | ||
``` | ||
resource text test_sh { | ||
content = "Next person that runs this need to remember to do the thing" | ||
} | ||
``` | ||
|
||
# The Real Usage | ||
The use case that prompted this provider is passing configuration and configuration scripts to AWS instances. Initially we used the cloudinit template data source but this rapidly grew very large, to the point of hitting the character limit for the instance user_data field. | ||
|
||
The solution we came up with was to drop user_data and, instead, push the configuration scripts to s3 in a provisioner, run Ansible to deploy them and then remove them from s3 in another provisioner. | ||
|
||
This solution has a fairly serious downside though. The configuration scripts are no longer processed by Terraform and so if they change, no change in the infrastructure is generated. Fine if you know about it because you can run the Ansible manually but when that arcane knowledge is lost, it is very easy for new instances to end up configured different from existing hosts. | ||
|
||
What we can do now is simply add a text resource for each config file. The actual deployment still happens via s3 but if the script files change then it flags up in the plan. | ||
|
||
``` | ||
resource text test_sh { | ||
content = file("${path.module}/test.sh") | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/hashicorp/terraform-plugin-sdk | ||
|
||
go 1.15 | ||
|
||
require github.com/hashicorp/terraform-plugin-sdk/v2 v2.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/textprovider" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin" | ||
) | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: func() *schema.Provider { | ||
return textprovider.Provider() | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package textprovider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// Provider ... | ||
func Provider() *schema.Provider { | ||
return &schema.Provider{ | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"text": textEvent(), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package textprovider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func textEvent() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceEventCreate, | ||
Read: resourceEventRead, | ||
Update: resourceEventUpdate, | ||
Delete: resourceEventDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"content": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"stored_content": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceEventCreate(d *schema.ResourceData, meta interface{}) error { | ||
d.SetId("1") | ||
return resourceEventRead(d, meta) | ||
} | ||
|
||
func resourceEventRead(d *schema.ResourceData, meta interface{}) error { | ||
d.Set("stored_content", d.Get("content")) | ||
return nil | ||
} | ||
|
||
func resourceEventUpdate(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} | ||
|
||
func resourceEventDelete(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} |