|
| 1 | +# S3 Event Notifier |
| 2 | + |
| 3 | +This example demonstrates how to write a Lambda that is invoked by an event originating from Amazon S3, such as a new object being uploaded to a bucket. |
| 4 | + |
| 5 | +## Code |
| 6 | + |
| 7 | +In this example the Lambda function receives an `S3Event` object defined in the `AWSLambdaEvents` library as input object. The `S3Event` object contains all the information about the S3 event that triggered the function, but what we are interested in is the bucket name and the object key, which are inside of a notification `Record`. The object contains an array of records, however since the Lambda function is triggered by a single event, we can safely assume that there is only one record in the array: the first one. Inside of this record, we can find the bucket name and the object key: |
| 8 | + |
| 9 | +```swift |
| 10 | +guard let s3NotificationRecord = event.records.first else { |
| 11 | + throw LambdaError.noNotificationRecord |
| 12 | +} |
| 13 | + |
| 14 | +let bucket = s3NotificationRecord.s3.bucket.name |
| 15 | +let key = s3NotificationRecord.s3.object.key.replacingOccurrences(of: "+", with: " ") |
| 16 | +``` |
| 17 | + |
| 18 | +The key is URL encoded, so we replace the `+` with a space. |
| 19 | + |
| 20 | +## Build & Package |
| 21 | + |
| 22 | +To build & archive the package you can use the following commands: |
| 23 | + |
| 24 | +```bash |
| 25 | +swift build |
| 26 | +swift package archive --allow-network-connections docker |
| 27 | +``` |
| 28 | + |
| 29 | +If there are no errors, a ZIP file should be ready to deploy, located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/S3EventNotifier/S3EventNotifier.zip`. |
| 30 | + |
| 31 | +## Deploy |
| 32 | + |
| 33 | +> [!IMPORTANT] |
| 34 | +> The Lambda function and the S3 bucket must be located in the same AWS Region. In the code below, we use `eu-west-1` (Ireland). |
| 35 | +
|
| 36 | +To deploy the Lambda function, you can use the `aws` command line: |
| 37 | + |
| 38 | +```bash |
| 39 | +REGION=eu-west-1 |
| 40 | +aws lambda create-function \ |
| 41 | + --region "${REGION}" \ |
| 42 | + --function-name S3EventNotifier \ |
| 43 | + --zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/S3EventNotifier/S3EventNotifier.zip \ |
| 44 | + --runtime provided.al2 \ |
| 45 | + --handler provided \ |
| 46 | + --architectures arm64 \ |
| 47 | + --role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/lambda_basic_execution |
| 48 | +``` |
| 49 | + |
| 50 | +The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`. |
| 51 | + |
| 52 | +Be sure to define `REGION` with the region where you want to deploy your Lambda function and replace `<YOUR_ACCOUNT_ID>` with your actual AWS account ID (for example: 012345678901). |
| 53 | + |
| 54 | +Besides deploying the Lambda function you also need to create the S3 bucket and configure it to send events to the Lambda function. You can do this using the following commands: |
| 55 | + |
| 56 | +```bash |
| 57 | +REGION=eu-west-1 |
| 58 | + |
| 59 | +aws s3api create-bucket \ |
| 60 | + --region "${REGION}" \ |
| 61 | + --bucket my-test-bucket \ |
| 62 | + --create-bucket-configuration LocationConstraint="${REGION}" |
| 63 | + |
| 64 | +aws lambda add-permission \ |
| 65 | + --region "${REGION}" \ |
| 66 | + --function-name S3EventNotifier \ |
| 67 | + --statement-id S3InvokeFunction \ |
| 68 | + --action lambda:InvokeFunction \ |
| 69 | + --principal s3.amazonaws.com \ |
| 70 | + --source-arn arn:aws:s3:::my-test-bucket |
| 71 | + |
| 72 | +aws s3api put-bucket-notification-configuration \ |
| 73 | + --region "${REGION}" \ |
| 74 | + --bucket my-test-bucket \ |
| 75 | + --notification-configuration '{ |
| 76 | + "LambdaFunctionConfigurations": [{ |
| 77 | + "LambdaFunctionArn": "arn:aws:lambda:${REGION}:<YOUR_ACCOUNT_ID>:function:S3EventNotifier", |
| 78 | + "Events": ["s3:ObjectCreated:*"] |
| 79 | + }] |
| 80 | + }' |
| 81 | + |
| 82 | +touch testfile.txt && aws s3 cp testfile.txt s3://my-test-bucket/ |
| 83 | +``` |
| 84 | + |
| 85 | +This will: |
| 86 | + - create a bucket named `my-test-bucket` in the `$REGION` region; |
| 87 | + - add a permission to the Lambda function to be invoked by Amazon S3; |
| 88 | + - configure the bucket to send `s3:ObjectCreated:*` events to the Lambda function named `S3EventNotifier`; |
| 89 | + - upload a file named `testfile.txt` to the bucket. |
| 90 | + |
| 91 | +Replace `my-test-bucket` with your bucket name (bucket names are unique globaly and this one is already taken). Also replace `REGION` environment variable with the AWS Region where you deployed the Lambda function and `<YOUR_ACCOUNT_ID>` with your actual AWS account ID. |
| 92 | + |
| 93 | +> [!IMPORTANT] |
| 94 | +> The Lambda function and the S3 bucket must be located in the same AWS Region. Adjust the code above according to your closest AWS Region. |
0 commit comments