diff --git a/README.md b/README.md index c4ef73b..9008deb 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,10 @@ $ wget https://github.com/Droplr/aws-env/raw/master/bin/aws-env-linux-amd64 -O a ``` 3. Start your application with aws-env - * `AWS_ENV_PATH` - path of parameters. If it won't be provided, aws-env will exit immediately. That way, you can run your Dockerfiles locally. + * `AWS_ENV_PATH` - path of parameters. If it isn't be provided, aws-env will not make requests to SSM. That way, you can run your Dockerfiles locally. * `AWS_REGION` and AWS Credentials - [configuring credentials](https://github.com/aws/aws-sdk-go#configuring-credentials) ``` -$ eval $(AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env) && node -e "console.log(process.env)" +$ AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env node -e "console.log(process.env)" ``` @@ -45,7 +45,7 @@ RUN apk update && apk upgrade && \ RUN wget https://github.com/Droplr/aws-env/raw/master/bin/aws-env-linux-amd64 -O /bin/aws-env && \ chmod +x /bin/aws-env -CMD eval $(aws-env) && node -e "console.log(process.env)" +CMD aws-env node -e "console.log(process.env)" ``` ``` @@ -69,13 +69,6 @@ $ docker run -t my-app $ wget https://github.com/Droplr/aws-env/raw/befe6fa44ea508508e0bcd2c3f4ac9fc7963d542/bin/aws-env-linux-amd64 ``` -* Many Docker images (e.g. ruby) are using /bin/sh as a default shell. It crashes `$'string'` - notation that enables multi-line variables export. For this reason, to use aws-env, it's - required to switch shell to /bin/bash: -``` -CMD ["/bin/bash", "-c", "eval $(aws-env) && rails s Puma"] -``` - * You should never pass AWS credentials inside the containers, instead use IAM Roles for that - [Managing Secrets for Amazon ECS Applications Using Parameter Store and IAM Roles for Tasks]( https://aws.amazon.com/blogs/compute/managing-secrets-for-amazon-ecs-applications-using-parameter-store-and-iam-roles-for-tasks/) diff --git a/aws-env.go b/aws-env.go index 8f46356..70e8025 100644 --- a/aws-env.go +++ b/aws-env.go @@ -1,22 +1,35 @@ package main import ( - "fmt" + "log" + "os" + "os/exec" + "path" + "syscall" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ssm" - "log" - "os" - "strings" ) func main() { if os.Getenv("AWS_ENV_PATH") == "" { log.Println("aws-env running locally, without AWS_ENV_PATH") - return + } else { + ExportVariables(os.Getenv("AWS_ENV_PATH"), "") } - ExportVariables(os.Getenv("AWS_ENV_PATH"), "") + binary, lookErr := exec.LookPath(os.Args[1]) + if lookErr != nil { + panic(lookErr) + } + + env := os.Environ() + args := os.Args[1:] + execErr := syscall.Exec(binary, args, env) + if execErr != nil { + panic(execErr) + } } func CreateClient() *ssm.SSM { @@ -43,7 +56,7 @@ func ExportVariables(path string, nextToken string) { } for _, element := range output.Parameters { - PrintExportParameter(path, element) + SetExportParameter(element) } if output.NextToken != nil { @@ -51,12 +64,10 @@ func ExportVariables(path string, nextToken string) { } } -func PrintExportParameter(path string, parameter *ssm.Parameter) { +func SetExportParameter(parameter *ssm.Parameter) { name := *parameter.Name value := *parameter.Value - env := strings.Trim(name[len(path):], "/") - value = strings.Replace(value, "\n", "\\n", -1) - - fmt.Printf("export %s=$'%s'\n", env, value) + _, envName := path.Split(name) + os.Setenv(envName, value) } diff --git a/bin/aws-env-darwin-386 b/bin/aws-env-darwin-386 index 855ca69..fec65de 100755 Binary files a/bin/aws-env-darwin-386 and b/bin/aws-env-darwin-386 differ diff --git a/bin/aws-env-darwin-amd64 b/bin/aws-env-darwin-amd64 index 0b7a795..8080b29 100755 Binary files a/bin/aws-env-darwin-amd64 and b/bin/aws-env-darwin-amd64 differ diff --git a/bin/aws-env-linux-386 b/bin/aws-env-linux-386 index 1b17514..500ed84 100755 Binary files a/bin/aws-env-linux-386 and b/bin/aws-env-linux-386 differ diff --git a/bin/aws-env-linux-amd64 b/bin/aws-env-linux-amd64 index bbcc1f1..6da04e8 100755 Binary files a/bin/aws-env-linux-amd64 and b/bin/aws-env-linux-amd64 differ diff --git a/bin/aws-env-windows-386 b/bin/aws-env-windows-386 index 22111b6..b55bd4d 100755 Binary files a/bin/aws-env-windows-386 and b/bin/aws-env-windows-386 differ diff --git a/bin/aws-env-windows-amd64 b/bin/aws-env-windows-amd64 index 64b1a18..3e1750c 100755 Binary files a/bin/aws-env-windows-amd64 and b/bin/aws-env-windows-amd64 differ