nu-modules/aws.nu

67 lines
2.1 KiB
Plaintext

def call_aws [args:list<string>] {
run-external --redirect-stdout "aws" $args
}
def comp-instanceId [] {
instances | each { |inst|
{
value: $inst.InstanceId,
description: $inst.InstanceType
}
}
}
export def login-into-docker [] {
^aws ecr get-login-password --region eu-central-1 | ^docker login --username AWS --password-stdin $env.AWS_INFASTRUCTURE_ACCOUNT
}
export def login [] {
call_aws ["sso", "login"]
login-into-docker
}
export def "restart instance" [instanceId:string@comp-instanceId] {
call_aws ["ec2", "reboot-instances", "--instance-ids", $instanceId]
}
export def "stop instance" [instanceId:string@comp-instanceId] {
call_aws ["ec2", "stop-instances", "--instance-ids", $instanceId]
}
export def "start instance" [instanceId:string@comp-instanceId] {
call_aws ["ec2", "start-instances", "--instance-ids", $instanceId]
}
export def "list alarm" [] {
call_aws [cloudwatch describe-alarms] | from json | get MetricAlarms | select -i AlarmName AlarmDescription | rename name description
}
def comp-alarm-name [] {
list alarm | get name
}
export def alarm [alarm:string@comp-alarm-name] {
call_aws [cloudwatch describe-alarm-history --alarm-name $alarm] | from json
| get AlarmHistoryItems
| where HistoryItemType == StateUpdate
| update HistoryData {|r| $r.HistoryData | from json}
| flatten
| update oldState {|r| $r.oldState | get stateValue}
| update newState {|r| $r.newState | get stateValue}
| update Timestamp { |r| $r.Timestamp | into datetime }
| select AlarmName Timestamp HistorySummary oldState newState
| rename name age summary old new
| insert date {|r| $r.age | date to-timezone "Europe/Berlin" | date format }
| where new == ALARM
| reverse
}
# list all ec2 instances
export def instances [] {
call_aws ["ec2", "describe-instances"] | from json
| get Reservations | get Instances | flatten
| select -i ImageId InstanceId InstanceType Architecture State KeyName PrivateIpAddress PublicIpAddress PrivateDnsName PublicDnsName Tags
| update State { |row| $row.State.Name }
}