Building a Resume Website with AWS Serverless Architecture
Sudarkodi Muthiah
Tech Project Leader | VoIP & SIP Specialist | Cloud Enthusiast | AWS Certified SAA | Cloud Security & AI Trailblazer | AI Hackathon winner
Introduction
In this blog, we will delve into the process of building a straightforward resume website using AWS serverless architecture. Throughout the tutorial, we will leverage a range of AWS services, including Amazon S3, Route53, CloudFront, AWS Certificate Manager, Lambda, and DynamoDB. Additionally, we will explore the use of GitHub Actions to facilitate smooth code changes and deployments.
Solution Architecture
Part 1 : Hosting a Resume website
To learn how to host a Static Resume Website with Amazon S3, CloudFront, and Route53, please refer to the article below:
This article provides detailed steps and instructions on setting up the hosting environment using Amazon S3, CloudFront, and Route53.
Part 2: Implementing a Visitor counter for the Resume website with AWS Lambda and DynamoDB
In our resume website implementation, we will incorporate a visitor counter feature. To achieve this, we will utilize a DynamoDB table to store and manage the visitor counter value. By leveraging the capabilities of DynamoDB, we can accurately track and display the number of visitors to our website.
Step 1: Create a DynamoDB table
To create a table in DynamoDB for storing the view counter data, follow these steps:
Once the table is created, proceed to the "Explore items" section. At this point, there will be no existing items in the table. Follow these steps to create a new item:
Step 2: Create a Lambda Function
To create a Lambda function that interacts with the DynamoDB table and increments the view counter, follow these steps:
In the advanced settings of the Lambda function, we will configure the following:
Step 3: Adding Permissions to Lambda Function
To grant the necessary permissions for the Lambda function to retrieve and update the viewer count in DynamoDB, follow these steps:
While in the configuration menu, it's important to configure the CORS (Cross-Origin Resource Sharing) "Allow-Origin" setting to restrict access to the Lambda function URL only from our specific domain name. This helps prevent unauthorized access from other domains and mitigates potential security risks.
Step 4: Adding Code to the Lambda Function
After creating the function and granting the necessary permissions, we will need to add the code that fetches the item from the DynamoDB table. This code will allow our Lambda function to retrieve the current view count.
The Sample code written in Python:
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Table Name')
def lambda_handler(event, context):
# TODO implement
response = table.get_item(Key={
'id':1
})
views = response['Item']['views']
views = views + 1
print(views)
response = table.put_item(Item={
'id':1,
'views':views
})
return views
The provided code snippet utilizes the Boto3 library to interact with DynamoDB and accomplish the following actions:
领英推荐
To test the functionality of your deployed Lambda function, you can use the curl command in your terminal. Follow these steps:
curl -X POST function_url
Part 3: Implementing the display of Visitor counter on the Resume Website
In this section, we will update the website code to incorporate the view count obtained from the API and display it on the website.
To display the visitor counter on your website, create a variable in the JavaScript file of your static website and assign to it the LambdaFunctionURL from the previous step and reference the JavaScript code within your index.html file.
const counter = document.querySelector(".counter-number");
async function updateCounter() {
let response = await fetch("Your-LambdaFunction-URL");
let data = await response.json();
counter.innerHTML = ` This page has ${data} Views!`;
}
updateCounter();
This JavaScript code retrieves the view count data from Lambda using the fetch function and updates the content of an HTML element with the class "counter-number" to display the count. The updateCounter function is responsible for displaying the view count, and it is automatically executed when the page loads.
Part 4: Implementing Source Control and CI/CD with GitHub Actions
In this section, we will set up a GitHub repository and configure GitHub Actions to automate the process of pushing changes to our S3 bucket whenever we commit changes to our website code. This Continuous Integration/Continuous Deployment (CI/CD) pipeline will streamline our development workflow by automatically updating our S3 bucket and reflecting the changes on our website. Follow these steps to achieve this:
Add the following code snippet to the “cicd.yml” file:
name: Upload website to S3
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: jakejarvis/s3-sync-action@master
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-east-1'#make sure the Region reflects yours
SOURCE_DIR: 'website' #make sure the SOURCE_DIR is the correct name
This GitHub Actions workflow is triggered whenever there is a push event on the “main” branch of the repository. It utilizes the “actions/checkout” action to fetch the latest code from the repository. The “jakejarvis/s3-sync-action” action is used to synchronize the files from the “website” directory to the S3 bucket.
GitHub Action to Sync S3 Bucket : https://github.com/jakejarvis/s3-sync-action?source=post_page-----b087ddef6b32--------------------------------
4. Add Environment Variables for GitHub Actions
To ensure successful execution of the actions, you need to define the required environment variables including your S3 bucket name, access key ID, and secret access key. Follow these steps to add the environment variables as secrets in your GitHub repository:
To test the successful execution of GitHub Actions, you can modifythe code html or JS code, save the changes, and utilize the Source Control button in VSCode to commit the modifications and synchronize them.
After synchronizing the changes, you can return to GitHub and verify the successful completion of the action, which results in the successful push of your changes to the S3 bucket. Follow these steps to verify the completion:
??Congratulations! We have succesfully deployed our Serverless Resume Webisite.??
By following the above steps and implementing the provided guidelines, you have successfully enhanced your resume website with a visitor counter, automated deployment using GitHub Actions, and secure management of environment variables. These enhancements not only improve the functionality and user experience of your website but also streamline your development workflow, allowing for seamless updates and efficient maintenance of your online resume.
Trainee Full-Stack Developer at Real IT Solutions Pune |Lean 6 Sigma White belt Certified|Scrum Foundation Certified|Double Star Ranger@Trailhead |OCI Certified |4X Alibaba Cloud Certified | Databricks 1x Certified
10 个月how can I become part of this cohort?