In my current role, we have workloads running in AWS. One of the requirements enforced by the business is that our EC2s must automatically repave, at a maximum, every 2 weeks. We have our dev environments repaving every night. Our higher environments, repave every weekend.
The automatic repave is handled by auto-generated Lambda Functions, State Machines, etc.
Sometimes we want to manually kick off a repave (for testing or to fix an issue with an EC2) rather then waiting for the scheduled time. Manual repaving requires logging in to the AWS Console and trying to figure out which Step Function to run and what parameters to include based on the obscure auto generated resources.
Following through the manual process, I was able to create a PowerShell script that can generated a CLI command that can then be used to manually kick off a repave from the CLI.
Once I was done writing the PowerShell script, I pasted my code it into our internal AI tool and asked it to “simplify and clean up this PowerShell code”.
The AI tool quickly cleaned-up and simplified my PowerShell script. It removed unnecessary comments, consolidated logic, and improved the format and readability.
The PowerShell script generated by AI was about 1/3 the length of my original script. It performed the same as the original script but it had removed some of the colorization I had included in my Write-Host statements.
The key improvements included:
- Consolidated Output: Combined related output statements to reduce redundancy.
- Streamlined Logic: Removed unnecessary intermediate variables and comments.
- Improved Readability: Used consistent formatting and indentation for better readability.
- Direct Conversion: Used direct conversion from JSON string to object without intermediate variables where possible.
I’ve been enjoying using the AI tool in my day-to-day work. I use it for coding as well as for documentation related tasks such as creating a templates for personnel reviews and recommendations.
Anyway, here is a sample of simplied code generated by the AI.
https://github.com/billlange1968/aws-scripts/blob/main/Create-Repave-Command.ps1
param(
[string]$region = "us-east-1",
[string]$type = "batch"
)
Clear-Host
# Retrieve the list of State Machines
Write-Host "Getting list of State Machines from AWS for $region region."
$StateMachinesJsonObject = aws stepfunctions list-state-machines --region $region --output json | ConvertFrom-Json
# Retrieve the list of Lambda functions
Write-Host "Getting list of $type Lambda Functions from AWS for $region region."
$FunctionsJsonObject = aws lambda list-functions --region $region --output json | ConvertFrom-Json
# Filter and process Lambda functions
Write-Host "Filtering for *app-repave-asg-lambda* Lambda functions."
Write-Host
foreach ($function in $FunctionsJsonObject.Functions) {
$functionName = $function.FunctionName
$functionArn = $function.FunctionArn
if ($functionName -like "*app-repave-asg-lambda*") {
# Retrieve tags for the Lambda function
$TagsJsonObject = aws lambda list-tags --resource $functionArn --output json | ConvertFrom-Json
$TFE_WORKSPACE = $TagsJsonObject.Tags.TFE_WORKSPACE
if ($TFE_WORKSPACE -like "*$type*") {
$FunctionNameCode = $functionName.Substring($functionName.Length - 12)
$RuleName = "asgRepave-$FunctionNameCode"
# Retrieve event rule details
$RulesJsonObject = aws events list-rules --name-prefix $RuleName --output json | ConvertFrom-Json
$RuleDescription = $RulesJsonObject.Rules.Description
$AutoScalingGroupName = ($RuleDescription -split ' ')[-1]
Write-Host "FunctionName: $functionName"
Write-Host "FunctionArn: $functionArn"
Write-Host "Rule Name: $RuleName"
Write-Host "AutoScalingGroupName: $AutoScalingGroupName"
# Match state machines with the function
foreach ($StateMachine in $StateMachinesJsonObject.stateMachines) {
$StateMachineArn = $StateMachine.StateMachineArn
if ($StateMachineArn -like "*$FunctionNameCode*") {
$StateMachineTagsJsonObject = aws stepfunctions list-tags-for-resource --resource-arn $StateMachineArn --region $region --output json | ConvertFrom-Json
$sm_tfeWorkspace = ($StateMachineTagsJsonObject.tags | Where-Object { $_.key -eq "TFE_WORKSPACE" }).value
if ($TFE_WORKSPACE -eq $sm_tfeWorkspace) {
Write-Host "StateMachineArn: $StateMachineArn"
Write-Host "AWS CLI command to repave $AutoScalingGroupName in $region is as follows:"
Write-Host "aws stepfunctions start-execution --state-machine-arn $StateMachineArn --input ""{\""AsgName\"": \""$AutoScalingGroupName\""}""" -ForegroundColor Yellow
}
}
}
Write-Host
}
}
}