Fork me on GitHub
#aws
<
2023-10-22
>
Caio Cascaes03:10:36

Has someone used the lib cognitec for AWS with SES? Is there some example or documentation to help me? My attempt, as I understand that lib cognitec of AWS is a translation of AWS CLI syntax to Clojure map syntax, so I deduced and implemented as:

(defn aws-client-factory
  [aws-service]
  {:api                  aws-service
   :region               (env :aws-region)
   :credentials-provider (credentials/basic-credentials-provider
                           {:access-key-id     (env :aws-access-key-id)
                            :secret-access-key (env :aws-secret-access-key)})})

(defn aws-ses-client
  []
  (aws/client (aws-client-factory :sesv2)))
Then
(s/defn send!
  [email :- models.schemata/Email
   template :- s/Str]
  (aws/invoke (config/aws-ses-client)
              {:op      :SendEmail
               :request {:From        config/ses-sender
                         :Destination {:ToAddresses [email]}
                         :Message     {:Subject {:Data    config/ses-subject-confirmation
                                                 :Charset "UTF-8"}
                                       :Body    {:Html {:Data    template
                                                        :Charset "UTF-8"}}}}}))
I'm getting 403. The log isn't clear if it's something wrong with AWS credentials or I did something wrong with implementation. I verified the AWS credentials:
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Sid": "VisualEditor0",
			"Effect": "Allow",
			"Action": [
				"ses:SendEmail",
				"ses:SendRawEmail"
			],
			"Resource": "*"
		}
	]
}
So the operation :SendEmail should work as I understand. Does someone have a clue? Or some example to help me? Thanks!

lukasz17:10:57

I don't have access to an AWS account anymore, but one thing: > as I understand that lib cognitec of AWS is a translation of AWS CLI syntax to Clojure map syntax, That's not what aws-api does - it translates the AWS' web API definitions into Clojure map syntax (ish, there's a bit of nuance there). So you need to use this as the reference https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_SendEmail.html or use built-in facilities of aws-api to get the docs - once you build an API client you can use it to get detailed information about each operation and its parameters. AWS CLI is not a 1-1 mapping of the API, there are some things that work differently or are simplified (e.g. aws s3 vs aws s3api etc)

lukasz17:10:09

2nd thing - you shared the IAM policy, but can you actually confirm that the AWS credentials are valid by calling STS' GetCallerIdentity? https://docs.aws.amazon.com/STS/latest/APIReference/API_GetCallerIdentity.html

Caio Cascaes19:10:02

Thank you for correcting my assumption. I'll do the STS stuff to verify.

👍 1
Caio Cascaes22:10:40

I managed to make it work! 🙌 After running (aws/ops (aws/client {:api :sesv2})) , I got the details of :SendEmail mapping/syntax, adjusted it to the code and recreated the IAM after running the GetCallerIdentity, that blamed that IAM was somewhat not working properly (I don't figure out of how and why), and 🎉 - e-mail send successfully! Also had to add the destination e-mail in the verified list when sandbox. Thanks a lot @U0JEFEZH6! 🤝 Here is the working code:

(s/defn send!
  [email :- models.schemata/Email
   template :- s/Str]
  (aws/invoke (config/aws-ses-client)
              {:op      :SendEmail
               :request {:FromEmailAddress config/ses-sender
                         :Destination      {:ToAddresses [email]}
                         :Content          {:Simple {:Subject {:Data    config/ses-subject-confirmation
                                                               :Charset "UTF-8"}
                                                     :Body    {:Html {:Data    template
                                                                      :Charset "UTF-8"}}}}}}))

🎉 1