AD Authentication for AWS console

When you get started with Amazon Web Services (AWS) one thing to do early is secure access to the web console.  Rather than manage another set of user accounts you can reuse your corporate directory (Microsoft Active Directory) to login to the AWS console.  You use AD Federation Services to do this.  Also, if you keep your ADFS server internal, then your AWS console is not accessible from the public Internet.

Start with this presentation:

Follow up with this one specifically about federation services

The original post on this is
It’s getting a little dated but gives you a walk though of the general process for setting this up.
The steps in the above guide can be automated with PowerShell. This guide will get you 90% of the way there:
Extra steps required for production deployment:

You can even brand the ADFS logon page.

Update: See next post for the other half of this puzzle, getting AWS API keys via AD authentication.

Update: Switched from using mail attribute to userPrincipleName attribute so this works for test or service accounts that don’t have mailboxes.

# Remixed from

# Modules: Requires AWS Powershell tools
Import-Module ‘C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1’

# Environment specific Variables
$hostname = $env:computername
$hostname = $hostname.ToLower()
$CurrentFolder = split-path -parent $MyInvocation.MyCommand.Definition

#location where makecert tool is present, you can find this tool part of Windows SDK
$makecertpath = (“$CurrentFolder\makecert.exe”)

$domainname = “”
$netbiosname = “companyname”
$adfsname = “adfs.$domainname”
$adfsFarmName=”Companyname ADFS”
# If you have a corp account check what your signin URL is

# Decrypt service account password (can be a lowly domain user. Like most service accounts, does not require interactive logon rights)
$key=(1..16) # lame 128bit key
$Encryptedpassword = Get-Content “$CurrentFolder\encrypted.txt”
$secure_string = $Encryptedpassword | ConvertTo-SecureString -key $key

Write-host “Running prerequsite tests”
# Use a cert from your public wildcard cert or your internal CA
# makecert.exe comes from Windows SDK if you must use a self-signed cert (lab only)
if ( !(Test-Path -Path $makecertpath))
‘makecertpath should point to full path of makecert.exe’

if ((Get-Module AWSPowerShell) -eq $null)
‘AWSPowerShell is not installed’

# I had to change this from Siva’s code
if ((Get-AWSCredentials -ListStoredCredentials | Select-String ‘default’) -eq $null)
‘AWS PS Default is not set. Run Initialize-AWSDefaults’

if ((Get-ChildItem ‘Cert:\LocalMachine\My’) -eq $null)
‘No certificate in local machine personal store’

# Requires AD powershell module to be installed
Write-host “Creating AD groups and adding test user to them”
#New-ADGroup AWS-Administrators -GroupScope Global -GroupCategory Security
#New-ADGroup AWS-Infrastructure -GroupScope Global -GroupCategory Security
#New-ADGroup AWS-Operations -GroupScope Global -GroupCategory Security
#Write-host “Creating service account for ADFS service to run”
#New-ADUser -Name $adfsServiceAccountName -AccountPassword $password -Enabled $true

Write-host “Installing ADFS (Active Directory Federation Service)”
Install-WindowsFeature Web-Server, ADFS-Federation, Web-Scripting-Tools -IncludeManagementTools
Import-Module WebAdministration

Write-host “Configuring service principle name: $adfsServiceAccountName”
setspn.exe -s http/$adfsname $adfsServiceAccountName
setspn.exe -s http/$hostname.$domainname $adfsServiceAccountName

# This step creates a self signed cert which is fine for testing but for prod we’ll use our public trusted wildcard cert
#Write-host “Generating self signed certificate in local machine store”
#&$makecertpath -n “CN=$adfsname” -r -pe -sky exchange -ss My -sr LocalMachine -eku

# Find the 1st certificate (preinstalled) in the local machine store (note: cert store is like a directory structure cert:\)
$sslcert = (Get-ChildItem ‘Cert:\LocalMachine\My’)[0]

Write-host “Installing ADFS”
$cred = New-Object System.Management.Automation.PSCredential (“$netbiosname\$adfsServiceAccountName”, $secure_string)
Install-AdfsFarm `
-CertificateThumbprint $sslcert.Thumbprint `
-FederationServiceDisplayName $adfsFarmName `
-FederationServiceName $adfsname `
-ServiceAccountCredential $cred `

write-host “Configuing proxy credentials”
$webClient = new-object System.Net.WebClient
$webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

# Establishing Two Way Trust AWS trusts company
#Download and save the ADFS metadata to a tempfile
#Because of self signed, need to disable the SSL Validation
# otherwise WebClient.DownloadFile will fail.
$adfsMetadataUrl = “https://localhost/FederationMetadata/2007-06/FederationMetadata.xml”
$metadatapath = [IO.Path]::GetTempFileName()
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Write-host “Establishing Two Way Trust company trusts AWS”
Add-ADFSRelyingPartyTrust -Name “Amazon” -MetadataUrl $AWSAccountSAMLMetadata

$webClient.DownloadFile( $adfsMetadataUrl, $metadatapath )
write-host “Saved to: $metadatapath”

# Register a new SAML Provider with IAMS that has this ADFS information
# Note, SSL interception on proxy will break this
$role = New-IAMSAMLProvider -Name “ADFS” -SAMLMetadataDocument (cat $metadatapath)
$account = $role.Substring(13,12)
write-host “Updated AWS account number: $account”
del $metadatapath

# Trust policy configures WHO can assume the role. Note the URL is always even if your AWS account uses another URL to sign into AWS console.
#custom replacement is used for $role, so don’t have to deal with escape chars
$trustPolicy = @’
“Version”: “2012-10-17”,
“Statement”: [
“Effect”: “Allow”,
“Action”: “sts:AssumeRoleWithSAML”,
“Principal”: {“Federated”: “$role”},
“Condition”: {
“StringEquals”: {“SAML:aud”: “”}
$trustPolicy = $trustPolicy.Replace(‘$role’, $role)

# Access Policies: Note do you own homework to decide what access policies are appropriate for you
$AdminAccessPolicy = @’
“Version”: “2012-10-17”,
“Statement”: [
“Sid”: “Stmt1434192117145”,
“Action”: “*”,
“Effect”: “Allow”,
“Resource”: “*”

# Create Roles
New-IAMRole -AssumeRolePolicyDocument $trustPolicy -RoleName “ADFS-Administrators”
Write-IAMRolePolicy -RoleName “ADFS-Administrators” -PolicyName “Administrator-Access” -PolicyDocument $AdminAccessPolicy

#Configure Assertions for the SAML Authentication Response
$nameId = @’
@RuleTemplate = “MapClaims”
@RuleName = “Name ID”
c:[Type == “”]
issue(Type = “”,
Issuer = c.Issuer,
OriginalIssuer = c.OriginalIssuer,
Value = c.Value,
ValueType = c.ValueType,
= “urn:oasis:names:tc:SAML:2.0:nameid-format:persistent”);

$roleSessionName = @’
@RuleTemplate = “LdapClaims”
@RuleName = “RoleSessionName”
c:[Type == “”,
Issuer == “AD AUTHORITY”]
issue(store = “Active Directory”,
types = (“”),
query = “;userPrincipleName;{0}”, param = c.Value);

#list of AD groups is first stored in a temporary variables
$tempVariable = @’
@RuleName = “Save AD Group Into http://temp/variable”
c:[Type == “”,
Issuer == “AD AUTHORITY”]
add(store = “Active Directory”,
types = (“http://temp/variable”),
query = “;tokenGroups;{0}”,
param = c.Value);

#RegEx here to replace AD AWS- group names with IAM ADFS- role names
$roleMapping = @’
@RuleName = “Role mapping”
c:[Type == “http://temp/variable”, Value =~ “(?i)^AWS-“]
issue(Type = “”, Value =
RegExReplace(c.Value, “AWS-“,

$roleMapping = $roleMapping.Replace(‘$account’, $account)
$ruleset = New-AdfsClaimRuleSet -ClaimRule $nameId,$roleSessionName, $tempVariable, $roleMapping

$issuanceAuthorizationRules = @’
@RuleTemplate = “AllowAllAuthzRule”
issue(Type = “”,
Value = “true”);

Set-ADFSRelyingPartyTrust -TargetName Amazon -IssuanceTransformRules $ruleset.ClaimRulesString -IssuanceAuthorizationRules $issuanceAuthorizationRules


Find more IT Infrastructure tips at

Leave a Reply