AWS Lambda PowerShell Simplified: The Ultimate Guide 101

on AWS Lambda, Microsoft • May 31st, 2022 • Write for Hevo

AWS Lambda Powershell FI

Windows PowerShell is a popular tool for automating activities in Windows. It is used by businesses and organizations to automate the majority of system management chores. This makes IT workers’ jobs easier and frees them from having to repeat tasks. Writing Windows PowerShell commands and scripts is also simple. When working with Windows PowerShell, you’ll wish to expand its capabilities. The reason for this is that it may lack the functionality to assist you with certain tasks.

To boost productivity and performance, businesses are focusing on cost-effective and auto-scale execution of applications or programs. AWS Lambda is a cost-effective and auto-scaling service that allows users to run code, applications or any backend function without having to manage the servers.

In this article, you will learn how to use AWS Lambda PowerShell and the different tools you can use with it. 

Table Of Contents

What is AWS Lambda?

AWS Lambda Powershell: lambda logo
Image Source

AWS Lambda is a serverless computing solution that was launched in 2014 and allows you to run code for any application or backend service without having to manage servers. AWS Lambda takes care of all the administrative tasks, such as CPU utility, memory, server, resources, and so on. The pricing plan makes you pay for a fixed server even if you are not using it to its full capability when you buy it. However, with AWS Lambda, you have the option of only paying for the computations you need.

AWS Lambda only runs your code when it’s needed. As a result, AWS Lambda can expand from a few requests per day to thousands of requests per second automatically. Triggers in AWS Lambda are used to determine when the code is required. More than 200 AWS services and SaaS apps are compatible with AWS Lambda.

Key Features of AWS Lambda

AWS Lambda Powershell: AWS Lambda Architecture
Image Source

The following are some of AWS Lambda’s key features:

  • Functions Defined as Container Images: With AWS Lambda, users may design, test, and deploy Lambda functions using their chosen container image tooling, procedures, and dependencies.
  • Lambda Extensions: Lambda extensions combine your Lambda functions with your chosen monitoring, security, observability, and governance technologies to improve your Lambda functions.
  • Database Accessibility: The database proxy manages and sends queries across a pool of database connections. As a result, the Lambda function can handle a lot of traffic without depleting the database connections.
  • Integration: The AWS Lambda function works with a variety of AWS services, including DynamoDB, API Gateway, S3, and others, to create functional applications.
  • Code Signing: Code signing provides customers with trust and integrity controls, ensuring that only unmodified code from authorized developers is sent to Lambda services.
  • Reduced Costs: Because customers only pay for the resources they use, the pay-as-you-go model eliminates the costs of idle time or storage.

What is Windows PowerShell?

AWS Lambda Powershell: powershell logo
Image Source

Windows PowerShell is a system management command-line tool and scripting language. Built on the Microsoft.NET framework, Windows PowerShell assists IT professionals in automating and controlling the administration of Windows OS and programs operating in the Windows Server environment.

You may manage your PCs from the command line using Windows PowerShell commands, often known as cmdlets. You may quickly access data stores such as the Registry and Certificate Store using Windows PowerShell. Some internet services offer APIs for accessing their data, whereas others do not.

Key Features of Windows PowerShell

AWS Lambda Powershell: PowerShell Architecture
Image Source

Here are some key features of Windows PowerShell:

  • Exchange Email Management: Exchange Servers may be fully administrated using Windows PowerShell. All aspects of your Exchange email server will be managed automatically by the Exchange Serve module.
  • Pipeline: A pipeline is a feature of Windows PowerShell that allows you to chain actions together. This feature can help users who are completing a task with several steps because it speeds up their execution rate.
  • Extending Administration Services: The Active Directory Module in Windows PowerShell is used to provide management capabilities to Active Directory objects such as computers, users, and groups.

Replicate Data in Minutes Using Hevo’s No-Code Data Pipeline

Hevo Data, a Fully-managed Data Pipeline platform, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 100+ Data Sources straight into your Data Warehouse or any Databases. To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

GET STARTED WITH HEVO FOR FREE

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

AWS Lambda PowerShell Tools

You can write AWS Lambda functions in PowerShell Core 6.0 utilizing the.NET Core 2.1 runtime by using the AWSLambdaPSCore module. Lambda allows PowerShell developers to handle AWS resources and write automation programs within the PowerShell environment. PowerShell support in Lambda allows you to respond to any Lambda event, such as an Amazon S3 event or an Amazon CloudWatch scheduled event, with PowerShell scripts or functions. The AWSLambdaPSCore module is a standalone AWS PowerShell module; it is not included in the AWS Tools for PowerShell, and installing it does not install the AWS Tools for PowerShell.

After installing the AWSLambdaPSCore module, you can author serverless functions using any available PowerShell cmdlets or create your own. Project templates for PowerShell-based serverless applications are included in the AWS Lambda Tools for PowerShell module, as are tools for publishing projects to AWS.

AWS Lambda PowerShell Tools: Prerequisites

Before you can install and utilize the AWSLambdaPSCore module, you must first complete the procedures below. 

  • Install the Correct PowerShell Version: Lambda’s PowerShell support is based on the PowerShell Core 6.0 release, which is cross-platform. PowerShell Lambda functions can be written on Windows, Linux, or Mac. If you don’t already have this version of PowerShell installed, go to the Microsoft PowerShell documentation website for help.
  • Install the .NET Core 2.1 SDK: Because PowerShell Core is based on.NET Core, both.NET Core and PowerShell Lambda functions use the same.NET Core 2.1 Lambda runtime. The.NET Core 2.1 SDK is used to construct the Lambda deployment package by the Lambda PowerShell publishing cmdlets. The Microsoft Download Center has the.NET Core 2.1 SDK available. Make sure you’re installing the SDK rather than the Runtime.

AWS Lambda PowerShell Tools: Install AWSLambdaPSCore

You are now ready to install the AWSLambdaPSCore module after fulfilling the prerequisites. In a PowerShell Core session, run the following command.

PS> Install-Module AWSLambdaPSCore -Scope CurrentUser

You’re now ready to use PowerShell to create Lambda functions.

Building Functions in AWS Lambda PowerShell 

When writing AWS Lambda PowerShell function code, the following sections describe how common programming patterns and key principles apply.

.NET runtimes
NameIdentifierOperating systemArchitectures
.NET 6dotnet6Amazon Linux 2x86_64, arm64
.NET Core 3.1dotnetcore3.1Amazon Linux 2x86_64, arm64

For the PowerShell runtime, Lambda provides the following sample applications:

  • Blank-PowerShell: A PowerShell function that demonstrates the use of logging, environment variables, and the AWS SDK.

Building AWS Lambda PowerShell: Set-Up

For the PowerShell runtime, Lambda provides a suite of tools and libraries.

The following cmdlets are included in the AWSLambdaPSCore module to assist in authoring and publishing PowerShell Lambda functions:

  • Get-AWSPowerShellLambdaTemplate: Returns a list of getting started templates using Get-AWSPowerShellLambdaTemplate.
  • New-AWSPowerShellLambda: Creates an initial PowerShell script based on a template using New-AWSPowerShellLambda.
  • Publish-AWSPowerShellLambda: This command publishes a PowerShell script to AWS Lambda.
  • New-AWSPowerShellLambdaPackage: Creates a Lambda deployment package for use in a continuous integration and delivery system.

Building AWS Lambda PowerShell: Deploy Functions

Your PowerShell script, PowerShell modules that your PowerShell script requires, and the assemblies required to host PowerShell Core are all included in a deployment package for the PowerShell runtime.

Creating Lambda Function in AWS Lambda PowerShell

Follow these steps to develop a new PowerShell script, upload it, and test it in AWS Lambda PowerShell:

  • Step 1: Run the following command to see a list of possible templates:
PS C:> Get-AWSPowerShellLambdaTemplate

Template               Description
--------               -----------
Basic                  Barebones script
CodeCommitTrigger      Script to process AWS CodeCommit Triggers
...
  • Step 2: Run the following command to build a sample script based on the Basic template:
New-AWSPowerShellLambda -ScriptName MyFirstPSScript -Template Basic

In the current directory, a new subdirectory entitled MyFirstPSScript.ps1 is generated. The -ScriptName argument determines the directory’s name. The -Directory argument can be used to specify a different directory.

As you can see, the new file has the following information:

# PowerShell script file to run as a Lambda function
# 
# When executing in Lambda the following variables are predefined.
#   $LambdaInput - A PSObject that contains the Lambda function input data.
#   $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
#
# The last item in the PowerShell pipeline is returned as the result of the Lambda function.
#
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement 
# indicating the module and version.
                
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.618.0'}

# Uncomment to send the input to CloudWatch Logs
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
  • Step 3: Uncomment the Write-Host line of the sample script to see how log messages from your PowerShell script are forwarded to Amazon CloudWatch Logs.

Add a new line at the end of the script with $PSVersionTable to show how you can return data from your Lambda functions. The $PSVersionTable is now part of the PowerShell pipeline. The return data for the Lambda function is the last object in the PowerShell pipeline once the PowerShell script is finished. $PSVersionTable is a global variable in PowerShell that also contains information about the current environment.

After making these changes, the sample script’s last two lines should look like this:

Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
$PSVersionTable
  • Step 4: Change the directory to the script’s location after modifying the MyFirstPSScript.ps1 file. Then, to publish the script to Lambda, run the following command:
Publish-AWSPowerShellLambda -ScriptPath .MyFirstPSScript.ps1 -Name  MyFirstPSScript -Region us-east-2
  • Step 5: Use the AWS Command Line Interface (AWS CLI) invoke command to call your function.
> aws lambda invoke --function-name MyFirstPSScript out

Building AWS Lambda PowerShell: Function Handler

The Lambda handler runs the PowerShell script when a Lambda function is called.

The following variables are predefined when the PowerShell script is run:

  • $LambdaInput: A PSObject containing the handler’s input. This input can be event data (released by an event source) or custom input (such as a string or other custom data object) that you give.
  • $LambdaContext: An Amazon.Lambda.Core.ILambdaContext object that can be used to get information about the current invocation, such as the current function’s name, memory limit, remaining execution time, and logging.

What Makes Hevo’s ETL Process Best-In-Class

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

Building AWS Lambda PowerShell: Context Object

When Lambda runs your function, it transmits context information to the handler via the $LambdaContext variable. This variable gives information about the invocation, function, and execution environment to methods and properties.

Properties of the context are as follows:

  • FunctionName: The Lambda function’s name.
  • FunctionVersion: The function’s version.
  • InvokedFunctionArn: The Amazon Resource Name (ARN) that is utilized to invoke the function is InvokedFunctionArn. If the invoker provides a version number or an alias, this value is true.
  • MemoryLimitInMB: The size of the function’s memory allocation.
  • AwsRequestId: The invocation request’s identifier.
  • LogGroupName: The function’s log group.
  • LogStreamName: The log stream for the function instance is named LogStreamName.
  • RemainingTime: The amount of time left before the execution times out in milliseconds.
  • Identity:  Information about the Amazon Cognito account that made the request possible.
  • ClientContext: It is a class that represents the context of a client (mobile apps). The client application provides Lambda with the client context.
  • Logger: The function’s logger object.

A simple handler function that prints some context information in AWS Lambda PowerShell is shown in the PowerShell code snippet below.

#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.618.0'}
Write-Host 'Function name:' $LambdaContext.FunctionName
Write-Host 'Remaining milliseconds:' $LambdaContext.RemainingTime.TotalMilliseconds
Write-Host 'Log group name:' $LambdaContext.LogGroupName
Write-Host 'Log stream name:' $LambdaContext.LogStreamName

Building AWS Lambda PowerShell: Function Logging

AWS Lambda automatically monitors and reports function metrics to Amazon CloudWatch on your behalf. For each instance of your Lambda function, a CloudWatch Logs log group and a log stream are included. The Lambda runtime environment logs and transmits logs and other output from your function’s code, sending details about each invocation to the log stream.

Creating a Function that Returns Logs in AWS Lambda PowerShell

You can use cmdlets on Microsoft.PowerShell.Utility, to generate logs from your function code.

Any logging module that writes to stdout or stderr, such as utility. Write-Host is used in the following example.

Example for function/Handler.ps1: Logging

#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.618.0'}
Write-Host `## Environment variables
Write-Host AWS_LAMBDA_FUNCTION_VERSION=$Env:AWS_LAMBDA_FUNCTION_VERSION
Write-Host AWS_LAMBDA_LOG_GROUP_NAME=$Env:AWS_LAMBDA_LOG_GROUP_NAME
Write-Host AWS_LAMBDA_LOG_STREAM_NAME=$Env:AWS_LAMBDA_LOG_STREAM_NAME
Write-Host AWS_EXECUTION_ENV=$Env:AWS_EXECUTION_ENV
Write-Host AWS_LAMBDA_FUNCTION_NAME=$Env:AWS_LAMBDA_FUNCTION_NAME
Write-Host PATH=$Env:PATH
Write-Host `## Event
Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 3)

Example for Log Format:

START RequestId: 56639408-xmpl-435f-9041-ac47ae25ceed Version: $LATEST
Importing module ./Modules/AWSPowerShell.NetCore/3.3.618.0/AWSPowerShell.NetCore.psd1
[Information] - ## Environment variables
[Information] - AWS_LAMBDA_FUNCTION_VERSION=$LATEST
[Information] - AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/blank-PowerShell-function-18CIXMPLHFAJJ
[Information] - AWS_LAMBDA_LOG_STREAM_NAME=2020/04/01/[$LATEST]53c5xmpl52d64ed3a744724d9c201089
[Information] - AWS_EXECUTION_ENV=AWS_Lambda_dotnet6_PowerShell_1.0.0
[Information] - AWS_LAMBDA_FUNCTION_NAME=blank-PowerShell-function-18CIXMPLHFAJJ
[Information] - PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin
[Information] - ## Event
[Information] - 
{
    "Records": [
        {
            "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
            "receiptHandle": "MessageReceiptHandle",
            "body": "Hello from SQS!",
            "attributes": {
                "ApproximateReceiveCount": "1",
                "SentTimestamp": "1523232000000",
                "SenderId": "123456789012",
                "ApproximateFirstReceiveTimestamp": "1523232000001"
            },
            ...
END RequestId: 56639408-xmpl-435f-9041-ac47ae25ceed
REPORT RequestId: 56639408-xmpl-435f-9041-ac47ae25ceed	Duration: 3906.38 ms	Billed Duration: 4000 ms	Memory Size: 512 MB	Max Memory Used: 367 MB	Init Duration: 5960.19 ms	
XRAY TraceId: 1-5e843da6-733cxmple7d0c3c020510040	SegmentId: 3913xmpl20999446	Sampled: true

For each call, the.NET runtime logs the START, END, and REPORT lines. The following information is provided in the report line.

Report Log:

  • RequestId: The invocation’s unique request ID.
  • Duration: The time it took your function’s handler method to process the event in seconds.
  • Billed Duration: The amount of time the invocation is billed for.
  • Memory Size: The amount of memory that the function is given.
  • Maximum Memory Used: The maximum amount of memory that the function can utilize.
  • Init Duration: The time it took the runtime to load the function and run code outside of the handler method for the first request delivered.
  • XRAY TraceId: The AWS X-Ray trace ID for tracked requests.
  • SegmentId: The X-Ray segment ID for traced requests.
  • Sampled: The sampling result for tracked requests.

Building AWS Lambda PowerShell: Function Errors

Lambda generates a JSON representation of an error when your code raises one. This error document shows in the invocation log as well as the output for synchronous invocations.

When you call a Lambda function in AWS Lambda PowerShell, it checks the parameter values and validates the rights in your execution role. It also ensures that the event document is a valid JSON document.

Lambda forwards the request to a function instance if it passes validation. The event document is converted into an object and passed to your function handler by the Lambda runtime environment in AWS Lambda PowerShell.

If Lambda detects an error, it returns the error’s exception type, message, and HTTP status code. The client or service that is called the Lambda function can either deal with the error programmatically or pass it on to a user. The appropriate error handling behavior is determined by the type of application, audience, and error source.

Conclusion

In this article, you learned how to use AWS Lambda PowerShell and its features. You also learned about the different processes while building an AWS Lambda PowerShell and how to use the tools.

However, as a Developer, extracting complex data from a diverse set of data sources like Databases, CRMs, Project management Tools, Streaming Services, and Marketing Platforms to your Database can seem to be quite challenging. If you are from non-technical background or are new in the game of data warehouse and analytics, Hevo Data can help!

Visit our Website to Explore Hevo

Hevo Data will automate your data transfer process, hence allowing you to focus on other aspects of your business like Analytics, Customer Management, etc. This platform allows you to transfer data from 100+ multiple sources to Cloud-based Data Warehouses like Snowflake, Google BigQuery, Amazon Redshift, etc. It will provide you with a hassle-free experience and make your work life much easier.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand.

You can also have a look at our unbeatable pricing that will help you choose the right plan for your business needs!

No-Code Data Pipeline for Your Data Warehouse