Skip to content

Instantly share code, notes, and snippets.

@naamancampbell
Last active January 2, 2018 08:31
Show Gist options
  • Save naamancampbell/23368a38342995a08d9e7e455632228d to your computer and use it in GitHub Desktop.
Save naamancampbell/23368a38342995a08d9e7e455632228d to your computer and use it in GitHub Desktop.
Reads PowerShell environment variables file to upload to AWS Lambda
<#
.SYNOPSIS
Reads PowerShell environment variables file to upload to AWS Lambda
.DESCRIPTION
The .env_lambda script is made of the following steps:
1. Skips the first three lines of the .env file
2. Retrieves the contents of each line after the first ":"
3. Joins all variables into a comma-separated string
4. Uploads variables up to AWS Lambda
.PARAMETER EnvVars
File containing PowerShell environment variable to upload to AWS Lambda.
See example for file format.
.PARAMETER SkipLines
Number of lines to skip from the start of the file.
Defaults to 0.
.PARAMETER LambdaFunction
Name of AWS Lambda Function to upload environment variables to.
.EXAMPLE
Example source .env.ps1 file:
-----------------------------
$env:PROJECT_HOME="C:\Users\naaman\Documents\Code\pythagoras"
cd $env:PROJECT_HOME
.\venv\Scripts\activate.ps1
$env:DEV_S3_BUCKET_NAME="pythagoras-dev-static.example.id.au"
$env:DEV_STATIC_PATH="https://pythagoras-dev-static.example.id.au/static"
-----------------------------
PS C:\Users\naaman> .\.env_lambda.ps1 -EnvVars .env.ps1 -SkipLines 3 -LambdaFunction pythagoras-app-dev
...
aws lambda update-function-configuration --function-name=pythagoras-app-dev --environment Variables='{DEV_S3_BUCKET_NAME="pythagoras-dev-static.example.id.au", DEV_STATIC_PATH="https://pythagoras-dev-static.example.id.au/static"}'
#>
[cmdletbinding()]
Param (
# Read from supplied environment variables filename
[Parameter(Mandatory = $true)]
[String] $EnvVars,
# Skip number of lines from top of environment variables file
[Parameter(Mandatory = $false)]
[String] $SkipLines = 0,
# Skip number of lines from top of environment variables file
[Parameter(Mandatory = $true)]
[String] $LambdaFunction
)
$lines = Get-Content $EnvVars | Select-Object -skip $SkipLines | %{$_.Split(':', 2)[1]}
$variables = @{}
foreach($line in $lines) {
$key_val = $line.Split('=')
$variables.Add($key_val[0],$key_val[1].trim("`""))
}
Update-LMFunctionConfiguration -FunctionName $LambdaFunction -Environment_Variable $variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment