The Knowledge Seeker

Host your Jekyll Blog with AWS CDK

Are you interested in creating a blog? Jekyll is a static site generator that makes it easy to create a blog and create blog posts using markdown language. Once you create your blog, AWS is a great option for hosting it. AWS is the leading cloud provider in the market today and has all the tools you’d need to host your blog and more. We discussed options for creating a blog in an earlier post; you should check it out https://blog.cleanslatetg.cloud/start-a-blog/.

There are several ways that infrastructure can be deployed to AWS, from creating things by hand in the AWS console to using the AWS SDK for your programming language of choice AWS provides multiple options for creating resources. One great option that is a hybrid of IaC and programming is AWS CDK. CDK is an IaC tool that allows users to create and manage infrastructure stacks easily and to define resources using their preferred programming language. We discussed CDK in depth in a previous post. If you’d like to know more about CDK, you should check out the About AWS CDK post: https://blog.cleanslatetg.cloud/about-aws-cdk/. For this example, we will create our Jekyll static site in AWS using CDK with Python.

Tech Stack

Source Control

Blog Infrastructure

CI/CD

Creating your Blog

Prerequisites

Steps

  1. Create a private GitHub repository for your blog
  2. Clone the repo https://github.com/cleanslate-technology-group/indyaws-cdk-python-jekyll-blog into your repo
  3. Find a Jekyll theme you’d like to use (if you don’t want to use the theme provided in the cloned repo)
  4. (Optional) - In the blog folder, delete all of the files and replace them with the desired Jekyll template files
  5. In the infrastructure folder, activate the Python virtual environment

     # Mac or Linux 
     source .venv/bin/activate
        
     # Windows
     # .venv\Scripts\activate.bat
    
  6. Install the requirements in the requirements.txt file

     pip install -r requirements.txt
    
  7. Bootstrap the AWS Account & Region

     cdk bootstrap --profile <profileName>
    
  8. At the root of your repository, create .env file. This file will be used to provide environment variables to the CDK script

     touch .env
    
  9. Populate the .env file with the following values

     AWS_ACCOUNT_NUMBER=<AWS_ACCOUNT_NUMBER>
     AWS_REGION=<AWS_REGION>
     DOMAIN_NAME=<DOMAIN_NAME>
     BASE_DOMAIN=<BASE_DOMAIN>
     REPO_OWNER=<REPO_OWNER>
     REPO_NAME=<REPO_NAME>
     REPO_BRANCH=<REPO_BRANCH>
    
    • AWS_ACCOUNT_NUMBER - This is the account number of the AWS account used to host the blog. This will be used to create an environment in CDK
    • AWS_REGION - The region in the AWS account that the blog will be deployed to. This will be used to create an environment in CDK
    • DOMAIN_NAME - This is the domain name that you want your blog to have (excluding www. The www record will be added automatically). Example: blog.supercoolsite.com
    • BASE_DOMAIN - This is the root domain you purchased on Route 53. This should not contain any subdomains. If you are not using subdomains for your blog, then the value of the DOMAIN_NAME and BASE_DOMAIN will be the same. Example: supercoolsite.com
    • REPO_OWNER - Name of the owner of your repository. It will either be your GitHub username if you are hosting the repo from your GitHub user or will be the name of the GitHub organization that owns the repository.
    • REPO_NAME - Name of your repository
    • REPO_BRANCH - Branch that will be used by CI/CD to detect commits and deploy from. I would recommend using the main branch.
  10. Commit and publish the changes to your repository up to your remote main branch
  11. Deploy the CDK stack using the deploy command

    cdk deploy --profile <profileName>
    
  12. After the deployment has finished, login to your AWS accounts management console, then select the region that you deployed the blog into
  13. Using the search bar at the top of the management console, search for and select “CodePipeline”
  14. Once in the CodePipline service, you will see the pipeline that was created by CDK for the blog. This is where you can see the progress of your blog deployments in the future. The first run of the CodePipeline will fail because you have to finish configuring the link between your AWS account and GitHub.
  15. In the left-hand menu, under CodePipeline, select Setting —> Connections
  16. On the connections, click on the pending connection named jekyll-static-site
  17. Once on the jekyll-static-site connection page, click the button to finish setting up the connection. Walk through the prompts to complete the setup.
  18. After the connection setup has been completed, you can return to CodePipeline. Select the pipeline for the blog and press the Release Changes button to force a new deployment.
  19. After the deployment has been completed, you should be able to see your blog at the domain name you provided in the .env file.
  20. Your blog is now published. You can post new articles to your blog by following the steps specified by your Jekyll theme. Once your new article is ready to be published, you can automatically deploy it by simply checking in your changes to the branch specified for the REPO_BRANCH variable in the .env file.

Happy Blogging!

read more

What is AWS CDK?

AWS Cloud Development Kit (CDK) is an infrastructure as code (IaC) tool that allows users to define resource stacks using popular programming languages and then deploy those stacks using AWS CloudFormation. CDK is a command line interface (cli) tool that can be downloaded to a user’s development machine or CI/CD systems. The cdk cli tool provides functionality to create new projects, check differences between the deployed stack and the local configuration, synthesize the cdk code into CloudFormation script, and deploy directly from the machine running the tool. CDK is a purpose-built tool built on top of AWS cloudformation to provide enhanced functionality than native CloudFormation. Using fully featured programming languages allows users to create complex logic and conditions that can sometimes be required when creating resource stacks. At runtime, CDK can look up and resolve dynamic values that should not or could not be statically defined.

Basic Concepts

Commands

There are many commands available using the cdk cli tool. To use the cdk tool you must first install it on your machine. Additionally, you will need an AWS account, the aws cli and a profile already set up on the local machine. For information about the aws cli please see the aws cli documentation https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html. For information about how to install the cdk cli tool and additional information about the following commands, see the cdk documentation https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html.

CDK is a powerful tool with a lot more features than have been covered in this post. I highly recommend you read through the docs and work through some examples to get hands-on with CDK. The CDK workshop provides some great examples to get you hands-on with CDK (https://cdkworkshop.com/).

CDK is a great option for DevOps engineers who would need more functionality out of their IaC tooling and developers who would feel more comfortable keeping their IaC stacks closer to their code. IaC is extremely important for running applications in the cloud, and AWS CDK is a great option to have in your tech stack.

read more

You should start a blog

Have you ever wanted to share your knowledge or passion with the world? We all have things we are interested in, good, and want to share with the world. We tell our friends, families, and coworkers about these passions, but sometimes we want to reach a bigger audience. A blog is a great way to do that. Blogs can be about anything; it could be about work projects or skills, cooking, or your last knitting project. The point is that you have something to share with the world, and a blog can help you to reach that larger audience.

Like anything new, starting a blog can be tough. It’s hard to know how to get started. At a high level, there are a couple of things to consider when starting a blog, how you want to host it and where you want to host it. Let’s first start with how you want to host it. There are many options for creating a blog. Typically, they fall into the category of an off-the-shelf application such as WordPress, a static-generated site, or a custom site.

How to Host

Applications such as WordPress can be great options for hosting a blog or other types of sites. WordPress is one of if not the most popular website platforms on the market today. It’s easy to use, has many third-party plugins, and has a strong support community. While WordPress is a strong option, it also comes with some trade-offs. WordPress uses PHP and server-side code to host your website, which means it must be run on a server. Since the site needs to run on a server, there is typically a cost to host the site. Often the cost to host a WordPress site is minimal for smaller sites, but the costs can quickly add up after adding redundancy to the site and extra plugins. The site running on a server also means that it needs to have security patching and maintenance performed on it to ensure the safety of your site. If the WordPress blog is hosted on a platform that manages your site for you, it will typically contribute to your overall cost. Finally, solutions such as WordPress typically offer many features that are overkill for a simple blog. Most blogs don’t need advanced features and, therefore, can make using these tools more complicated for users.

Static site generators are a great option for hosting a simple blog. They are frameworks created using popular programming languages such as Ruby, Javascript, and Go. These frameworks allow users to create content by writing posts in markdown and then generate all of the code required to create the blog. Since there is no server-side code, these sites can be hosted on platforms such as AWS for a very low cost, if not free. Many of these frameworks also have templates that can be used for the site layout and aesthetics. An additional benefit of static sites is the reduced overhead. Since servers are not required to run the sites, you don’t have to worry about patching and maintenance on servers. One of my personal favorite static site generators is Jeykll. It’s an easy-to-use framework written in Ruby. It has many great, free templates available for users, and it has a large community that can help address any issues or questions.

A custom site could also be an option for individuals with the skill and desire to create their site. WordPress and static site generators do not require development skills from users. Users can choose from tons of different templates. The benefit of creating a custom site is that it allows you to create the site exactly the way you want it. It’s a good option if you have the required skills, but for someone like myself who does not come from a front-end development background, the learning curve is more than I’d want to take on for a simple blog.

Where to Host

Now that you understand how to host your blog better, it’s important to understand where to host it. There are a lot of companies on the market today that host websites. WordPress.com, Squarespace, and Wix are well-known options for off-the-shelf applications. They offer a variety of different options and pricing to choose from. If you would like to use an off-the-shelf solution such as WordPress and don’t want to have to manage the underlying resources yourself, then you should consider one of these providers.

If you would rather host your site yourself, or a static or custom site, then other platforms should be considered. For static sites, platforms such as Netlify can be good options. Netlify and other similar platforms offer entry-level starter tiers for free for users to get their blogs published globally. These platforms require very little to no knowledge of the underlying infrastructure required to host your site, and you can get your site up and running quickly.

Platforms like Netlify can be great for getting started but are very limited in what they offer. To expand your options, you should consider using a cloud platform such as AWS. AWS is a great option for hosting any of the discussed types of blogs. You can host off-the-shelf solutions along with static and custom sites. Using AWS gives you access to a wide array of services for not just your blog but any other applications you may want to use in the future. AWS always only charges for the resources you use, so hosting a static blog globally can be done for a very low cost, if not free.

There is a wide array of options to choose from when hosting a blog. There is not a one size fits all option either. You need to pick the right application and hosting provider to meet your individual needs. Personally, I chose to go with Jeykll and AWS but if WordPress with a managed provider works better for you, then do that. What’s most important at the end of the day is that you are sharing your knowledge and passion with the world.

read more