📖 Scenario: You are setting up a static website hosted on an AWS S3 bucket. To allow your website to request resources from this bucket securely, you need to configure Cross-Origin Resource Sharing (CORS) rules.
🎯 Goal: Configure CORS rules on an AWS S3 bucket to allow GET requests from a specific origin.
📋 What You'll Learn
Create a CORS configuration JSON with one rule
Allow GET method only
Allow origin 'https://example.com'
Allow all headers
Set max age to 3000 seconds
💡 Why This Matters
🌍 Real World
Configuring CORS is essential when hosting static websites or APIs on AWS S3 to control which websites can access your resources.
💼 Career
Cloud engineers and DevOps professionals often configure CORS to secure web applications and enable cross-origin requests safely.
Progress0 / 4 steps
1
Create the initial CORS configuration structure
Create a variable called cors_configuration and assign it a dictionary with a key CORSRules that holds an empty list.
AWS
Hint
Start by creating a dictionary with the key 'CORSRules' set to an empty list.
2
Add a CORS rule dictionary
Add a dictionary inside the CORSRules list in cors_configuration. This dictionary should have keys AllowedMethods, AllowedOrigins, AllowedHeaders, and MaxAgeSeconds with empty or zero values.
AWS
Hint
Add a dictionary with the required keys inside the 'CORSRules' list.
3
Configure the CORS rule with specific values
Set the AllowedMethods to a list containing "GET", AllowedOrigins to a list containing "https://example.com", AllowedHeaders to a list containing "*", and MaxAgeSeconds to 3000 inside the first dictionary in CORSRules.
AWS
Hint
Fill in the lists and number with the exact values to allow GET from the example.com origin.
4
Complete the AWS CLI command to apply the CORS configuration
Write the AWS CLI command string called apply_cors_command that uses aws s3api put-bucket-cors with the bucket name my-static-site-bucket and the CORS configuration file cors.json.
AWS
Hint
Write the exact AWS CLI command string to apply the CORS configuration from the file.
Practice
(1/5)
1. What does CORS stand for and why is it important in AWS S3 bucket configuration?
easy
A. Cross-Origin Resource Sharing; it controls access to resources from different websites.
B. Cloud-Origin Resource Security; it encrypts data in S3 buckets.
C. Cross-Object Resource Sync; it syncs files between buckets.
D. Cloud-Origin Resource Sharing; it manages AWS user permissions.
Solution
Step 1: Understand the acronym CORS
CORS stands for Cross-Origin Resource Sharing, a web security feature.
Step 2: Identify its role in AWS S3
CORS controls which websites can access resources in an S3 bucket, preventing unauthorized cross-site requests.
Final Answer:
Cross-Origin Resource Sharing; it controls access to resources from different websites. -> Option A
Quick Check:
CORS = Cross-Origin Resource Sharing [OK]
Hint: Remember CORS controls cross-site resource access [OK]
Common Mistakes:
Confusing CORS with encryption or syncing
Thinking CORS manages AWS user permissions
Assuming CORS is only for AWS internal use
2. Which of the following is the correct JSON snippet to allow GET and POST methods from any origin in an S3 bucket CORS configuration?
easy
A. { "CORSRules": [{ "AllowedOrigins": ["http://example.com"], "AllowedMethods": ["PUT"] }] }
B. { "CORSRules": [{ "AllowedOrigins": ["*"], "AllowedMethods": ["DELETE"] }] }
B. AllowedOrigins should be '*', not a specific domain.
C. AllowedHeaders cannot be '*'.
D. CORSRules must include MaxAgeSeconds.
Solution
Step 1: Review AllowedMethods for PUT requests
PUT is not listed in AllowedMethods, only GET and POST are allowed.
Step 2: Understand CORS behavior for unsupported methods
Requests with methods not allowed by CORS are blocked by browsers.
Final Answer:
PUT method is not included in AllowedMethods. -> Option A
Quick Check:
AllowedMethods missing PUT causes CORS error [OK]
Hint: Check AllowedMethods includes your HTTP method [OK]
Common Mistakes:
Assuming AllowedOrigins must be '*' for CORS to work
Thinking AllowedHeaders '*' is invalid
Believing MaxAgeSeconds is required
5. You want to configure an S3 bucket to allow only GET and OPTIONS requests from two specific domains: https://site1.com and https://site2.com. You also want browsers to cache the CORS response for 1 hour. Which is the correct CORS configuration JSON?