Welcome! This guide will take you through the process of putting your website on the internet step-by-step.
The Strategy: To make everything work smoothly, we need to do things in a specific order:
- Database & Backend (so your form has somewhere to send data).
- Update Code (connect your website to the backend).
- S3 Hosting (put your website on the internet).
Before hosting the visual part (S3), we need to make sure the form works.
This is where user details will be saved.
- Log in to the AWS Console.
- Search for DynamoDB and click Create table.
- Table name:
UserDetails - Partition key: Type
id(and select String). - Leave everything else as default and click Create table.
This is the code that catches the form data.
- Search for Lambda and click Create function.
- Select Author from scratch.
- Function name:
SubmitFormFunction - Runtime:
Node.js 20.x(or latest). - Click Create function.
- The Code:
- Scroll down to the Code source section.
- Open
backend/lambdafunc.json your computer. - Copy the entire content.
- Paste it into the Lambda code editor (replace existing code).
- Click Deploy.
- Permissions (Crucial Step!):
- Go to the Configuration tab -> Permissions.
- Click the Role name (it looks like
SubmitFormFunction-role-xxxxx). - In the new tab (IAM), click Add permissions -> Attach policies.
- Search for
AmazonDynamoDBFullAccess(easier for beginners) and check it. - Click Add permissions.
This gives your function a URL web address.
- Search for API Gateway.
- Click Create API -> REST API -> Build.
- API Name:
FormAPI-> Create API. - Create Resource:
- Click Actions -> Create Resource.
- Resource Name:
submit. - Click Create Resource.
- Create Method:
- With
/submitselected, Click Actions -> Create Method. - Select POST -> Click Checkmark.
- Integration type: Lambda Function.
- Lambda Function:
SubmitFormFunction. - Click Save.
- With
- Enable CORS (Crucial for Web Browser Access):
- Why? Web browsers block websites from sending data to a different website (your API) for security. CORS tells the browser "It's okay, let this website talk to me."
- Select the Resource: Click on the
/submitresource (the folder icon, not the POST method itself). - Find the Button: Click the Actions dropdown button (usually at the very top of the list of resources/methods).
- Select Option: Choose Enable CORS from the list.
- Settings: A new screen appears.
- leave "Gateway Responses" checked.
- "Access-Control-Allow-Origin" should say
'*'(this means ANY website can send data).
- Confirm: Click the blue button Enable CORS and replace existing CORS headers.
- Final Pop-up: A confirmation box will appear asking "Are you sure?". Click Yes, replace existing values. You will see a bunch of green checkmarks appear.
- Deploy:
- Click Actions -> Deploy API.
- Stage name:
prod. - Click Deploy.
- Copy the URL:
- Look for Invoke URL at the top.
- It looks like:
https://xyz123.execute-api.us-east-1.amazonaws.com/prod - Write this down!
Now we connect your local code to the live AWS backend.
- Open the file
frontend/script.json your computer. - Find line 6:
const API_URL = 'YOUR_API_GATEWAY_URL_HERE'; - Replace
'YOUR_API_GATEWAY_URL_HERE'with the Invoke URL you just copied from API Gateway.- Important: Add
/submitto the end of the URL. - Example:
const API_URL = 'https://xyz.amazonaws.com/prod/submit';
- Important: Add
- Save the file.
Now that the code works, let's put it on the internet using an S3 Bucket.
This guide explains how to host a static portfolio website on AWS S3 and distribute it via CloudFront for enhanced performance and security.
- Go to the S3 console and create a new bucket.
- Configure the following settings:
- Enable ACL
- Disable Block Public Access
- Enable Versioning
- Upload your portfolio files and folders to the S3 bucket.
- Set permissions to public so that the content is accessible over the web.
- Go to the Properties tab of your S3 bucket.
- Enable Static Website Hosting.
- Set the index document to
index.html.
- Go to the CloudFront console and create a new distribution.
- Configure the distribution settings:
- Name: Give a descriptive name
- Website/App Type: Single website or application
- Origin Domain: Select your S3 bucket web endpoint
- Origin Settings: Choose the S3 bucket created in Step 1
- Redirect HTTP to HTTPS for secure access
- Enable WAF Security Protections for additional security
- Use domain as shown in ACM for access
- Add Subdomain for access. Ex: www
- Default Root Object:
index.html
- Ensure that your S3 bucket policy allows public read access.
- Use AWS Certificate Manager (ACM) to attach SSL certificates for HTTPS.
- Versioning on the bucket helps manage updates and rollback if needed.
You're Done! 🎉 You have built and deployed a full serverless application.