SST Resize Images Example (S3)

  • SST
  • Lambda
  • S3
  • CloudWatch Logs
  • IAM Role
About 3 min

# SST Resize Images Example (S3)

Using SST to deploy your IaC is one of our favorite new ways of building serverless systems. For those of you not familiar with their tooling. They are a framework built upon the AWS CDK, to allow you to easily define your infrastructure as code, and then have it deploy it to AWS with CloudFormation. In this new series of docs, we are going to take what they built over there, and then show you how you can test it using Commandeer.

In this example we deploy an S3 Bucket that is connected to a Lambda that will take the image that is uploaded and then resize it. For help on getting this all deployed to your environment, you can follow along in their tutorial.

# Commandeer S3 Testing Tool

The S3 testing tool allows you to upload files to an S3 bucket that is connected to a lambda and view the invoked logs. You can access it under the testing tab for the account you have selected in Commandeer.

Account Dashboard

Below you can see the page that you will be taken to.

Commandeer S3 Testing Tool

Note that in this example, only files that are uploaded to the /photos location will be processed by the lambda, and they are then saved to the /resized-photos location. You do want to be careful with this setup, as writing to the same bucket from the lambda that is invoked on that bucket can cause an infinite loop, and you will pay for those charges. For a production environment, we recommend writing output to another S3 bucket that does not invoke a lambda.

# Add File Section

This first section you will notice is the Add File section, which allows you to set the prefix you want to upload to, which is like a folder structure in S3, and then add a file.

# Resources Section

Under this is the Resources section. This will give you a list of related AWS resources that are associated with this test. In our case, we have 4 resources.

  • CloudWatch Log - aws/lambda/dev-commandeer... - this is where all logging from your Lambda goes
  • Lambda - dev-commandeer-imageResizeHandler... - this is the lambda that is invoked when a file is saved to the /photos location of the bucket
  • IAM Execution Role - dev-commandeer-image-photoResize... - this is the IAM role that is assumed by the lambda. This is important, especially if the lambda is going to save data to another AWS resource like S3, DynamoDB, or SQS. It will need to be configured with the proper permissions. You normally do this in the SST code
  • S3 Bucket - dev-commandeer-image... - this is the bucket that the file is uploaded to.

Each of these can be clicked on to view the resource, and make any changes to it. For instance, clicking on the Lambda will bring up the Lambda detail on the right-hand side. In our source code, we have a nice way to log, that can be turned on with an environment variable of VERBOSE=true. So, if you click on the lambda, you will see this:

Lambda Environment Variables

And what's really nice, is you stay in context of the tester. So, clicking the lambda shows it like this on the same page.

Lambda Detail

# Results Section

On the right-hand side is where the results reside. Before you run it, you will see the brief explanation for the tool. Once you upload your file, this is where you will see the CloudWatch log information. This is also where the resources are shown. You can then simply close any resource, to return back to the results.

S3 Testing Results

After you press the run button, and upload the file, you will see the resulting CloudWatch log invocation like below. This allows you to view them in table or terminal style format. You can also search the logs, show the entire data object, and allow us to attempt to beautify the output. You can also easily copy or share this information with a teammate.

S3 Testing Results Invocation CloudWatch Logs

Below you can see the actual running of an upload of the image, and the resulting CloudWatch logs of the invocation.

Commandeer S3 Tester

Now, if we take a look at the bucket, we can see the images that have been uploaded, and then the newly resized images.

S3 Image Bucket

Clicking on one if the images, will then bring up the image in context.

Moonshot Image

# Conclusion

Combining the Commandeer testing tools this with the auto-refreshing of Lambda's that SST provides really makes for a magical testing experience.

Stay tuned over the next few weeks, as we begin to roll out more of these testing examples for SQS, DynamoDB Streams, SNS (Pub/Sub), EventBridge Rules (CRON) and more. You can download Commandeer here.

Last update: July 1, 2022 09:57