Working With Amazon s3
Working With Amazon s3
Working With Amazon s3
AKIA5426PU4CAGUNHSFZ
https://955291510532.signin.aws.amazon.com/console
///////////////////////////////////////////////////////////////////////////////////
//////////
Working with Amazon S3
Lab overview
In this lab, you create and configure an Amazon Simple Storage Service (Amazon S3)
bucket to share images with an external user at a media company (mediacouser) who
has been hired to provide pictures of the products that the café sells. You also
configure the S3 bucket to automatically send an email notification to the
administrator when the bucket contents are modified.
The following diagram shows the component architecture of the Amazon S3 file-
sharing solution and illustrates its usage flow.
architectural diagram
An AWS Identity and Access Management (IAM) user named mediacouser, which
represents an external user at a media company, has been pre-created with the
appropriate Amazon S3 permissions to allow the user to add, change, or delete
images from the bucket. The necessary Amazon S3 permissions are reviewed for each
user to make sure that access to the bucket is secure and appropriate for each
role.
As an alternative, mediacouser can use the AWS Command Line Interface (AWS CLI) to
change the contents of the S3 bucket.
Note: In real-world implementations, external users might not receive direct access
to CLI Host as depicted in the diagram.
Objectives
By the end of this lab, you will be able to do the following:
Use the s3api and s3 AWS CLI commands to create and configure an S3 bucket.
Duration
This lab requires approximately 90 minutes to complete.
Wait until the message "Lab status: ready" appears, and then choose X to close the
Start Lab panel.
At the top of these instructions, choose AWS to open the AWS Management Console on
a new browser tab. The system automatically signs you in.
Tip If a new browser tab does not open, a banner or icon at the top of your browser
will indicate that your browser is preventing the site from opening pop-up windows.
Choose the banner or icon, and choose Allow pop-ups.
Arrange the AWS Management Console so that it appears alongside these instructions.
Ideally, you should be able to see both browser tabs at the same time to follow the
lab steps.
At the top of these instructions, choose Details, and then choose Show.
From the Credentials panel, copy the values for the AccessKey and SecretKey, and
paste them into a text editor. You use these values throughout the lab. After you
have copied and pasted the values, choose X to close the Credentials panel.
Task 1: Connecting to the CLI Host EC2 instance and configuring the AWS CLI
In this task, you connect to the CLI Host EC2 instance by using EC2 Instance
Connect and configure the AWS CLI so that you can run commands.
On the AWS Management Console, in the Search bar, enter and choose EC2 to open the
EC2 Management Console.
Choose Connect.
This option opens a new browser tab with the EC2 Instance Connect terminal window.
You use this terminal window to complete the tasks throughout the lab. If the
terminal becomes unresponsive, refresh the browser or use the steps in this task to
connect again.
Task 1.2: Configuring the AWS CLI on the CLI Host instance
To set up the AWS CLI profile with credentials, run the following command in the
EC2 Instance Connect terminal:
aws configure
At the prompts, copy the following values that you pasted into your text editor,
and paste them into the terminal window as directed.
You are ready to run AWS CLI commands to interact with AWS services.
To do so, you run the following commands in the EC2 Instance Connect terminal
window.
To create an S3 bucket, run the following command. In the command, replace <cafe-
xxxnnn> with your bucket name. Your bucket name must begin with cafe- and should
include a combination of letters and numbers to make your bucket name unique:
Note: Bucket names cannot contain uppercase letters. If you receive an error when
you try to create your S3 bucket, make sure your bucket name doesn't include
uppercase letters.
Next, you load some images into the S3 bucket under the /images prefix. Sample
image files are provided in the initial-images folder on the CLI Host.
To load images into the bucket, run the following command. In the command, replace
<cafe-xxxnnn> with your bucket name:
To verify that the files were synced to the S3 bucket, run the following command.
In the command, replace <cafe-xxxnnn> with your bucket name:
On the AWS Management Console, in the Search bar, enter and choose IAM to open the
IAM Management Console.
If needed, review the AWS managed policy that permits users to change their own
password.
On the Permissions tab, you should see two policies: IAMUserChangePassword and
mediaCoPolicy. These policies are assigned to the mediaco IAM group that you
reviewed in the previous task.
To verify that you see the mediaco IAM group, choose the Groups tab.
The mediacouser user is a member of this group and therefore inherits the
permissions assigned to the mediaco group.
In the Access keys section, choose Create access key, and choose the following
options:
Select the check box for I understand the above recommendation and want to proceed
to create an access key.
Choose Next.
Choose Done.
On the mediacouser page, from the Security credentials tab, copy the Console sign-
in link.
To sign in to the AWS Management Console as the mediacouser user, use one of the
following options:
Important: Do not sign out of the session where you are signed in as the
voclabs/user. Instead, choose one of two options:
Option 2: Use the same browser type, but open a new incognito or private browser
session.
For either option that you choose, enter the Console sign-in link that you copied
from the previous step into your new browser tab. The AWS Management Console sign-
in page opens and already has the Account ID populated.
Password: Training1!.
On the new AWS Management Console page, in the Search bar, enter and choose S3 to
open the S3 Management Console.
From the list of buckets, select the bucket that you created earlier.
To display the list of images that were uploaded earlier, select images/.
To test the view use case, select Donuts.jpg, and choose Open.
A new browser tab should open that shows a picture of various donuts.
Tip: If a new browser tab does not open, a banner or icon at the top of your
browser will indicate that your browser is preventing the site from opening pop-up
windows. Choose the banner or icon, and choose Allow pop-ups.
In the Console tab, in the breadcrumb trail at the top, choose images/ to see the
contents of the images folder again.
To test the upload use case, choose Upload.
On the Upload page, choose Add files, and choose any image or picture from your
local computer.
Choose Upload.
A new browser tab should open that shows the file that you uploaded.
Close the browser tab that shows the file that you uploaded.
To test the delete use case, in the Console tab, in the image list, select the
check box for Cup-of-Hot-Chocolate.jpg.
Choose Delete.
On the Delete objects page, in the Delete objects? box, enter delete.
Next, you test the unauthorized use case where mediacouser attempts to change the
bucket's permissions.
In the breadcrumb trail at the top, choose your bucket to return to the bucket
content list.
Notice that for Permissions overview, the following error message is displayed:
"Insufficient permissions." mediacouser is prevented from changing the bucket
permissions. You could also try to upload a file directly to the root of the
bucket. This action should also fail.
You have successfully created an Amazon S3 bucket, and you have confirmed that it
is securely configured for file sharing with another user.
On the AWS Management Console, in the Search bar, enter SNS and choose Simple
Notification Service to open the Simple Notification Service console.
If necessary, to open the navigation pane, choose the menu icon () on the left.
Choose Standard.
A message is displayed indicating that the s3NotificationTopic SNS topic has been
successfully created.
From the s3NotificationTopic page in the Details section, copy and paste the ARN
value to a text editor. You need this value later in this lab.
Replace the contents of the JSON editor with the following policy. In the JSON
object, replace <ARN of s3NotificationTopic> with the ARM value that you copied
earlier, and replace <cafe-xxxnnn> with your S3 bucket name. Remember to remove the
enclosing angle brackets (< >).
{
"Version": "2008-10-17",
"Id": "S3PublishPolicy",
"Statement": [
{
"Sid": "AllowPublishFromS3",
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Action": "SNS:Publish",
"Resource": "<ARN of s3NotificationTopic>",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:s3:*:*:<cafe-xxxnnn>"
}
}
}
]
}
Take a moment to review the intent of this policy. It grants the cafe S3 share
bucket permission to publish messages to the s3NotificationTopic SNS topic.
Next, you subscribe to the topic to receive the event notifications from the S3
share bucket.
Choose the Topic ARN box, and choose the s3NotificationTopic SNS topic that appears
as an option.
In the Endpoint box, enter an email address that you can access.
A message displays that confirms that the subscription was created successfully.
Check the inbox for the email address that you provided. You should see an email
message with the subject AWS Notification - Subscription Confirmation.
Open the email message, and choose Confirm subscription. A new browser tab opens
and displays a page with the message Subscription confirmed!
In the terminal window for the CLI Host instance, enter the following command to
edit a new file named s3EventNotification.json:
vi s3EventNotification.json
In the editor, to change to insert mode, press i.
In the following JSON object, replace <ARN of s3NotificationTopic> with the ARN
value that you recorded earlier. Remember to remove the enclosing angle brackets (<
>). Copy and paste your customized JSON configuration into the editor window.
{
"TopicConfigurations": [
{
"TopicArn": "<ARN of s3NotificationTopic>",
"Events": ["s3:ObjectCreated:*","s3:ObjectRemoved:*"],
"Filter": {
"Key": {
"FilterRules": [
{
"Name": "prefix",
"Value": "images/"
}
]
}
}
}
]
}
Take a moment to review the intent of this configuration. It requests that Amazon
S3 publish an event notification to the s3NotificationTopic SNS topic whenever an
ObjectCreated or ObjectRemoved event is performed on objects inside an Amazon S3
resource with a prefix of images/.
To save the file and exit the editor, enter :wq and press Enter.
To associate the event configuration file with the S3 share bucket, run the
following command. In the command, replace <cafe-xxxnnn> with your S3 bucket name:
Open the email message, and examine the notification message. It should be similar
to the following:
{"Service":"Amazon S3","Event":"s3:TestEvent","Time":"2019-04-
26T06:04:27.405Z","Bucket":"","RequestId":"7A87C25E0323B2F4","HostId":"fB3Z...SD///
/PWubF3E7RYtVupg="}
Notice that the value of the "Event" key is "s3:TestEvent". Amazon S3 sent this
notification as a test of the event notifications configuration that you set up.
To configure the CLI Host's AWS CLI client software to use the mediacouser
credentials, in the SSH window for the CLI Host instance, enter the following
command:
aws configure
At the prompts, enter the following:
AWS Access Key ID: Copy and paste the value of the Access key ID of mediacouser,
which is in the mediacouser_accessKeys.csv file that you downloaded in Task 3.
AWS Secret Access Key: Copy and paste the value of the Secret Access Key of
mediacouser from the same file that you downloaded in Task 3.
Default region name: Press Enter at the prompt to keep the same Region that you
selected earlier in this lab.
Default output format: Enter json.
Next, you test the put use case by uploading the Caramel-Delight.jpg image file
from the new-images folder on the CLI Host.
To upload this file, run the following command. In the command, replace <cafe-
xxxnnn> with your S3 bucket name:
Check the inbox for the email address that you used to subscribe to the
s3NotificationTopic SNS topic. You should see a new email message with the subject
Amazon S3 Notification.
Open the email message, and examine the notification message. Notice the following
information:
The value of the key object is images/Caramel-Delight.jpg, which is the image file
key that you specified in the command.
Next, you test the get use case by getting the object with a key of
images/Donuts.jpg from the bucket.
To get this object, run the following command. In the command, replace <cafe-
xxxnnn> with your S3 bucket name:
Next, you test the delete use case by deleting the object with a key of
images/Strawberry-Tarts.jpg from the bucket.
To delete this object, run the following command. In the command, replace <cafe-
xxxnnn> with your S3 bucket name:
Open the email message, and examine the notification message. Notice the following
information:
The value of the object key is images/Strawberry-Tarts.jpg, which is the image file
key that you specified in the command.
This notification indicates that the object with a key of images/Strawberry-
Tarts.jpg was deleted from the S3 share bucket.
To try to change the permission of the Donuts.jpg object so that it can be read
publicly, run the following command. In the command, replace <cafe-xxxnnn> with
your S3 bucket name:
Conclusion
Congratulations! You now have successfully done the following:
Used the s3api and s3 AWS CLI commands to create and configure an S3 bucket
Lab complete
Congratulations! You have completed the lab.
At the top of this page, choose End Lab and then choose Yes to confirm that you
want to end the lab.
A panel appears indicating that "You may close this message box now. Lab resources
are terminating."
To close the End Lab panel, choose the X in the upper-right corne