AWS Lambda with PowerShell

Amazon Web Services (AWS) released some exciting news back in September, announcing Lambda support for PowerShell Core. The feature now enables us to execute PowerShell scripts and functions to respond to events in AWS.

Before we start writing our scripts and functions, we need to know what prerequisites we need to create a PowerShell AWS Lambda environment. A few steps need to occur to achieve this:

  1. Install PowerShell Core
  2. Install the .NET Core 2.1 Software Development Kit (SDK)
  3. Install the AWSLambdaPSCore module
    Install-Module AWSLambdaPSCore -Scope CurrentUser

When installing the .NET Core 2.1 SDK, make sure you install the SDK and not the runtime. Once you have installed these components, you're ready to start!

AWSLambdaPSCore ^

AWS provides a Lambda PowerShell Core module to create and publish your scripts and functions to AWS Lambda. The module contains four cmdlets to work with:

  • Get-AWSPowerShellLambdaTemplate: Returns a list of templates you can use
  • New-AWSPowerShellLambda: Creates the script from a template listed in Get-AWSPowerShellLambdaTemplate
  • Publish-AWSPowerShellLambda: Publishes the script/function to AWS Lambda
  • New-AWSPowerShellLambdaPackage: Creates the Lambda package deployable in a continuous integration/continuous deployment (CI/CD) system.

Two PowerShell automatic variables are also available in Lambda. The $LambdaContext variable provides information about the interaction during runtime. Amazon documents the information available to access it here.

It presents a Lambda input object in the form of a second automatic variable called

$LambdaInput. $LambdaInput, which is a PSObject type, provides context on the reasons for calling the functions.

Creating an AWS Lambda package ^

I've explained a few of the basics of using PowerShell AWS Lambda. Now let's create a package to publish to Lambda. The template I will be using for this script is "basic." I will create a script that will take advantage of AWS Elastic Compute Cloud (EC2) instance tags. To begin, we will create our script template:

Generating a new PowerShell script template

Generating a new PowerShell script template

This generates two files in the new folder: the script file PSShutdown.ps1 and It will populate PSShutdown.ps1 with the following content:

PSShutdown script default content from the template

PSShutdown script default content from the template

Note the #Requires line:

If you want to add any additional modules to Lambda, you will need to add the name and version of your module in addition to the AWSPowerShell module.

To complete the PSShutdown.ps1 script, I will add the code below:

The code looks for an EC2 instance with the power_off_by_script tag set to true and the state code of 16, which indicates a powered-on instance. It passes each instance found through a foreach loop getting the instance ID, which Stop-EC2Instance uses to identify an instance.

Publish AWS Lambda package ^

Now that the script is complete, we need to publish it to AWS Lambda. To do this, we'll use another AWSLambdaPSCore module cmdlet, Publish-AWSPowerShellLambda. You will need to add an Identity and Access Management (IAM) role to the cmdlet parameter, or the cmdlet will prompt for the role before continuing.

The Publish-AWSPowerShellLambda cmdlet will package up your function/script with the module. Under the Lambda section, the AWS console will display your function/script:

Function displayed in the AWS console

Function displayed in the AWS console

To test the new Lambda function, you can invoke it through the console or use the dotnet command, which is part of the .NET Core 2.1 SDK. In this example, I'm using the dotnet command:

The dotnet command testing your Lambda function

The dotnet command testing your Lambda function

Summary ^

Serverless computing allows you to build applications, services, and such without the need of a server. The added advantage to serverless applications is you don't need to provision, scale, and manage any servers. We now have the ability to create serverless functions in Lambda with PowerShell. This is exciting, since along with Python, Go, Java, Node.js, and C#, PowerShell is a programming language that can create Lambdas in AWS.

This is the complete code used in this article:

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account