Streamlining Static Website Deployment with GitHub Actions and S3

Prachi JukariaPrachi Jukaria
4 min read

In this exciting project, we'll be showcasing the seamless hosting of a static website on AWS S3, utilizing the robust capabilities of GitHub Actions as our CI/CD tool. GitHub Actions serves as a comprehensive continuous integration and continuous delivery (CI/CD) platform, empowering you to automate the entire build, test, and deployment pipeline. Through the creation of workflows, we'll demonstrate how to automate processes such as building and testing each pull request and deploying merged pull requests to production.

Tasks:

  1. Creating a S3 Bucket

  2. Creating an IAM role

  3. Writing the GitHub action workflow

Step 1: Creating a S3 bucket

  1. Open the AWS console page and search for the S3 service

  2. Click on Create Bucket to create a new bucket for your static website hosting

  3. Give an appropriate name to your bucket

  4. Unblock all public access

  5. Scroll down and click on Create Bucket.

  6. Once the bucket is created you have to modify it for website hosting. for this open your s3 bucket and move to the properties tab.

  7. scroll down the Static Website hosting section and click on the edit tab

  8. Click on the enable button and then you have to specify a file as an index document and another file as an error document.

  9. Scroll down and click on Save Changes.

  10. Now move to the permissions tab and click on edit under the bucket policy section.

  11. Now add the policy given below for your bucket and click on save changes.

    {
        "Version": "2012-10-17",
        "Id": "Policy1702560685545",
        "Statement": [
            {
                "Sid": "Stmt1702560680458",
                "Effect": "Allow",
                "Principal": "*",
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::static-file-hosting-github-actions/*"
            }
        ]
    }
    

Step 2: Create an IAM role

  1. Search for IAM service in the AWS console.

  2. click on users from the left panel of the IAM dashboard.

  3. Click on Create User.

  4. Give a name to the user and click on next.

  5. Now attach these policies to your user and click on next.

  6. Now review your user and click on Create User.

  7. Your user has been created. Now click on the user.

  8. Under the Security Credentials Tab, scroll down to the Access Keys section and then click on Create Access Key.

  9. Select the use case and then click on next then on create access key.

  10. Download the .csv file to store your access key and secret access key.

Step 3: Writing the GitHub action workflow

  1. Open the GitHub repository, where you have the code for your static website.
    Note: In case you need the code for a static website, you can use the link below.

     https://github.com/PrachiJukaria/staticWebisiteS3.git
    
  2. Firstly, we have to set secrets for our access key and secret access key. Click on the settings tab in your repository

  3. Now click on Secrets and Variables and then actions from the left panels.

  4. Now click on the new repository secret and add your access key and secret access key.

  5. Now we will write a GitHub action workflow. For this move to the action tab in your repository and click on set up a workflow yourself.

  6. Now start writing the GitHub action workflow.

     name: static website hosting
    
     on:
       push:
         branches:
         - master
    
     jobs:
       deploy:
         runs-on: ubuntu-latest
         steps:
         - name: checkout
           uses: actions/checkout@v4.1.1
    
         - name: setting up AWS credentials
           uses: aws-actions/configure-aws-credentials@v4
           with:
             aws-region: us-east-1
             aws-access-key-id: ${{ secrets.ACCESS_KEY }}
             aws-secret-access-key: ${{ secrets.SECRET_KEY }}
    
         - name: copying the code to S3 bucket
           run: aws s3 sync . s3://BUCKET_NAME --delete
    
  7. Once the workflow is written and built successfully, push any changes into your repository. After pushing the changes you can switch to the action tab in your repository.

    You can see that the pipeline ran successfully.

  8. To see the website, move to your S3 bucket. In the properties tab, under the static website hosting section, you can see the link for your website.

  9. Access the link, you will see that the website is live.

Conclusion

In conclusion, this project successfully demonstrated the seamless deployment of a static website using AWS S3 bucket, complemented by an efficient CI/CD workflow powered by GitHub Actions. The incorporation of IAM roles ensured secure management of secret keys, while the implementation of a meticulous bucket policy enhanced S3 bucket security. By seamlessly integrating these technologies, this project not only achieved a robust hosting solution but also exemplified best practices in cloud infrastructure management and continuous integration for web development projects.

0
Subscribe to my newsletter

Read articles from Prachi Jukaria directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prachi Jukaria
Prachi Jukaria