|
| 1 | +# AWS Amazon S3 to AWS Lambda - Create a Lambda function that resizes images uploaded to S3 |
| 2 | + |
| 3 | +The SAM template deploys a Lambda function, an S3 bucket and the IAM resources required to run the application. A Lambda function consumes <code>ObjectCreated</code> events from an Amazon S3 bucket. The Lambda code checks the uploaded file is an image and creates a thumbnail version of the image in the same bucket. |
| 4 | + |
| 5 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 11 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 12 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 13 | + |
| 14 | +## Deployment Instructions |
| 15 | + |
| 16 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 17 | + ``` |
| 18 | + git clone https://github.com/aws-samples/serverless-patterns |
| 19 | + ``` |
| 20 | +1. Change directory to the pattern directory: |
| 21 | + ``` |
| 22 | + cd s3-lambda |
| 23 | + ``` |
| 24 | +1. From the command line, use AWS SAM to build and deploy the AWS resources for the pattern as specified in the template.yml file: |
| 25 | + ``` |
| 26 | + sam build |
| 27 | + sam deploy --guided |
| 28 | + ``` |
| 29 | +1. During the prompts: |
| 30 | + * Enter a stack name |
| 31 | + * Enter the desired AWS Region |
| 32 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 33 | +
|
| 34 | + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. |
| 35 | +
|
| 36 | +1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 37 | +
|
| 38 | +## How it works |
| 39 | +
|
| 40 | +* Use the AWS CLI upload an image to S3 |
| 41 | +* If the object is a .jpg or a .png, the code creates a thumbnail and saves it to the target bucket. |
| 42 | +* The code assumes that the destination bucket exists and its name is a concatenation of the source bucket name followed by the string -resized |
| 43 | +
|
| 44 | +============================================== |
| 45 | +
|
| 46 | +## Testing |
| 47 | +
|
| 48 | +Run the following S3 CLI command to upload an image to the S3 bucket. Note, you must edit the {SourceBucketName} placeholder with the name of the S3 Bucket. This is provided in the stack outputs. |
| 49 | +
|
| 50 | +```bash |
| 51 | +aws s3 cp './events/example.jpg' s3://{SourceBucketName} |
| 52 | +``` |
| 53 | + |
| 54 | +Run the following command to check that a new version of the image has been created in the destination bucket. |
| 55 | + |
| 56 | +```bash |
| 57 | +aws s3 ls s3://{DestinationBucketName} |
| 58 | +``` |
| 59 | + |
| 60 | +## Cleanup |
| 61 | + |
| 62 | +1. Delete the stack |
| 63 | + ```bash |
| 64 | + aws cloudformation delete-stack --stack-name STACK_NAME |
| 65 | + ``` |
| 66 | +1. Confirm the stack has been deleted |
| 67 | + ```bash |
| 68 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" |
| 69 | + ``` |
| 70 | +---- |
| 71 | +Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 72 | + |
| 73 | +SPDX-License-Identifier: MIT-0 |
0 commit comments