The IAM Roles Anywhere Plugin for AWS SDK for Java is an AWS SDK for Java v2 plugin for IAM Roles Anywhere. Sign requests with an X.509 certificate to obtain temporary AWS credentials, without long-term access keys.
AWS IAM Roles Anywhere allows your workloads running outside of AWS (on-premises servers, containers, or other cloud environments) to obtain temporary AWS credentials by using X.509 certificates. This eliminates the need to store long-term AWS access keys in your applications.
Scenario: You have an application running on-premises or in another cloud that needs to access AWS services securely without embedding long-term credentials.
Solution: Add RolesAnywherePlugin to your AWS SDK clients. It calls IAM Roles Anywhere CreateSession and supplies temporary, auto-rotating AWS credentials.
- Trust Anchor: A root certificate authority (CA) or intermediate CA certificate registered with IAM Roles Anywhere
- Profile: An IAM Roles Anywhere profile that defines which roles can be assumed
- Role: An IAM role with the necessary permissions for your application
- Certificate: An X.509 certificate signed by your registered CA
- Private Key: The private key corresponding to your certificate
import software.amazon.rolesanywhere.plugin.RolesAnywherePlugin; import software.amazon.rolesanywhere.plugin.X509IdentityProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import java.nio.file.Paths; // fromFiles re-reads the cert and key on every credential refresh, so on-disk // rotation is picked up automatically without restarting the process. RolesAnywherePlugin plugin = RolesAnywherePlugin.builder() .identityProvider(X509IdentityProvider.fromFiles( Paths.get("path/to/certificate.pem"), Paths.get("path/to/private-key.pem"), "RSA")) .trustAnchorArn("arn:aws:rolesanywhere:us-east-1:123456789012:trust-anchor/12345678-1234-1234-1234-123456789012") .profileArn("arn:aws:rolesanywhere:us-east-1:123456789012:profile/12345678-1234-1234-1234-123456789012") .roleArn("arn:aws:iam::123456789012:role/MyApplicationRole") // .region(Region.US_EAST_1) // Optional - inferred from ARNs .build(); S3Client s3Client = S3Client.builder() .addPlugin(plugin) .region(Region.US_EAST_1) .build(); s3Client.listBuckets();
Adding the plugin to a non-AWS SDK client throws IllegalStateException at configuration time.
If the SDK client already has a credentialsProvider configured, the plugin
composes with it via AwsCredentialsProviderChain — the customer's provider
is tried first, and RolesAnywhereCredentialsProvider is the fallback. The
plugin never silently overwrites explicit customer configuration.
Pick one of the pre-built factories for the common cases; implement
resolve() yourself only when the certificate + key come from a dynamic
backend.
| Factory | When to use |
|---|---|
X509IdentityProvider.fromFiles(cert, key, alg) |
Certificate and PKCS#8 key on local disk. Re-read on every refresh so on-disk rotation is picked up automatically. |
X509IdentityProvider.fromFiles(cert, key, alg, password) |
Same, for password-protected PKCS#8 keys. |
X509IdentityProvider.fromFiles(cert, key, alg, chain) |
Same, with a separate intermediate chain file. |
X509IdentityProvider.fromFiles(cert, key, alg, chain, password) |
Password-protected key with intermediate chain. |
X509IdentityProvider.ofStatic(identity) |
Identity is embedded/static (tests, HSM handles built once). Same object reused every refresh; the private key is never destroyed. |
Custom resolve() lambda |
Fetching from Secrets Manager, Parameter Store, S3, ACM PCA, KeyStore/PKCS#11, or any dynamic source. Combine with X509Identity.fromPem(...) if the source returns PEM strings. |
import software.amazon.rolesanywhere.plugin.RolesAnywherePlugin; import software.amazon.rolesanywhere.plugin.X509IdentityProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import java.nio.file.Paths; public class RolesAnywhereExample { public static void main(String[] args) throws Exception { RolesAnywherePlugin plugin = RolesAnywherePlugin.builder() .identityProvider(X509IdentityProvider.fromFiles( Paths.get("client-cert.pem"), Paths.get("client-key.pem"), "RSA")) .trustAnchorArn("arn:aws:rolesanywhere:us-east-1:123456789012:trust-anchor/ta-12345") .profileArn("arn:aws:rolesanywhere:us-east-1:123456789012:profile/profile-12345") .roleArn("arn:aws:iam::123456789012:role/MyRole") .roleSessionName("MyApplication") .durationSeconds(3600) // 1 hour // No need to specify .region() - inferred from ARNs .build(); // Add it to AWS clients S3Client s3 = S3Client.builder() .addPlugin(plugin) .build(); // Make API calls s3.listBuckets().buckets().forEach(bucket -> System.out.println("Bucket: " + bucket.name())); } }
- identityProvider:
X509IdentityProviderthat supplies the certificate and private key on every credential refresh - trustAnchorArn: ARN of the trust anchor registered in IAM Roles Anywhere
- profileArn: ARN of the profile that defines role assumption rules
- roleArn: ARN of the IAM role to assume
- region: AWS region (automatically inferred if not specified - see Region Resolution below)
- roleSessionName: Name for the role session (default: generated)
- durationSeconds: Duration of the AWS credentials session in seconds (900-43200, default: 3600)
- endpoint: Custom endpoint URI for testing or special configurations
- fipsEnabled: Use FIPS endpoints.
null(default, unset — resolves to non-FIPS),true, orfalse. FIPS endpoints are only available in US regions and AWS GovCloud. - dualStackEnabled: Use dual-stack IPv4/IPv6 endpoints.
null(default, unset — resolves to non-dual-stack),true, orfalse. - httpClient: Custom HTTP client
- staleTime: How early before AWS credential expiration to trigger a refresh. Defaults to 5 minutes.
- minRefreshInterval: Minimum interval between credential refresh cycles to prevent retry storms. Defaults to 5 minutes. Cannot be less than 30 seconds (use
Duration.ZEROto disable).
The region parameter is optional. The provider automatically resolves the AWS region using this priority order:
- Explicitly set region via
.region()method - System property:
aws.regionsystem property - Environment variable:
AWS_REGIONenvironment variable - ARN inference: Extracted from trust anchor and profile ARNs (if both have the same region)
- Throw exception: Couldn't identify desired region
This means you can often omit the region entirely:
// Region will be automatically inferred from ARNs or environment RolesAnywherePlugin plugin = RolesAnywherePlugin.builder() .identityProvider(X509IdentityProvider.ofStatic(identity)) .trustAnchorArn("arn:aws:rolesanywhere:eu-west-1:123456789012:trust-anchor/ta-12345") .profileArn("arn:aws:rolesanywhere:eu-west-1:123456789012:profile/profile-12345") .roleArn("arn:aws:iam::123456789012:role/MyRole") // No .region() needed - will use eu-west-1 from ARNs .build();
The plugin is the recommended entry point. RolesAnywhereCredentialsProvider is also public, for cases where you need an AwsCredentialsProvider directly: calling resolveCredentials() outside an SDK client, or sharing one provider (and one session cache) across many clients.
import software.amazon.rolesanywhere.plugin.RolesAnywhereCredentialsProvider; import software.amazon.rolesanywhere.plugin.RolesAnywherePlugin; RolesAnywhereCredentialsProvider provider = RolesAnywhereCredentialsProvider.builder() .identityProvider(X509IdentityProvider.ofStatic(identity)) .trustAnchorArn(/* ... */) .profileArn(/* ... */) .roleArn(/* ... */) .build(); // Use it directly... AwsCredentials creds = provider.resolveCredentials(); // ...or wrap it in a plugin to share across clients RolesAnywherePlugin plugin = RolesAnywherePlugin.create(provider); S3Client s3 = S3Client.builder().addPlugin(plugin).build(); DynamoDbClient ddb = DynamoDbClient.builder().addPlugin(plugin).build();
One shared provider gives all clients a single CreateSession exchange and a single credential cache. Calling RolesAnywherePlugin.builder()...build() per client creates a separate provider (and cache) each time.
For certificates signed by intermediate CAs, include the certificate chain:
List<X509Certificate> intermediates = Arrays.asList(intermediateCert1, intermediateCert2); X509Identity identity = X509Identity.create(leafCertificate, privateKey, intermediates);
For certificates issued by AWS Private CA, implement X509IdentityProvider.resolve() to dynamically issue and load short-lived certificates. The private key stays in memory only during credential signing.
import software.amazon.rolesanywhere.plugin.RolesAnywherePlugin; import software.amazon.rolesanywhere.plugin.X509Identity; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.services.acmpca.AcmPcaClient; import software.amazon.awssdk.services.acmpca.model.*; import software.amazon.awssdk.services.acmpca.waiters.AcmPcaWaiter; import software.amazon.awssdk.regions.Region; import java.security.KeyPairGenerator; import java.security.KeyPair; import java.security.cert.X509Certificate; import java.time.Duration; // Create the PCA client once — it holds no key material and is safe to reuse AcmPcaClient pcaClient = AcmPcaClient.builder() .region(Region.US_EAST_1) .build(); // Build the plugin with ACM PCA-issued certificates RolesAnywherePlugin plugin = RolesAnywherePlugin.builder() .identityProvider(() -> { try { // Generate a fresh key pair — private key stays local to this lambda KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); // Issue a short-lived certificate from ACM PCA (reuses shared client) IssueCertificateResponse issueResponse = pcaClient.issueCertificate( IssueCertificateRequest.builder() .certificateAuthorityArn("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/ca-id") .csr(/* CSR built from keyPair.getPublic() */) .signingAlgorithm("SHA256WITHRSA") .validity(Validity.builder().value(1L).type(ValidityPeriodType.DAYS).build()) .build()); // Wait for the certificate to be issued before retrieving it try (AcmPcaWaiter waiter = pcaClient.waiter()) { waiter.waitUntilCertificateIssued( GetCertificateRequest.builder() .certificateAuthorityArn("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/ca-id") .certificateArn(issueResponse.certificateArn()) .build()); } GetCertificateResponse certResponse = pcaClient.getCertificate( GetCertificateRequest.builder() .certificateAuthorityArn("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/ca-id") .certificateArn(issueResponse.certificateArn()) .build()); return X509Identity.fromPem( certResponse.certificate(), pemEncode(keyPair.getPrivate()), "RSA"); } catch (Exception e) { throw SdkClientException.create("Failed to issue certificate from ACM PCA", e); } }) .trustAnchorArn("arn:aws:rolesanywhere:us-east-1:123456789012:trust-anchor/ta-12345") .profileArn("arn:aws:rolesanywhere:us-east-1:123456789012:profile/profile-12345") .roleArn("arn:aws:iam::123456789012:role/MyRole") .minRefreshInterval(Duration.ofMinutes(2)) // Avoid hammering ACM PCA on transient failures .region(Region.US_EAST_1) .build();
This pattern:
- Minimizes key exposure: The private key is a local variable, eligible for GC after signing
- Handles failures: Wraps errors in
SdkClientExceptionso callers get a clear, unchecked signal - Prevents retry storms:
minRefreshIntervalensures ACM PCA isn't called more than once per interval even if credentials are expired
When loading private keys, you must specify the key algorithm type:
- RSA keys: Use
"RSA"as the key type - Elliptic Curve keys: Use
"EC"as the key type - ML-DSA keys: Use
"ML-DSA"as the key type. ML-DSA requires a JCE provider such as BouncyCastle. The provider only needs to be registered once during application startup.
// RSA X509IdentityProvider rsa = X509IdentityProvider.fromFiles( Paths.get("rsa-cert.pem"), Paths.get("rsa-key.pem"), "RSA"); // EC X509IdentityProvider ec = X509IdentityProvider.fromFiles( Paths.get("ec-cert.pem"), Paths.get("ec-key.pem"), "EC"); // ML-DSA — register a supporting JCE provider at application startup Security.addProvider(new BouncyCastleProvider()); X509IdentityProvider mldsa = X509IdentityProvider.fromFiles( Paths.get("mldsa-cert.pem"), Paths.get("mldsa-key.pem"), "ML-DSA");
The provider validates all inputs and throws descriptive exceptions:
IllegalArgumentException: Invalid ARNs, missing required parametersSdkClientException:X509IdentityProviderfailed (cert store unavailable, HSM error, IO), network errors, or invalid responses from AWSSdkServiceException: IAM Roles Anywhere returned an error response. Includes expired or otherwise invalid certificates (validated server-side as part ofCreateSession)
- Protect Private Keys: Store private keys securely and never commit them to version control
- Certificate Rotation: Implement regular certificate rotation
- Least Privilege: Grant minimal necessary permissions to the IAM role
- Monitor Usage: Use AWS CloudTrail to monitor credential usage
- Validate Certificates: Ensure certificates are valid and not expired
- Private key lifecycle: Every
X509Identitycarries an ownership bit that controls whether the plugin will callPrivateKey.destroy()on the key after eachCreateSessionsigning. JCE providers that honorDestroyable(BouncyCastle, some HSM providers) will zero their internal secret state; JDK-bundled providers throwDestroyFailedException, which the plugin swallows at debug log level.X509Identity.create(cert, privateKey, ...),X509Identity.fromPem(...), andX509IdentityProvider.fromFiles(...)are plugin-owned: the private key is destroyed after use. This is the safe default for keys loaded per refresh (disk, Secrets Manager, Parameter Store, dynamic ACM PCA issuance).X509Identity.createWithoutDestroyingKey(cert, privateKey, ...)andX509IdentityProvider.ofStatic(...)are caller-owned: the plugin never destroys the key. Use these for HSM handles, PKCS#11 keys, or any key shared across other JCE consumers, where destroying would break the next refresh or another subsystem.
The certificates and encrypted PKCS#8 keys under
src/test/resources/certificates/ are test-only fixtures — locally
generated keys used to exercise parsing, decryption, and signing code
paths. They grant no access to any real system and must never be used in
production.
- Each fixture file is listed in
.gitallowedat the repo root so secret scanners do not flag it as a leaked credential. - See
src/test/resources/certificates/README.mdfor full context and for instructions on regenerating the fixtures viagenerate-encrypted-keys.sh.
We chose to commit pre-generated fixtures rather than generate them from
a Gradle task at test time. Generating at test time would remove the keys
from the repository entirely, but it also requires openssl on every
build host and adds per-run latency to the test suite. Given that these
keys grant no access to anything, we considered the build-hermeticity
trade-off worth it.
Method 1: Using JSON file (Recommended)
# Create trust anchor JSON file cat > trust-anchor.json << EOF { "name": "MyTrustAnchor", "source": { "sourceType": "CERTIFICATE_BUNDLE", "sourceData": { "x509CertificateData": "$(cat ca-cert.pem | sed 's/$/\\n/' | tr -d '\n' | sed 's/\\n$//')" } }, "enabled": true } EOF # Create trust anchor aws rolesanywhere create-trust-anchor \ --cli-input-json file://trust-anchor.json \ --region us-east-1
Method 2: One-liner with jq
aws rolesanywhere create-trust-anchor \
--cli-input-json "$(jq -n --rawfile cert ca-cert.pem '{
name: "MyTrustAnchor",
source: {
sourceType: "CERTIFICATE_BUNDLE",
sourceData: {
x509CertificateData: $cert
}
},
enabled: true
}')" \
--region us-east-1{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "rolesanywhere.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}aws rolesanywhere create-profile \
--name "MyProfile" \
--role-arns "arn:aws:iam::123456789012:role/MyRole"This is a Java library built with Gradle.
# Build the library ./gradlew build # Run tests ./gradlew test # Run checkstyle on main code ./gradlew checkstyleMain # Run checkstyle on test code ./gradlew checkstyleTest
- AWS SDK for Java v2
- Java 8 or higher