This pattern demonstrates how to create an Amazon API Gateway REST API with a direct proxy to an Amazon S3 bucket. The API allows files to be uploaded to the S3 bucket. An AWS Lambda authorizer will authorize the incoming HTTP requests. Asynchronously trigger a Lambda to process the uploaded file.
An Amazon API Gateway REST API is deployed, one AWS Lambda Authorizer, one Amazon S3 bucket, and one AWS Lambda that will be triggered after a file is uploaded.
The Amazon API Gateway is defined in the AWS Serverless Application Model (SAM) template.yaml
file using the OpenAPI specification.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-s3-lambda-dotnet
- Create an AWS account 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.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- .NET 8.0
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
-
Change directory to the pattern directory:
cd apigw-s3-lambda-dotnet
-
From the command line, use AWS SAM to build and deploy the AWS resources for the pattern as specified in the template.yaml file:
sam build sam deploy --guided
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region
- Allow SAM CLI to create IAM roles with the required permissions.
Once you have run
sam deploy -guided
mode and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the API Gateway endpoint URL,Lambda Authorizer, S3 bucket, the Lambda triggered from a file upload, and an SQS queue that's used to store S3 events that failed to be processed by the Lambda.
In order to test, follow the steps given below:
After deploying the template.yaml
file, copy the URL of the API Gateway endpoint that was created. Navigate to CloudFormation in the AWS Console. Click on the Stack that was created after deploying the template.yaml
file. Find the CloudFormation output value named: API
Submit an HTTP PUT request to API Gateway to upload a file to S3. The file will be stored in the S3 bucket and asynchronously trigger the Lambda function to process the file.
Below is an example HTTP PUT request to upload an image file to S3. Replace the URL with your API Gateway URL. Replace the key
query parameter to the name of the S3 bucket created after deploying the template.yaml
file.
curl -i --location --request PUT "https://<your-url>.execute-api.us-east-1.amazonaws.com/prod/s3?key=file-upload-bucket-111111111111>%2Funicorn1.jpg" --header "Authorization: mytesttoken" --header "Content-Type: image/jpeg" --data "C:/Temp/pictures/unicorn1.jpg"
Navigate to CloudFormation in the AWS Console. Click on the Stack that was created after deploying the template.yaml
file. Find the S3 bucket name by viewing the CloudFormation output value named Bucket
.
Navigate to S3 in the AWS Console to validate the new file has been uploaded.
Navigate to CloudFormation in the AWS Console. Click on the Stack that was created after deploying the template.yaml
file. Find the name of the Lambda function that will be invoked after a file is uploaded to S3 by viewing the CloudFormation output value named S3FileProcessorFunction
.
- Delete the stack
sam delete
- Confirm the stack has been deleted
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0