Skip to content

Commit fefd455

Browse files
authored
Merge pull request #7 from Guslington/master
eval parameters from stdout for readonly file systems
2 parents e60c9f8 + 0abcda6 commit fefd455

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Searches for SSM Parameters in your AWS account based on the variables provided
1717
* `AWS_REGION` [Required] - Region in which the SSM Parameters are stored
1818
* `DIRECTORY` [Optional] - Directory path of the .env file. Can contain child directories. Default is `/ssm`. *NOTE:* The default cannot be changed if used in a side car configuration.
1919
* `LOG_LEVEL` [Optional] - Levels such as `fatal`, `error`, `warn`, `info`, `debug`, or `disable`. Default is `info`
20+
* `TO_STDOUT` [Optional] - (boolean) prints the parameters to stdout to be evaled. *NOTE:* `LOG_LEVEL` needs to be set to `warn` or above.
2021
* `FORMAT` [Optional] - Format of the .env file.
2122
* unset
2223
```bash
@@ -65,11 +66,25 @@ $ aws ssm put-parameter --name /my-app/production/DB_USERNAME --value "Username"
6566
$ aws ssm put-parameter --name /my-app/production/prod1/DB_PASSWORD --value "SecretPassword" --type SecureString --key-id "alias/aws/ssm" --region ap-southeast-2
6667
```
6768

69+
## Output
70+
71+
awsenv can output the parameters in different ways
72+
73+
* to .env file
74+
* set `FORMAT` to `shell`, `unquoted-shell`
75+
* optionally set the output directory of the .env file with `DIRECTORY`
76+
* eval from the .env file
77+
* leave all optional defaults and eval the outputted `/ssm/.env` file. `eval $(cat /ssm/.env)`
78+
* eval from stdout (for readonly filesystems)
79+
* set `TO_STDOUT` to `true` and eval `awsenv`. `eval $(awsenv)`
80+
* set `LOG_LEVEL` to `warn` or above to stop log outputs from being evaled.
81+
82+
6883
## Usage
6984

70-
There are 2 ways this can be implemented
85+
### Sidecar
7186

72-
1. Include `base2/awsenv` as a side car container
87+
Include `base2/awsenv` as a side car container
7388

7489
* volume mount the `/ssm` directory
7590
* eval the `/ssm/.env` file to export the environment parameters
@@ -88,10 +103,12 @@ test:
88103
entrypoint: eval $(cat /ssm/.env)
89104
```
90105

91-
2. Build `FROM base2/awsenv as awsenv` and extract the binary
106+
### Copy Binary
107+
108+
Build `FROM base2/awsenv as awsenv` and extract the binary
92109

93110
* extract the binary from the `base2/awsenv` image to your `PATH`
94-
* eval the `/ssm/.env` file to export the environment parameters
111+
* run the awsenv binary in your entrypoint script
95112

96113
```Dockerfile
97114
FROM base2/awsenv as awsenv

aws-env.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ func main() {
4444
ExportVariables(path, "", params)
4545
}
4646

47-
ParametersToFile(params)
47+
evalStdout := strings.ToLower(os.Getenv("EVAL_STDOUT"))
48+
49+
if evalStdout == "true" {
50+
ParametersToStdout(params)
51+
} else {
52+
ParametersToFile(params)
53+
}
4854
}
4955

5056
func CreateClient() *ssm.SSM {
@@ -91,6 +97,14 @@ func TrimParameter(path string, parameter *ssm.Parameter) (string, string) {
9197
return env, value
9298
}
9399

100+
func ParametersToStdout(params map[string]string) {
101+
golog.Debug("Printing params to stdout")
102+
for key, value := range params {
103+
key = strings.ToUpper(key)
104+
fmt.Printf("export %s=$'%s'\n", key, value)
105+
}
106+
}
107+
94108
func ParametersToFile(params map[string]string) {
95109
var buffer bytes.Buffer
96110
format := os.Getenv("FORMAT")
@@ -109,6 +123,7 @@ func ParametersToFile(params map[string]string) {
109123
os.MkdirAll(dir, 0755)
110124
}
111125

126+
golog.Debugf("Writing params to %s/.env", dir)
112127
// Write evironment variables to .env file
113128
err := ioutil.WriteFile(dir + "/.env", buffer.Bytes(), 0744)
114129
if err != nil {

0 commit comments

Comments
 (0)