You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/languages/python.md
+181Lines changed: 181 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -586,6 +586,187 @@ curl -X POST http://127.0.0.1:8080/function/s3-example?key=hello.txt \
586
586
curl http://127.0.0.1:8080/function/s3-example
587
587
```
588
588
589
+
## Example: Manage AWS ECR repositories with IRSA
590
+
591
+
This example is for OpenFaaS deployed on [AWS EKS](https://aws.amazon.com/eks/). It shows how to use the `boto3` SDK with [IAM Roles for Service Accounts (IRSA)](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) to manage AWS ECR repositories from a Python function. Instead of storing static AWS credentials as secrets, the function obtains ambient credentials automatically from a Kubernetes Service Account that is mapped to an IAM role.
592
+
593
+
IRSA is the recommended way to handle AWS credentials for functions running on EKS. It avoids long-lived static credentials that need to be manually rotated, and follows the principle of least privilege by scoping permissions to individual functions through IAM roles.
594
+
595
+
IRSA must be enabled on your EKS cluster. This requires an IAM OIDC identity provider associated with the cluster. See [Creating an IAM OIDC provider for your cluster](https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html) for setup instructions. For a more detailed end-to-end walkthrough, see: [Manage AWS Resources from OpenFaaS Functions With IRSA](https://www.openfaas.com/blog/irsa-functions/).
596
+
597
+
**1. Create an IAM Policy**
598
+
599
+
Create a policy that grants the permissions your function needs. This example creates and queries ECR repositories:
600
+
601
+
```json
602
+
{
603
+
"Version": "2012-10-17",
604
+
"Statement": [
605
+
{
606
+
"Effect": "Allow",
607
+
"Action": [
608
+
"ecr:CreateRepository",
609
+
"ecr:DeleteRepository",
610
+
"ecr:DescribeRepositories"
611
+
],
612
+
"Resource": "*"
613
+
}
614
+
]
615
+
}
616
+
```
617
+
618
+
Save the above to `ecr-policy.json` and create the policy:
619
+
620
+
```bash
621
+
aws iam create-policy \
622
+
--policy-name ecr-create-query-repository \
623
+
--policy-document file://ecr-policy.json
624
+
```
625
+
626
+
Note the ARN from the output, e.g. `arn:aws:iam::ACCOUNT_NUMBER:policy/ecr-create-query-repository`.
627
+
628
+
**2. Create an IAM Role and Kubernetes Service Account**
629
+
630
+
Use `eksctl` to create a Kubernetes Service Account in the `openfaas-fn` namespace that is linked to an IAM role with the policy attached:
This can also be done manually by creating the IAM Role in AWS, followed by a Kubernetes Service Account annotated with `eks.amazonaws.com/role-arn`.
646
+
647
+
**3. Create the function**
648
+
649
+
Pull the `python3-http-debian` template and scaffold a new function:
650
+
651
+
```bash
652
+
faas-cli template store pull python3-http-debian
653
+
faas-cli new --lang python3-http-debian ecr-create-repo \
654
+
--prefix ttl.sh/openfaas-examples
655
+
```
656
+
657
+
**4. Add the boto3 dependency**
658
+
659
+
Add `boto3` to the function's `requirements.txt`:
660
+
661
+
```
662
+
boto3
663
+
```
664
+
665
+
**5. Configure the function**
666
+
667
+
Update `stack.yaml` to set the AWS region and assign the Kubernetes Service Account created for IRSA. The `com.openfaas.serviceaccount` annotation tells OpenFaaS which service account to attach to the function's pod:
No secrets are needed — the AWS SDK picks up credentials automatically from the service account token that is mounted into the pod by EKS.
682
+
683
+
**6. Write the handler**
684
+
685
+
The handler uses `boto3` to create ECR repositories. The `boto3` session is initialised once and reused across invocations. Because IRSA is configured, the SDK automatically obtains temporary credentials from the service account token without any explicit credential management.
0 commit comments