-
Notifications
You must be signed in to change notification settings - Fork 17
/
iam_assume_role.ps1
56 lines (48 loc) · 1.81 KB
/
iam_assume_role.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
A wrapper script for Assuming different AWS roles, useful for roles and specifically cross account roles.
.DESCRIPTION
Wraps up support for AWS role support
.PARAMETER Path
The path to the .
.EXAMPLE
C:\PS>.\code\bumsonseats\iam_assume_role.ps1 -AccountNo 12121212121 -Role MarcusDogsbody
.NOTES
Author: James Woolfenden
Date: January 10, 2019
#>
Param(
[Parameter(Mandatory=$true)]
[string]$AccountNo,
[Parameter(Mandatory=$true)]
[string]$Role,
[string]$SESSION_NAME = "PACKER"
)
function iam_assume_role
{
<#
.Description
iam_assume_role allows you to run as a different role in a different account
.Example
iam_assume_role -AccountNo $AccountNo -Role SuperAdmin
#>
Param(
[Parameter(Mandatory=$true)]
[string]$AccountNo,
[Parameter(Mandatory=$true)]
[string]$Role
)
Write-Output "AccountNo: $AccountNo"
Write-Output "Role : $Role"
$ARN="arn:aws:iam::$($AccountNo):role/$Role"
Write-Output "ARN : $ARN"
Write-Output "aws sts assume-role --role-arn $ARN --role-session-name $SESSION_NAME --duration-seconds 3600"
$Creds=aws sts assume-role --role-arn $ARN --role-session-name $SESSION_NAME --duration-seconds 3600 |convertfrom-json
[Environment]::SetEnvironmentVariable("AWS_DEFAULT_REGION","eu-west-2")
[Environment]::SetEnvironmentVariable("AWS_ACCESS_KEY_ID",$creds.Credentials.AccessKeyId)
[Environment]::SetEnvironmentVariable("AWS_ACCESS_KEY",$creds.Credentials.AccessKeyId)
[Environment]::SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY",$creds.Credentials.SecretAccessKey)
[Environment]::SetEnvironmentVariable("AWS_SECRET_KEY", $creds.Credentials.SecretAccessKey)
[Environment]::SetEnvironmentVariable("AWS_SESSION_TOKEN",$creds.Credentials.SessionToken)
}
iam_assume_role -AccountNo $AccountNo -Role $Role