I'm a newbie in AWS environment and I can't really find a good answer to my problem.
There is a csv file that is located in an Amazon S3 bucket. I have to send an email to the email placed in the second column if the value in the first column equals to high.
Metric | Email address |
---|---|
High | [email protected] |
Low | [email protected] |
Is this achievable just by using Lambda and SES, or do I have to use anything else?
Thank you for your help.
CodePudding user response:
Following this tutorial, create a lambda function that will be triggered on an S3 object upload.
From the event data, you will know the name of the object that was uploaded. In the lambda, get the contents of said file from S3.
If the criteria is met, then use SES from within the lambda to send an email.
N.B. You'll need to configure the lambda function to have an IAM role to read from S3, as well as to send an email with SES.
CodePudding user response:
Yes, this is a valid use case. Here are High Level steps (I am answering this with assumption from Java API) :
Create a Lambda function that uses the S3 and SES APIs. The IAM role you need for the Lambda function needs S3 and SES permissions.
Call the S3 getObjectAsBytes method to get the CSV file.
Read the CSV and for each email address where there is a High value, invoke the SES send email method.
Here is the S3 Code:
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(keyName)
.bucket(bucketName)
.build();
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
Here is the SES Code to send an email.
public static void send(SesClient client,
String sender,
String recipient,
String subject,
String bodyText,
String bodyHTML
) throws MessagingException {
Destination destination = Destination.builder()
.toAddresses(recipient)
.build();
Content content = Content.builder()
.data(bodyHTML)
.build();
Content sub = Content.builder()
.data(subject)
.build();
Body body = Body.builder()
.html(content)
.build();
Message msg = Message.builder()
.subject(sub)
.body(body)
.build();
SendEmailRequest emailRequest = SendEmailRequest.builder()
.destination(destination)
.message(msg)
.source(sender)
.build();
try {
System.out.println("Attempting to send an email through Amazon SES " "using the AWS SDK for Java...");
client.sendEmail(emailRequest);
} catch (SesException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}