The Curious Dev

Various programming sidetracks and shiny-object detours

Cloudifying MoinMoin

This post takes the Installing MoinMoin on Nginx and uWSGI post further by deploying the built instance of MoinMoin within an AutoScalingGroup (ASG).

Creating the AMI

From my previous post of we have come out with a running instance configured as desired. The only trouble with this situation is that it’s a snowflake.

First step to producing a server that can be repeatedly recreated is to take a snapshot of the instance, this is as simple as choosing so in the AWS console and giving it a name.

Create an Amazon Machine Image(AMI)

Take the time to describe your image so you can remember what it does down the track.

Describe your image

It doesn’t typically take very long, certainly not with these tiny instances with only 8GB of disk.

Pending image

You can then find the created image under the Images > AMIs menu in the console.

Pending image

This AMI ID is what we’ll use in the next step.

Templating with CloudFormation

So now we’ve got an AMI, the next step is to create a CloudFormation template that will produce an ASG for the MoinMoin application to be served out of.

First up, we create a skeleton of the template, with some metadata, parameters and resources:

{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description": "Template for the MoinMoin wiki at wiki.easyas.info.",
"Parameters" : {

},
"Resources": {

}

In the Parameters section I’ll add a few options that might be needed to be tweaked at deployment time. Properties such as the aforementioned AMI, the EC2 instance type and the VPC subnet to operate in. I’m cheating a little here by using a Security Group that I created manually for another project.

"Parameters" : {
  "BaseAMI": {
      "Description": "AMI to use for AutoScaling",
      "Type": "String",
      "Default": "ami-00bd6960"
  },
  "InstanceType": {
      "Description": "Instance Type",
      "Type": "String",
      "Default": "t2.nano"
  },
  "moinmoinSG": {
    "Description": "SecurityGroup to control access to instance",
    "Type": "String",
    "Default": "sg-0feddb69"
  },
  "moinmoinKeypair": {
    "Description": "The keypair used by instances within the ASG",
    "Type": "AWS::EC2::KeyPair::KeyName",
    "Default": "ec2-user-keys"
  },
  "vpcSubnet": {
    "Description": "VPC Subnet for all the things, for now",
    "Type": "String",
    "Default": "subnet-2653ec43"
  }
}

Next up is the meat of the template, Resources, where the actual ASG is declared along with the LaunchConfiguration that it needs to know how to fire up instances.

Here’s the LaunchConfiguration, where we start with the MoinMoin AMI, configure an 8GB disk, and then setup the UserData to ensure that MoinMoin is up and running as soon as the instance comes up:

"Resources": {
  "moinmoinLC": {
    "Type": "AWS::AutoScaling::LaunchConfiguration",
    "Properties": {
        "AssociatePublicIpAddress": "true",
        "BlockDeviceMappings": [
          {
            "DeviceName": "/dev/xvda",
            "Ebs": {
              "DeleteOnTermination": "true",
              "VolumeType": "gp2",
              "VolumeSize": "8"
            }
          }
        ],
        "ImageId": { "Ref" : "BaseAMI" },
        "KeyName": { "Ref" : "moinmoinKeypair" },
        "IamInstanceProfile": "moinmoin-role",
        "InstanceType": { "Ref" : "InstanceType" },
        "SecurityGroups": [ { "Ref" : "moinmoinSG" }  ],
        "UserData": {"Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/sh", "\n",
          "yum update -y", "\n",
          "#TODO call script to update IP in Route53 config for wiki.easyas.info"
          "start moin", "\n",
          "service nginx start", "\n"
          ]]}
        }
    }
  }
}

An ASG is a really powerful way to provide reliability with minimal effort. The AutoScalingGroup is where we reference the above “moinmoinLC” LaunchConfiguration and declare the UpdatePolicy and the number of instances.

The UpdatePolicy is the way that CloudFormation knows how to transition from one version of a template to another. The DesiredCapacity is just that, how many instances we’d prefer to have. Having the MinSize and MaxSize both set to one simply ensures there’s always a new instance brought up if the running one were to fail with no more than one instance being provisioned at any one time.

"Resources": {
  "moinmoinASG": {
    "Type" : "AWS::AutoScaling::AutoScalingGroup",
    "Properties": {
      "DesiredCapacity": "1",
      "HealthCheckGracePeriod": 180,
      "HealthCheckType": "EC2",
      "LaunchConfigurationName": { "Ref": "moinmoinLC" },
      "MaxSize": "1",
      "MinSize": "1",
      "Tags": [
        {
          "Key": "Name",
          "Value": "moinmoin-wiki",
          "PropagateAtLaunch": true
        }
      ],
      "VPCZoneIdentifier": [ { "Ref": "vpcSubnet" } ]
    },
    "UpdatePolicy" : {
      "AutoScalingRollingUpdate" : {
        "MinInstancesInService" : "0",
        "MaxBatchSize" : "1",
        "WaitOnResourceSignals" : "true",
        "PauseTime" : "PT5M"
      }
    }
  }
}

So that completes the template and we can simply run that in the CloudFormation console, there are several options but here I just upload the file:

Choose your template file

Then we select the values for the parameters, most of which are conveniently prefilled by the default values of our template parameters:

Select parameter values

Add some tags, Name becomes the name of the provisioned instance(s):

Tagging the template

Review and confirm the template execution:

Review and confirm the template

If things go smoothly, you’ll be able to see the template execute and end up in a clean “CREATE_COMPLETE” state.

Successful template creation

You should then be able to go on over to the EC2 console and see the running instance:

The provisioned instance for the ASG

And of course, check out the site!

MoinMoin, cloudified!

As an alternative to using the AWS console, you can execute a CloudFormation template from the AWS CLI incredibly easily:

aws cloudformation create-stack --stack-name mystackname --template-url s3://mybucket/a.template.file.json --parameters s3://mybucket/my.parameters.file.json

Hope that helps demonstrate how easy it can be to fire up an Auto Scaled server with minimal fuss.

Comments

Included file 'facebook_like.html' not found in _includes directory