How To Create An AWS Lambda PowerShell Function the Easy Way — Using Docker
Background
I’ve been working with PowerShell since its beginning and haven’t looked back since. Last year, while working on an serverless project for work, I decided to try Lambda on my own and deploy some PowerShell code to AWS to see how it works.
Fast forward to now…I started working more with AWS on my own while studying for some certs. I jumped back into Lambda and was reminded of how tedious it was to setup my environment for publishing my PowerShell code to AWS. Unfortunately, I didn’t take any notes last time and wanted to ensure I next time around I could just focus on the code. That is where Docker comes in to save the day and our story begins — I created a Docker container with all the dependencies required to publish your PowerShell code to AWS in as little as 5 minutes with a few scripts included to help streamline the process.
So, jump on in and give it a try.
See It In Action
Assumptions
- You have an AWS Account and access to you “Access Key Id” and “Secret Access key”
- You have access to Docker or Docker Desktop
- You have some Docker know how
If you are lacking in either of these areas head on over to the Docker website and learn how to Get Started!
So, How Easy Is It To Publish My Code?
Really easy, check out the steps below…
Start the container
docker run -dit — name powershell-builder rickjacobo/lambda-powershell-project-builder
Access the container shell
docker exec -it powershell-builder pwsh
Create a project
./create-project.ps1
The create-project script will ask a few questions during setup to assist with
- Setting up the PowerShell Lambda Project
- Creating a script to edit your PowerShell code
- Creating a script to publish your PowerShell
- Adding your AWS credentials via the ‘aws configure’ cli
Add code to your project
./edit-<projectname>.ps1
This is the same as running
vi /<projectname>/<projectname>.ps1
If you don’t have any code readily available feel free to append the code below to <projectname>.ps1.
$Json = @"
{
"Message":"Hello, World!"
}
"@
$Json
Publish your code to AWS
./publish-<projectname>.ps1
That’s it — the container abstracts a lot of the complexity of getting started. Hopefully, you’ll give it a try.
Project Notes
- The lambda-powershell-project-builder container has all the dependencies required to get started quickly with PowerShell on AWS Lambda
- The create-project.ps1 script runs the Publish-AWSPowerShellLambda command with the values collected during the script execution and creates the edit and publish scripts on the fly based on your answers. Finally, it launches the ‘aws configure’ cli command for so you can enter your aws “Access Key Id” and “Secret Access key” which is required for publishing your Lambda application.
- The publish-<project>.ps1 script run the Publish-AWSPowerShellLambda script with some sensible defaults