これらの二日間は、私はラムダとPythonを使用する方法を見ているが、通常より多くのPowerShellを使用してシステムを管理するために使用されています。ラムダの内部でそれをしようとするPowerShellを使用する方法。
まず、ローカルコンピュータにインストールされ、次の3つのモジュールが必要です。
安装PowerShellのコア
https://github.com/powershell/powershell
安装.NETコアソフトウェア開発キット(SDK)
https://www.microsoft.com/net/download
安装AWSLambdaPSCoreモジュール
のインストール・モジュールAWSLambdaPSCore -ScopeあるCurrentUser
内Powershell6コンソールで実行され、インストールされた
新AWSPowerShellLambda -ScriptName awstag -Template基本
彼は自動的に基本的なテンプレート、空白の文書で、PS、およびreadmeファイルに基づいて目次を作成します。我々は追加のモジュールは、ここで変更する必要が追加する必要がある場合は、ブランクのPSファイルは自動的に、powershellcoreモジュールをロードされます。ここに私のテストスクリプトの一つです。主な機能は、スクリプトタグをチェックしてくださいEC2、ボリュームスナップショットを作成することであり、毎月、私はタグによって異なるクリニック法案を表示する必要があるため、対応するタグを持っています。また、60日以上であれば、スナップショット場合は、途中で私は自動的に削除与えました。
# PowerShell script file to be executed as a AWS Lambda function.
#
# When executing in Lambda the following variables will be 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 will be 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.335.0'}
# Uncomment to send the input event to CloudWatch Logs
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
Write-Host "Checking EC2 instance Tags status" -ForegroundColor Yellow
$all=Get-EC2Instance | select -expand instances
$return=$all | Where-Object {$_.tag.key -notcontains "Clinic"}
if($return -ne $null){
$username = "[email protected]"
$password = "Passwordtest" | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
$id=$return.InstanceId
Send-MailMessage -From [email protected] -to [email protected] -SmtpServer smtp.office365.com -Port 587 -UseSsl -Subject "EC2 instance Tag" -body "$id" -Credential $credential
exit
}
# confirm EC2 instances were tagged
$result=@()
foreach($item in $all){
$Name=$item.tag | Where-Object {$_.Key -eq 'Name'} | select -ExpandProperty value
$clinic=$item.tag | Where-Object {$_.Key -eq 'clinic'} | select -ExpandProperty value
$item | add-member -NotePropertyName Description -NotePropertyValue $name
$item | add-member -NotePropertyName Clinic -NotePropertyValue $clinic
$item = $item | select *
$result+=$item
}
$result | select Description, InstanceId, privateIpaddress, Clinic | Group-Object Clinic
write-host "Updating Volume Tags Status ... " -ForegroundColor Yellow
#Tag all volumes based on their attached EC2 Clinic Tag
$allvol=Get-EC2Volume | Where-Object {$_.tag.key -notcontains "Clinic"}
foreach($item in $result){
foreach($item2 in $allvol){
if ($item2.attachments.instanceid -eq $item.InstanceId){
$value=$item.Clinic
New-EC2Tag -Resource $item2.VolumeId -Tag @{Key="Clinic";value=$value}
}
}
}
Write-Host "Updating Snapshot Tags Status..." -ForegroundColor Yellow
#Tag all snapshots based on the volume Tag
$allvol=Get-EC2Volume
$filter= New-Object Amazon.EC2.Model.Filter -Property @{Name = "owner-id"; Values ='386115804199' }
$snapshots=Get-EC2Snapshot -Filter $filter
$snapshots1= $snapshots | ? {$_.Tag.key -notcontains "Clinic"}
foreach($i in $snapshots1){
$volid=$i.VolumeId
foreach($j in $allvol){
if($volid -eq $j.Volumeid){
$value=$j.tag | Where-Object {$_.key -eq 'Clinic'} | select -ExpandProperty value
$name=$j.Tag | Where-Object {$_.key -eq "Name"} | select -ExpandProperty value
$snapid=$i.snapshotid
write-host "--$snapid--"
New-EC2Tag -Resource $snapid -Tag @{Key="Clinic";value=$value}
New-EC2Tag -Resource $snapid -Tag @{Key="Name";value=$name}
}
}
}
write-host "Deleting Snapshots older than over 60 days !" -ForegroundColor Yellow
$date=(get-date).AddDays(-40)
foreach($snapshot in $snapshots){
$id=$snapshot.snapshotid
if($snapshot.starttime -lt $date){
$snapshot
Remove-EC2Snapshot -SnapshotId $id -Confirm:$false
}
}
次のコンソールPowershell6で、彼は自動的にバインドされた役割IAM圧縮関連のモジュールもしてスクリプトを実行した後、ラムダコンソールにアップロードされます。ここではIAM役割私はちょうどアクセスEC2とCloudWatchのログを可能にし、書きます。
公開-AWSPowerShellLambda -ScriptPath。\ awstag.ps1 -name awstag -iamrole 'ec2fullaccess' '領域AP-南東-2
そして、その1分に、あなたがアップロード機能を見ることができるAWSを記録。
直接見ることができ、この1のためのPythonコードとは異なり、あなたがあまりにも多くを言うが、私は直接呼び出しを表示することはできません
テスト試みは、成功を示しています
移動して、対応するCloudWatchのを参照してください
やりました!