Posts by SpinningOps Editor

How to create CloudFront distribution

CDN have many benefits and it’s improving your content delivery speed and security, CloudFront with S3 hosted website or your custom app that hosted on EC2 can improve your app delivery.

How can we create a new distribution?

You can use the dashboard but if you need to create more than one or have this task repeated it’s better to use a script to create and manage your distributions.

run the create_cloud_front_distibution to create a new dist, add your domain name as the parameter in the function.

it will take a few minutes to create the new distribution.

How to create ACM

If you’re using AWS it’s very easy using ACM for your SSL certificates, the integration is the best option in my opinion for your stackm again if you’re using AWS.

How to create a new ACM with just a few lines of code?

You only need one function:
1. create_acm

You can use the list_acm_certs function to verify the certificate creation.

How to use the scripts?

  • Run the create_acm function with your domain name as the variable
  • verify the acm was created with the list_acm_certs function
  • update your CNAME DNS record for the domain and check at the AWS ACM dashboard that the certificate was approved.

You can download the code from github

How to create static website hosting using S3

Sometimes you need a static website for your company or clients, static websites are still a thing that can be useful and can be used for many tasks such as: information, LP or any other content that does not require a database.

How to create a static website hosting in a few lines of code?

You only need three functions:
1. create bucket
2. create bucket policy
3. enable static website hosting

also don’t forget uploading your website files to the bucket.

Additional tasks to make your website faster and add SSL

Create CloudFront distribution and add your website bucket as origin
Create ACM certificate and add it to the cloudFront for the relevant bucket

If you want to download those functions to create your hosted bucket it’s here

import boto3
import json
# enter bucket name
BUCKET_NAME = 'new-s3-bucket-spinningops'
def s3_client(): s3 = boto3.client('s3') return s3
def create_bucket(bucket_name):
    """ creates a new s3 bucket """
    return s3_client().create_bucket( Bucket=bucket_name,
    CreateBucketConfiguration={ 'LocationConstraint': 'eu-central-1' } )
def create_bucket_policy():
    bucket_policy = { 'Version': '2012-10-17', 'Statement': [ {
        'Sid': 'PublicRead',
        'Effect': 'Allow',
        'Principal': '*',
        'Action': ['s3:*'],
        'Resource': f'arn:aws:s3:::{BUCKET_NAME}/*' } ] }
    # Convert the policy from JSON dict to string
    policy_string = json.dumps(bucket_policy)
    # Set the new policy return s3_client().put_bucket_policy(
    Bucket=BUCKET_NAME,
    Policy=policy_string )
def enable_static_website_hosting():
    """ modify bucket for website hosting """
    website_configuration = {
        'ErrorDocument': {'Key': 'error.html'},
        'IndexDocument': {'Suffix': 'index.html'}, }
    return s3_client().put_bucket_website( Bucket=BUCKET_NAME,
    WebsiteConfiguration=website_configuration )

if __name__ == '__main__':
    ''' execute the function you want, uncomment to reuse
    functions '''
    # create_bucket(BUCKET_NAME)
    # host_static_website(WEBSITE_BUCKET_NAME)
    # create_bucket_policy() enable_static_website_hosting()

How to monitor your website with only 2 metrics?

When you have a static website on your company or just for yourself you still need to monitor it to ensure it’s operational. so let’s do this with only 2 metrics.

First metric is HTTPS

by monitoring HTTPS we know that the SSL certificate is ok and the web server is responding, that’s two metrics for the price of one.

Second metric is Web-scraping

when we web scraping our website we know if the content is served as it should be thus making sure our web server responded with content pages and not empty pages.

Worth mentioning third metric

you’ll probably want to add page speed metric to make sure the website is responding fast enough for visitors.

Summary

I like to keep things simple and by using only those 2 metrics I can make sure the website is working as expected and I don’t need to monitor more than 2 or 3 metrics, I just monitor the metrics that important the most.

Blog Intro

Welcome to SpinningOps blog! this blog created for cloud administrators that adopted DevOps approach.

What is this blog approach?

  • Simplicity
  • NO SSH
  • Disposable resources
  • Immutable infrastructure

Do more with less

if you’re interested in those approaches please subscribe to the blog.