In this tutorial, you'll store the movies thumbnails in an S3 bucket and configure the service to serve the content from your bucket.
To store your data in Amazon S3, you first create a bucket and specify a bucket name and AWS Region. Then, you upload your data to that bucket as objects in Amazon S3. Each object has a key (or key name), which is the unique identifier for the object within the bucket.
- upload images from cli
- make it public static
- change netflix to this source test
In the below example, you create a bucket to store user analytics data for the NetflixFrontend app.
-
Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
-
In the left navigation pane, choose Buckets.
-
Choose Create bucket.
The Create bucket wizard opens.
-
In Bucket name, enter a DNS-compliant name for your bucket.
The bucket name must:
- Be unique across all of Amazon S3.
- Be between 3 and 63 characters long.
- Not contain uppercase characters.
- Start with a lowercase letter or number.
-
In Region, choose the AWS Region where you want the bucket to reside.
Choose the Region where you provisioned your EC2 instance.
-
Under Object Ownership, leave ACLs disabled. By default, ACLs are disabled. A majority of modern use cases in Amazon S3 no longer require the use of ACLs. We recommend that you keep ACLs disabled, except in unusual circumstances where you must control access for each object individually.
-
Enable Default encryption with
SSE-S3
encryption type. -
Choose Create bucket.
In the NetflixFrontend app, whenever a user opens the details window for a specific movie, an HTTP request is sent to the server containing the activity information. This activity if turn is stored as an object in an S3 bucket for later usage (for example by user analytics or recommendation teams).
You can review the code that upload object to S3 under pages/api/analytics.ts
in the NetflixFrontend repo..
In your EC2 instance where the NetflixFrontend container is running, re-run the container while providing the following environment variables to the container:
AWS_REGION
- Your region code (e.g.us-east-1
).AWS_S3_BUCKET
- The name of your bucket.
Visit the Netflix app, expand one of the movies information window, this activity should trigger an HTTP request to the app, which is turn will save the activity object in S3 bucket.
Disclaimer: This is not going to work. You should see an error like "Error updating user session" in the console logs of the NetflixFrontend logs. Since the identity who writes the data to the S3 bucket is your EC2 instance, it has to have permissions to operate in S3.
Keep reading....
To access an S3 bucket from an EC2 instance, you need to create an IAM role with the appropriate permissions and attach it to the EC2 instance. The role should have policies that grant the necessary permissions to read from and write to the S3 bucket, and the EC2 instance needs to be launched with this IAM role. IAM role will be taught soon. But for now, just follow the instructions below.
-
Open the IAM console at https://console.aws.amazon.com/iam/.
-
In the navigation pane, choose Roles, Create role.
-
On the Trusted entity type page, choose AWS service and the EC2 use case. Choose Next: Permissions.
-
On the Attach permissions policy page, search for AmazonS3FullAccess AWS managed policy.
-
On the Review page, enter a name for the role and choose Create role.
To replace an IAM role for an instance
-
In EC2 navigation pane, choose Instances.
-
Select the instance, choose Actions, Security, Modify IAM role.
-
Choose your created IAM role, click Save.
After assigning the role, check that app stores user activity in your S3 bucket.
What happen if you upload an object name that already exists?
You'll notice that the new object overrides the old one, without any option to restore the older version. If this happens unintentionally or due to a bug in the application code, it can result in the permanent loss of data.
The risk of data loss can be mitigated by implementing versioning in S3. When versioning is enabled, each object uploaded to S3 is assigned a unique version ID, which can be used to retrieve previous versions of the object. This allows you to recover data that was accidentally overwritten or deleted, and provides a safety net in case of data corruption or other issues.
-
Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
-
In the Buckets list, choose the name of the bucket that you want to enable versioning for.
-
Choose Properties.
-
Under Bucket Versioning, choose Edit.
-
Choose Enable, and then choose Save changes.
-
Upload multiple object with the same key, make sure versioning is working.
When versioning is enabled in S3, every time an object is overwritten or deleted, a new version of that object is created. Over time, this can lead to a large number of versions for a given object, many of which may no longer be needed for business or compliance reasons.
By creating lifecycle rules, you can define actions to automatically transition non-current versions of objects to a lower-cost storage class or delete them altogether. This can help you reduce storage costs and improve the efficiency of your S3 usage, while also ensuring that you are in compliance with data retention policies and regulations.
For example, you might create a lifecycle rule to transition all non-current versions of objects to Standard-IA
storage after 30 days, and then delete them after 365 days. This would allow you to retain current versions of objects in S3 for fast access, while still meeting your data retention requirements and reducing storage costs for non-current versions.
-
Choose the Management tab, and choose Create lifecycle rule.
-
In Lifecycle rule name, enter a name for your rule.
-
Choose the scope of the lifecycle rule (in this demo we will apply this lifecycle rule to all objects in the bucket).
-
Under Lifecycle rule actions, choose the actions that you want your lifecycle rule to perform:
- Transition noncurrent versions of objects between storage classes
- Permanently delete noncurrent versions of objects
-
Under Transition non-current versions of objects between storage classes:
-
In Storage class transitions, choose Standard-IA.
-
In Days after object becomes non-current, enter 30.
-
-
Under Permanently delete previous versions of objects, in Number of days after objects become previous versions, enter 90 days.
-
Choose Create rule.
If the rule does not contain any errors, Amazon S3 enables it, and you can see it on the Management tab under Lifecycle rules.
Explore the S3 pricing page.
Compute the monthly cost of the below bucket characteristics:
- us-east-1
- S3 Standard
- 4TB stored data
- 40 million PUT requests.
- 10 million GET requests.
- 5TB inbound traffic
- 10TB outbound traffic