Friday, May 23, 2025
HomePowershellWrite nice AWS Lambda PowerShell capabilities

Write nice AWS Lambda PowerShell capabilities


On this submit, I share some ideas I exploit after I write AWS Lambda PowerShell capabilities. Yow will discover plenty of assist writing .Web finish consumer capabilities for Lambda. By finish consumer, I imply issues which can be a part of an software system, like kicking off Glue jobs, working with Redshift and so forth.

However what about admin duties? PowerShell was initially conceived as an administrator’s language so it excels at widespread admin AWS duties, particularly these related to IaaS purposes like creating AMIs (although you may additionally take into account DLM), updating safety teams with newly propagated BGP routes and extra.

What makes PowerShell so enticing for Lambda capabilities is that it has full logic capabilities plus all of the .Web core lessons. And AWS does a exceptional job of protecting the AWS PowerShell cmdlets up-to-date. So, a Lambda perform in PowerShell has all the ability of a scripting language, plus all of .Web and all the present AWS capabilities.

Tip #1

Use Publish-AWSPowerShellLambda simply as soon as. Use it simply as soon as to get the “naked bones” of the perform arrange in AWS. You’ll be able to see an preliminary add within the screenshot under.

Example of Publish-AWSPowerShellLambda
Publish-AWSPowerShellLambda (Click on to enlarge)

After that, a significantly better workflow is to make use of New-AWSPowerShellLambdaPackage. As proven within the screenshot under, it will create a .zip of the perform. Importing the .zip won’t change the perform’s exterior settings in Lambda, which is an efficient factor if you wish to reap the benefits of Suggestions 2 and three.

New-AWSPowerShellLambdaPackage
New-AWSPowerShellLambdaPackage (click on to enlarge)

Tip #2

Use separate SNS subjects for end-user and diagnostic notifications. Admin scripts doing upkeep and making adjustments to the atmosphere nearly all the time have to notify customers of what occurred (“Your AMI is gone!) and in addition present a log of what occurred when the perform ran in case of errors.

SNS Topics for use with Lambda
SNS Subjects to be used with Lambda (click on to enlarge)

Right here you see two separate SNS subjects arrange, with the one for finish customers (“output”) utilizing the e-mail type notification and the one for the admin (“execution”) utilizing JSON type electronic mail. Tip 3 discusses learn how to use the execution SNS subject to good impact; I’ll talk about the end-user SNS subject in one other weblog submit quickly.

Tip #3

Use the “execution” perform vacation spot for fulfillment/failure logging. Certain, you possibly can Write-Host throughout perform execution to log something you prefer to CloudWatch. Perhaps it’s old-school, however I want being notified by electronic mail (the place I spend a whole lot of time anyway) over having to hunt the data from amongst many CloudWatch log teams.

It’s as much as you to resolve if you’d like each success and failure notifications (I all the time do each and filter them into folders in my messaging app). However you’ll come to depend on the SNS notifications as the primary place to go to see “what occurred.”

When you arrange the vacation spot, right here’s the way you may use the messaging functionality. I’m a fan of inspecting $AWSHistory, so this code exhibits a dirt-simple pwsh perform that logs $AWSHistory.LastServiceResponse into an array after which at perform termination, makes use of Publish-SnsMessage to ship it to subject subscribers.

$responses = @() # Outline array to carry $AWSHistory responses 
#area Operate to create an array of output strains to be written to SNS
perform snsWriteLine
{
    param (
        $l
    )
    $obj = New-Object -TypeName psobject 
    $obj = $l
    return $obj
}
#endregion
# Utilizing the perform after an AWS cmdlet name
$amis = Get-EC2Image -Proprietor self | Type-Object -Property CreationDate # Get assortment of AMIs created by this account
$responses += awsResponses $AWSHistory.LastCommand
.....
# On the perform's finish, put the array on the pipeline, which causes AWS to put in writing it to the "vacation spot"
(ConvertTo-Json -InputObject $responses -Compress -Depth 99) # Make SURE that is the final assertion to place something on the pipeline

This code provides the final response object to an array, then at perform finish converts that to JSON and pushes it to the pwsh pipeline. Lambda then sends that to the vacation spot famous in Tip #2 and, voila!, automated notification of what occurred in your perform is in place for each failure and/or success.

One warning: these responses can add up, so use the vacation spot logging judiciously. In case you log greater than 6MB of JSON objects, your Lambda perform will terminate with an HTTP 413 error.

That’s it for this submit…I’ve extra that I’ll get to quickly. Let me know within the feedback what you suppose.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments