Home

Work with Amazon S3

Overview

In my work with Amazon S3 here, I created and configured an Amazon S3 bucket to share images with an external user at a media company (mediacouser). This user was hired to provide pictures of products that the café sells. I also configured the S3 bucket to automatically send email notifications to the administrator when the bucket contents were modified.

System Architecture

The usage flow I implemented worked like this:

My Objectives

By the end of this work, I successfully:

Connecting to the CLI Host EC2 instance and configuring the AWS CLI

Connecting to the CLI Host EC2 instance

First, I connected to the CLI Host EC2 instance using EC2 Instance Connect:

I used this terminal window to complete all tasks throughout. When the terminal became unresponsive, I refreshed the browser or reconnected using these steps.

Configuring the AWS CLI on the CLI Host instance

To set up the AWS CLI profile with credentials, I ran the following command in the EC2 Instance Connect terminal:

aws configure

At the prompts, I entered the following values:

With this setup, I was ready to run AWS CLI commands to interact with AWS services.

Creating and initializing the S3 share bucket

In this task, I used the AWS CLI to create the S3 share bucket and upload some images.

To create the S3 bucket, I ran:

aws s3 mb s3://cafe-xxxnnn --region 'us-west-2'

I replaced <cafe-xxxnnn> with my unique bucket name that began with cafe- and included a combination of letters and numbers.

I received a message similar to: make_bucket: cafe-xxxx9999999

Bucket names cannot contain uppercase letters. I made sure my bucket name didn't include uppercase letters to avoid errors.

Next, I loaded some images into the S3 bucket under the /images prefix. Sample image files were provided in the initial-images folder on the CLI Host:

aws s3 sync ~/initial-images/ s3://cafe-xxxnnn/images

The command output listed all the image files being uploaded.

To verify that the files were synced to the S3 bucket, I ran:

aws s3 ls s3://cafe-xxxnnn/images/ --human-readable --summarize

I could see the details of the uploaded image files, including the number of files uploaded and their total size.

Reviewing the IAM group and user permissions

Next, I reviewed the permissions assigned to the mediaco IAM user group. This group was created to provide a way for media company users to use the AWS Management Console or AWS CLI to upload and modify images in the S3 share bucket.

Reviewing the mediaco IAM group

To review the permissions assigned to the mediaco group:

I noticed the following statements in this policy:

Reviewing the mediacouser IAM user

To review the properties of the mediacouser user:

I created an access key for the mediacouser:

Testing the mediacouser permissions

I tested the permissions by signing in to AWS Management Console as mediacouser and performing view, upload, and delete operations on the contents of the images folder in the S3 share bucket.

To test viewing:

To test uploading:

To test deleting:

To test unauthorized operations:

Configuring event notifications on the S3 share bucket

In this task, I configured the S3 share bucket to generate event notifications to an SNS topic whenever the bucket contents changed.

Creating and configuring the s3NotificationTopic SNS topic

I returned to the AWS Management Console where I was signed in as voclabs/user.

To configure the topic's access policy, I clicked Edit and expanded the Access policy section. I replaced the contents of the JSON editor with the following policy:

{ "Version": "2008-10-17", "Id": "S3PublishPolicy", "Statement": [ { "Sid": "AllowPublishFromS3", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": "SNS:Publish", "Resource": "", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:" } } } ] }

This policy granted my cafe S3 share bucket permission to publish messages to the s3NotificationTopic SNS topic.

Email Subscription Setup

Adding an event notification configuration to the S3 bucket

I created an event notification configuration file identifying events for Amazon S3 to publish and the topic destination for notifications:

vi s3EventNotification.json

I customized and pasted this JSON configuration:

{ "TopicConfigurations": [ { "TopicArn": "", "Events": ["s3:ObjectCreated:*","s3:ObjectRemoved:*"], "Filter": { "Key": { "FilterRules": [ { "Name": "prefix", "Value": "images/" } ] } } } ] }

This configuration requested Amazon S3 to publish event notifications to my SNS topic whenever an ObjectCreated or ObjectRemoved event occurred on objects with a prefix of images/.

To associate the configuration with my bucket, I ran:

aws s3api put-bucket-notification-configuration --bucket --notification-configuration file://s3EventNotification.json

Testing the S3 share bucket event notifications

In this final task, I tested the S3 bucket event notification by performing the expected mediacouser operations which triggered email notifications.

Configuring the AWS CLI for mediacouser

aws configure

At the prompts, I entered the mediacouser credentials:

Testing Operations and Notifications

To test the put operation, I uploaded an image:

aws s3api put-object --bucket --key images/Caramel-Delight.jpg --body ~/new-images/Caramel-Delight.jpg

I checked my email inbox and found a notification with:

Next, I tested the get operation:

aws s3api get-object --bucket --key images/Donuts.jpg Donuts.jpg

I noticed no email notification was generated for this operation, which was expected since my bucket was configured for notifications only on object creation or deletion.

Then I tested the delete operation:

aws s3api delete-object --bucket --key images/Strawberry-Tarts.jpg

I received another notification message with:

Finally, I tested an unauthorized operation:

aws s3api put-object-acl --bucket --key images/Donuts.jpg --acl public-read

The command failed with this error message: "An error occurred (AccessDenied) when calling the PutObjectAcl operation: Access Denied"

Summary

Related Topics