You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,171 +6,6 @@ Since this provider is rather different than most other provider, it is recommen
6
6
7
7
**Note:** many people use this provider for wrapping APIs of resources that are not supported by existing providers. For an example of using this provider to manage a Github repo resource, see `examples/github-repo`
8
8
9
-
## Configuring the Provider
10
-
The provider can be configured with optional `environment` and `sensitive_environment` attributes. If these are set, then they will be used to configure all resources which rely on them (without triggering a force new update!)
11
-
12
-
```
13
-
provider "shell" {
14
-
environment = {
15
-
AWS_ACCESS_KEY = var.access_key
16
-
AWS_DEFAULT_REGION = var.region
17
-
}
18
-
sensitive_environment = {
19
-
AWS_SECRET_ACCESS_KEY = var.secret_key
20
-
}
21
-
}
22
-
```
23
-
24
-
Additionally, you can configure the provider with an optional `interpreter` and `enable_parallelism` flags. If you do not specify an interpreter, then the default shell for your machine will be used. Meanwhile, `enable_parallelism` defaults to false but can be turned on if you want to speed things up.
25
-
26
-
```
27
-
provider "shell" {
28
-
interpreter = ["/bin/bash", "-c"]
29
-
enable_parallelism = true
30
-
}
31
-
Data Sources
32
-
------------
33
-
34
-
The simplest example is the data source which implements only Read(). Any output to stdout or stderr will show up in the logs, but to save state, you must output a JSON payload to stdout. The last JSON object printed to stdout will be taken to be the output state. The JSON can be a complex nested JSON, but will be flattened into a `map[string]string`. The reason for this is that your JSON payload variables can be accessed from the output map of this resource and used like a normal terraform output, so the value must be a string. You can use the built-in jsondecode() function to read nested JSON values if you really need to.
35
-
36
-
Below is an example of using the data source. The output of `whoami` is stored in a JSON object for the key `user`
37
-
38
-
```
39
-
data "shell_script" "user" {
40
-
lifecycle_commands {
41
-
read = <<-EOF
42
-
echo "{\"user\": \"$(whoami)\"}"
43
-
EOF
44
-
}
45
-
}
46
-
# "user" can be accessed like a normal Terraform map
47
-
output "user" {
48
-
value = data.shell_script.user.output["user"]
49
-
}
50
-
```
51
-
52
-
An apply would output the following:
53
-
54
-
```
55
-
shell_script.user: Creating...
56
-
shell_script.user: Creation complete after 0s [id=bpcs8j5grkris295e4qg]
**Note:** the above example can be a very valuable way to get environment variables or other environment specific information into normal Terraform variables!
65
-
66
-
Another data source example, this time to get the weather in San Francisco:
Resources are a bit more complicated. At a minimum, you must implement the `CREATE`, and `DELETE` lifecycle commands. `READ` and `UPDATE` are optional arguments.
99
-
100
-
* If you choose not to implement the `READ` command, then `CREATE` (and `UPDATE` if you are using it) must output JSON. The local state will not be synced with the actual state, but for many applications that is not a problem.
101
-
102
-
* If you choose not to implement `UPDATE`, then if a change occurs that would trigger an update, the resource will be instead be destroyed and then recreated - same as `ForceNew`. For many applications this is not a problem.
103
-
104
-
I suggest starting off with just `CREATE` and `DELETE` and then implementing `READ` and `UPDATE` as needed. If you choose to implement `READ`, then you must output the state in the form of a properly formatted JSON, it should not alter the resource it is reading, and you should not output the state in either the create or update scripts (otherwise it will be overridden). See the examples in the test folder for how to do each of these.
105
-
106
-
A complete example that uses all four lifecycle commands is shown below:
107
-
```
108
-
variable "oauth_token" {
109
-
type = string
110
-
}
111
-
112
-
provider "shell" {
113
-
environment = {
114
-
GO_PATH = "/Users/Admin/go"
115
-
}
116
-
sensitive_environment = {
117
-
OAUTH_TOKEN = var.oauth_token
118
-
}
119
-
interpreter = ["/bin/sh", "-c"]
120
-
enable_parallelism = false
121
-
}
122
-
123
-
resource "shell_script" "github_repository" {
124
-
lifecycle_commands {
125
-
//I suggest having these command be as separate files if they are non-trivial
126
-
create = file("${path.module}/scripts/create.sh")
127
-
read = file("${path.module}/scripts/read.sh")
128
-
update = file("${path.module}/scripts/update.sh")
129
-
delete = file("${path.module}/scripts/delete.sh")
130
-
}
131
-
132
-
environment = {
133
-
//changes to one of these will trigger an update
134
-
NAME = "HELLO-WORLD"
135
-
DESCRIPTION = "description"
136
-
}
137
-
138
-
139
-
//sensitive environment variables are exactly the
140
-
//same as environment variables except they don't
141
-
//show up in log files
142
-
sensitive_environment = {
143
-
USERNAME = var.username
144
-
PASSWORD = var.password
145
-
}
146
-
147
-
//this overrides the provider supplied interpreter
148
-
//if you do not specify this then the default for your
149
-
//machine will be used (/bin/sh for linux/mac and cmd for windows)
150
-
interpreter = ["/bin/bash", "-c"]
151
-
152
-
//sets current working directory
153
-
working_directory = path.module
154
-
155
-
//triggers a force new update if value changes, like null_resource
156
-
triggers = {
157
-
when_value_changed = var.some_value
158
-
}
159
-
}
160
-
161
-
output "id" {
162
-
value = shell_script.github_repository.output["id"]
163
-
}
164
-
```
165
-
166
-
Stdout and stderr stream to log files. You can get this by setting:
167
-
168
-
```
169
-
export TF_LOG=1
170
-
```
171
-
**Note:** if you are using sensitive_environment to set sensitive environment variables, these values won't show up in the logs
172
-
173
-
174
9
Requirements
175
10
------------
176
11
@@ -206,7 +41,33 @@ Then commit the changes to `go.mod` and `go.sum`.
206
41
Using the provider
207
42
----------------------
208
43
209
-
Fill this in for each provider
44
+
You can use this provider to make custom external resources and data sources:
0 commit comments