Skip to content

Commit 5447f45

Browse files
committed
gptel-bedrock: aws profile keyword argument
This extends the previous AWS_PROFILE based get-credentials implementation to allow explicit setting of the profile
1 parent 8f8416b commit 5447f45

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

README.org

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,7 @@ Register a backend with
10021002
:model-region 'apac)
10031003
#+end_src
10041004

1005-
The Bedrock backend gets your AWS credentials from the environment variables. It expects to find either
1006-
~AWS_ACCESS_KEY_ID~, ~AWS_SECRET_ACCESS_KEY~, ~AWS_SESSION_TOKEN~ (optional), or if present, can use ~AWS_PROFILE~ to get these directly from the ~aws~ cli.
1005+
When the ~:profile~ option is specified, the Bedrock backend uses the shared AWS config and credentials files to obtain credentials based on the AWS Profile selected. Alternatively ~AWS_PROFILE~ environment variable can be used to provide the profile name. If ~AWS_ACCESS_KEY_ID~, ~AWS_SECRET_ACCESS_KEY~ and ~AWS_SESSION_TOKEN~ environment variables are provided the profile will be ignored.
10071006

10081007
NOTE: The Bedrock backend needs curl >= 8.5 in order for the sigv4 signing to work properly,
10091008
https://github.com/curl/curl/issues/11794
@@ -1022,6 +1021,8 @@ The above code makes the backend available to select. If you want it to be the
10221021
(gptel-make-bedrock "AWS"
10231022
;; optionally enable streaming
10241023
:stream t
1024+
;; optionally specify the aws profile
1025+
;; :profile
10251026
:region "ap-northeast-1"
10261027
;; subset of gptel--bedrock-models
10271028
:models '(claude-sonnet-4-20250514)

gptel-bedrock.el

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,16 @@ Non-nil CLEAR-CACHE will refresh credentials."
513513
(gptel-bedrock--fetch-aws-profile-credentials profile t))
514514
(t (user-error "AWS credentials expired for profile: %s" profile)))))
515515

516-
(defun gptel-bedrock--get-credentials ()
516+
(defun gptel-bedrock--get-credentials (profile)
517517
"Return the AWS credentials to use for the request.
518518
519+
If credentials are not available based on the AWS_ACCESS_KEY_ID
520+
AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN environment variables,
521+
aws configure export-credentials is used to obtain
522+
credentials. PROFILE specifies the AWS profile to use for
523+
retrieving credentials if AWS_PROFILE environment variable is not
524+
set.
525+
519526
Returns a list of 2-3 elements, depending on whether a session
520527
token is needed, with this form: (AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
521528
AWS_SESSION_TOKEN).
@@ -524,11 +531,12 @@ Convenient to use with `cl-multiple-value-bind'"
524531
(let ((key-id (getenv "AWS_ACCESS_KEY_ID"))
525532
(secret-key (getenv "AWS_SECRET_ACCESS_KEY"))
526533
(token (getenv "AWS_SESSION_TOKEN"))
527-
(profile (getenv "AWS_PROFILE")))
534+
(profile (or (getenv "AWS_PROFILE")
535+
profile)))
528536
(cond
529537
((and key-id secret-key) (cl-values key-id secret-key token))
530538
((and profile) (gptel-bedrock--fetch-aws-profile-credentials profile))
531-
(t (user-error "Missing AWS credentials; currently only environment variables are supported")))))
539+
(t (user-error "Missing AWS credentials; provide them either via environment variables or specify PROFILE when calling gptel-make-bedrock")))))
532540

533541
(defvar gptel-bedrock--model-ids
534542
;; https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
@@ -577,10 +585,12 @@ REGION is one of apac, eu or us."
577585
(or (alist-get model gptel-bedrock--model-ids nil nil #'eq)
578586
(error "Unknown Bedrock model: %s" model))))
579587

580-
(defun gptel-bedrock--curl-args (region)
581-
"Generate the curl arguments to get a bedrock request signed for use in REGION."
588+
(defun gptel-bedrock--curl-args (region profile)
589+
"Generate the curl arguments to get a bedrock request signed for use in REGION.
590+
591+
PROFILE specifies the aws profile to use for aws configure export-credentials."
582592
;; https://curl.se/docs/manpage.html#--aws-sigv4
583-
(cl-multiple-value-bind (key-id secret token) (gptel-bedrock--get-credentials)
593+
(cl-multiple-value-bind (key-id secret token) (gptel-bedrock--get-credentials profile)
584594
(nconc
585595
(list
586596
"--user" (format "%s:%s" key-id secret)
@@ -604,6 +614,7 @@ REGION is one of apac, eu or us."
604614
region
605615
(models gptel--bedrock-models)
606616
(model-region nil)
617+
(profile nil)
607618
(stream nil)
608619
curl-args
609620
(protocol "https"))
@@ -614,6 +625,7 @@ Keyword arguments:
614625
REGION - AWS region name (e.g. \"us-east-1\")
615626
MODELS - The list of models supported by this backend
616627
MODEL-REGION - one of apac, eu, us or nil
628+
PROFILE - the aws profile to use for aws configure export-credentials
617629
CURL-ARGS - additional curl args
618630
STREAM - Whether to use streaming responses or not."
619631
(declare (indent 1))
@@ -627,12 +639,12 @@ STREAM - Whether to use streaming responses or not."
627639
:host host
628640
:header nil ; x-amz-security-token is set in curl-args if needed
629641
:models (gptel--process-models models)
630-
:model-region model-region
642+
:model-region model-region
631643
:protocol protocol
632644
:endpoint "" ; Url is dynamically constructed based on other args
633645
:stream stream
634646
:coding-system (and stream 'binary)
635-
:curl-args (lambda () (append curl-args (gptel-bedrock--curl-args region)))
647+
:curl-args (lambda () (append curl-args (gptel-bedrock--curl-args region profile)))
636648
:url
637649
(lambda ()
638650
(concat protocol "://" host

0 commit comments

Comments
 (0)