← Solutions Architect Associate study path

Practice in your account · 20 min

An Amazon S3 event that invokes a Lambda function

In this lab you will make every file you upload to a folder in a bucket trigger a Lambda function, and you will see the invocation in CloudWatch Logs.

In this lab you will build an event pattern, in which a file that arrives in Amazon S3 is processed without a running and waiting for it. An event is a notice that something happened, such as a new file arriving. For example, when a seller at an online store uploads a product photo, that notice can set a program to work checking the photo, without the store paying for a server that spends the day waiting for photos.

Amazon S3 stores files in , and each stored file is called an object. AWS Lambda runs a piece of code, called a function, only when something calls it, and charges only for that time. When you upload a file to the subidas/ folder of a bucket, S3 will send an s3:ObjectCreated:* notification, which is the notice that an object was created, and AWS Lambda will run a function that reads the object and writes its type and size to .

The exam calls this way of connecting two services loose coupling, which is a topic in domain 2. The bucket doesn't know what the function does and the function doesn't know who uploaded the file, so you can change the function without touching the bucket. For example, if tomorrow the store wants the function to also make a small version of each photo, it changes only the function's code, and the bucket and the sellers keep working the same way.

An S3 event that processes each new photo

The full version of this pattern puts an Amazon SQS queue between the bucket and the function and writes the output to a second bucket. A queue is a line of messages waiting their turn: it holds the notices until the function can handle them. This lab builds the short version, in which the bucket invokes the function directly, that is, sets it running, and the function only writes to the logs, which are the messages it leaves while it runs.

The second bucket exists because, if the function wrote its result to subidas/ in the same bucket, it would invoke itself. For example, if the store's function saved the small photo in subidas/, that new photo would trigger the function again, and the function would create another photo, over and over. That is called a recursive loop.

Before you start

First do the budget lab. This lab's CloudFormation template, which is the file that describes everything that will be created, creates an , so when you create the stack you have to check the box under Capabilities. That role is what gives the function its permissions, and the box confirms you know the template creates permissions.

Each bucket name is unique across all AWS accounts, so two people can't use the same one. This lab's bucket name doesn't clash with anyone else's, because the template builds it with your account number and the (cloudpath-saa-lab08c-<cuenta>-<región>). For example, if someone else does this lab with their own account, their account number is different and their bucket has a different name.

What the template creates

The [lab08-s3-evento-lambda.yaml](/roadmaps/plantillas/lab08-s3-evento-lambda.yaml) template creates four resources:

  • A role for the function, which has AWSLambdaBasicExecutionRole (to write logs) and s3:GetObject (to read objects) only on the lab's bucket.
  • A Python 3.14 function with 128 MB of memory, which for each new object writes a line with its path, its content type and its size.
  • The permission for S3 to invoke the function (AWS::Lambda::Permission), which is limited to your account and to that bucket.
  • The bucket, which is private and has an s3:ObjectCreated:* notification filtered by the subidas/ prefix.

The prefix is the beginning of an object's name, and in S3 it works as a folder. Filtering the notification by the subidas/ prefix means that only objects whose name starts that way trigger the function. For example, the store can keep the photos sellers send in subidas/ and its invoices in another folder, and only the photos set the function to work.

The AWS tutorial that goes with this step creates the same thing by hand in the Lambda console. The template saves you from creating the role and the permission, and it also adds the prefix filter.

Create the stack and upload the files

A stack is the set of resources CloudFormation creates from the template, and its outputs are the data it gives you when it finishes. You will upload the files from and check the result in CloudWatch. For example, the NombreDelBucket output is the name you will type in the commands to upload the files.

  1. In CloudFormation, choose Create stack, With new resources (standard), Upload a template file and upload lab08-s3-evento-lambda.yaml.
  2. Name the stack cloudpath-saa-lab08c-evento-lambda. Under Capabilities, check the IAM resources box and choose Submit.
  3. When the stack reaches CREATE_COMPLETE, open Outputs and copy NombreDelBucket and GrupoDeLogs.
  4. Open the Lambda console and the stack's function. In the Function overview panel, S3 shows up as a trigger. Choose it to see that the event is s3:ObjectCreated:* with the subidas/ prefix.
  5. Open CloudShell and upload one file inside subidas/ and another one outside it:

bash B=<NombreDelBucket> echo "hola" > a.txt aws s3 cp a.txt s3://$B/subidas/a.txt aws s3 cp a.txt s3://$B/otra-carpeta/a.txt

  1. In CloudWatch, open Log groups and the /aws/lambda/... group named in the GrupoDeLogs output. Open the most recent stream. There should be an Objeto nuevo: s3://.../subidas/a.txt tipo=text/plain bytes=5 line and none for otra-carpeta/. The second file didn't generate an event because the prefix filter left it out.

A log group stores a function's logs, and each stream is a series of that function's messages ordered by time. The subidas/a.txt line is the proof that S3 sent the notice and Lambda ran the function. For example, at the store, each photo uploaded to subidas/ would leave its own line in this group, and an invoice saved in another folder wouldn't leave any.

If your account is a new experience account and you create the function by hand from the console instead of using the template, the IAM role manager creates the execution role for you. With the template nothing changes.

True or false?

0 of 3
  1. The file you uploaded to otra-carpeta/ also invoked the function.

  2. You can change the function's code without touching the bucket's configuration.

  3. If the function wrote its result to subidas/ in the same bucket, it would invoke itself.

How to know you're done

To check the invocations from CloudShell, use the log group from the output:

aws logs filter-log-events --log-group-name "<GrupoDeLogs>" --filter-pattern "Objeto" \
  --query "events[].message" --output text

This command searches the log group for the lines that contain the word Objeto and shows them. The result has to show a single line, the one for subidas/a.txt. If you upload another file to subidas/, a second one shows up within a few seconds. For example, if you uploaded b.txt to otra-carpeta/, no new line would show up.

Delete what you created

So you don't leave anything in the account, delete the lab in this order:

  1. Empty the bucket, because CloudFormation can't delete a bucket that has objects:

bash aws s3 rm s3://$B --recursive

  1. In CloudFormation, select cloudpath-saa-lab08c-evento-lambda and choose Delete.
  2. Delete the log group by hand in CloudWatch, Log groups, or with aws logs delete-log-group --log-group-name "<GrupoDeLogs>". That group wasn't created by the stack but by Lambda on the first invocation, so deleting the stack doesn't delete it.

On the exam

When the question asks to process each file as soon as it arrives in S3 and without servers, the answer is an S3 event notification to Lambda. If it also talks about "spikes of thousands of files" or about "not losing events if the function fails", an SQS queue goes in between, because the queue holds each notice until the function processes it. For example, if on a sale day the store's sellers upload thousands of photos in minutes, the queue lines them up, and if the function fails with a photo, the notice stays in the queue so it can be tried again.

Lambda detects recursive loops that go through S3 and stops them. Even so, the design answer the exam expects is to write the output to another bucket or another prefix.

Which architecture do you choose?

A company receives spikes of thousands of files in an S3 bucket and needs to process each one without servers and without losing events if the function fails. Which architecture do you choose?

Next

  • Tutorial: Using an Amazon S3 trigger to invoke a Lambda function: https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html
  • cloud path practice exams: /examenes?lang=en