

 **Help improve this page** 

To contribute to this user guide, choose the **Edit this page on GitHub** link that is located in the right pane of every page.

# Use IAM for agents in Amazon EKS from application code with a SPIFFE library

This walkthrough shows how a workload can obtain and use its AWS-issued identity directly from application code, with no sidecar and no service mesh. The application reads its identity from the EKS Pod Identity agent over the SPIFFE Workload API and does mutual TLS itself.

This is one of several ways to consume workload identity. For most Kubernetes customers, a service mesh or a sidecar proxy is the more common choice, because it keeps identity handling out of the application. Use the library approach when you want the application itself to hold and present its identity, for example a standalone service that is not in a mesh.

**Note**  
IAM for agents is in preview release for Amazon EKS and is subject to change. It is available in all commercial AWS Regions except Middle East (Bahrain) (`me-south-1`) and Middle East (UAE) (`me-central-1`).

This walkthrough applies to any EKS cluster where the EKS Pod Identity agent is available, not only EKS Auto Mode. The configuration is the same on managed node groups, self-managed nodes, and Karpenter, when the prerequisites are met.

## The approach
<a name="pod-id-wi-library-approach"></a>

The EKS Pod Identity agent exposes the SPIFFE Workload API over a node-local unix socket. A workload calls that API to fetch its X.509-SVID and trust bundle, and to be notified when they rotate. A SPIFFE library wraps these calls so that your application works with a maintained identity source rather than parsing certificates itself.

SPIFFE libraries exist for several languages, for example go-spiffe for Go, java-spiffe for Java, and py-spiffe for Python. The approach is the same in each. Point the library at the Pod Identity agent socket, obtain a source that stays current as the identity rotates, and use it to build the TLS configuration for your server and client connections. This walkthrough uses [go-spiffe](https://github.com/spiffe/go-spiffe) as the worked example.

The example chains three services, `frontend` to `orders` to `payments`. Each runs under its own service account and authenticates to the next over mutual TLS, authorizing the peer by its SPIFFE ID.

## Prerequisites
<a name="pod-id-wi-library-prereqs"></a>
+ An EKS cluster with the EKS Pod Identity agent available. For more information, see [Prerequisites](pod-identity-workload-identity.md#pod-id-wi-prereqs). Enabling workload identity on the pod projects the Pod Identity agent socket into it.
+ A workload identity provider and a template. Any template works. Whatever SPIFFE IDs your template renders are the identities that your application authorizes against. This walkthrough uses the `cluster.local` trust domain and the mesh path shape (`spiffe://cluster.local/ns/<namespace>/sa/<service account>`). Substitute the SPIFFE IDs that your own template produces.
+ A Pod Identity association for each service account, with `workloadIdentityConfig` enabled and referencing the provider and template.

## Obtain the identity source
<a name="pod-id-wi-library-source"></a>

Create a SPIFFE X.509 source pointed at the Pod Identity agent socket. The source fetches the workload’s SVID and trust bundle and keeps them current, so you create it once and reuse it for every connection.

```go
import (
    "github.com/spiffe/go-spiffe/v2/spiffeid"
    "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
    "github.com/spiffe/go-spiffe/v2/workloadapi"
)

const socket = "unix:///var/run/secrets/pods.eks.amazonaws.com/workloadidentity/agent.sock"

source, err := workloadapi.NewX509Source(ctx,
    workloadapi.WithClientOptions(workloadapi.WithAddr(socket)))
if err != nil {
    log.Fatalf("failed to create X509 source: %v", err)
}
defer source.Close()
```

## Serve mutual TLS (authorize the caller)
<a name="pod-id-wi-library-server"></a>

Build a server TLS configuration from the source that requires the client to present its own SVID, and authorize a specific caller by its SPIFFE ID. Here the `payments` service accepts calls only from `orders`.

```go
allowed := spiffeid.RequireFromString("spiffe://cluster.local/ns/payments/sa/orders-sa")

server := &http.Server{
    Addr:      ":8443",
    Handler:   handler,
    TLSConfig: tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeID(allowed)),
}
log.Fatal(server.ListenAndServeTLS("", ""))
```

## Call another service over mutual TLS (authorize the server)
<a name="pod-id-wi-library-client"></a>

Build a client TLS configuration from the same source, and require the upstream to present an expected SPIFFE ID. Here `orders` calls `payments` and requires the `payments` identity.

```go
paymentsID := spiffeid.RequireFromString("spiffe://cluster.local/ns/payments/sa/payments-sa")

client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeID(paymentsID)),
    },
}
resp, err := client.Get("https://payments.payments.svc:8443/")
```

Both sides present their AWS-issued SVID and verify the other’s SPIFFE ID against the trust bundle. Neither side holds a long-lived secret, and the source refreshes the material as it rotates.

## Deploy
<a name="pod-id-wi-library-deploy"></a>

Deploy each service with its own service account, and enable the workload-identity socket on the pod with the pod-identity opt-in label so that the library can reach the Pod Identity agent. No sidecar is added.

## Verify
<a name="pod-id-wi-library-verify"></a>

 **The application obtained its identity.** At startup, the service logs the SVID that it received, for example `spiffe://cluster.local/ns/payments/sa/payments-sa`. If the source cannot reach the Pod Identity agent socket, creating it fails and the service does not start.

 **The chain works over mutual TLS.** A request through `frontend` to `orders` to `payments` returns successfully. Because each server authorizes a specific caller SPIFFE ID, the call succeeds only when the peer presents the expected AWS-issued identity.

 **The peer identity is enforced.** A caller whose SPIFFE ID is not the authorized one is rejected by the server `AuthorizeID` check, and a server that presents an unexpected SPIFFE ID is rejected by the client.

For the sidecar equivalents, see [Use IAM for agents with Istio in Amazon EKS](pod-id-wi-istio.md) and [Use IAM for agents with self-managed Envoy sidecars in Amazon EKS](pod-id-wi-envoy.md).