-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update-AWSAccess.ps1
39 lines (37 loc) · 1.35 KB
/
Update-AWSAccess.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<#
.Synopsis
Automatically updates the specified IP of the security group in AWS
#>
$AccessKey = ""
$SecretKey = ""
$IPLookupURL = "ipv4bot.whatismyipaddress.com"
$NewIP = (Invoke-RestMethod -Uri $IPLookupURL -Method GET) + "/32"
$Region = ""
$SecGroupID = ""
Try {
$SG = Get-EC2SecurityGroup -GroupID $SecGroupID -Region $Region -AccessKey $AccessKey -SecretKey $SecretKey
}
Catch {
Write-Error "`nAn Error Occurred while retrieving the default information: $_"
Exit
}
$OldPermissions = $SG.IpPermissions
$OldIP = $OldPermissions.Ipv4Ranges.CidrIp
$Permissions = $SG.IpPermissions
$NewPermissions = New-Object Amazon.EC2.Model.IpPermission
$NewPermissions.IpProtocol = $Permissions.IpProtocol
$NewPermissions.FromPort = $Permissions.FromPort
$NewPermissions.ToPort = $Permissions.ToPort
$NewPermissions.IpRanges = $NewIP
If ($NewIP -ne $OldIP) {
Write-Host "Old IP: $OldIP"
Write-Host "New IP: $NewIP"
Write-Host "IP change detected. Updating Security Group"
Revoke-EC2SecurityGroupIngress -GroupId $SecGroupID -IpPermissions $OldPermissions -Region $Region -AccessKey $AccessKey -SecretKey $SecretKey
Grant-EC2SecurityGroupIngress -GroupId $SecGroupID -IpPermissions $NewPermissions -Region $Region -AccessKey $AccessKey -SecretKey $SecretKey
}
Else {
Write-Host "Old IP: $OldIP"
Write-Host "Current IP: $NewIP"
Write-Host "IP matches Security Group. Skipping."
}