Ludwig Tutorial 8: Auto Scaling Group

This tutorial is actually a Ludwig composition. You can download it here and fugue run it like you would any other composition. You can use lwc, the Ludwig compiler, to compile the composition.

If you’re interested in text editor plug-ins, you can find them here. Click here to return to the Tutorial table of contents.

Ready?

Let’s Go!

We’ll begin with the work from the last tutorial.

composition

import Fugue.AWS as AWS
import Fugue.AWS.EC2 as EC2
import Fugue.AWS.ELB as ELB

Finally, an ASG is introduced in this tutorial, so the Fugue.AWS.AutoScaling module is imported. We’ll add the ASG itself near the bottom of the file, so we’ll scroll past our cumulative work...

import Fugue.AWS.AutoScaling as AutoScaling

my-vpc: EC2.Vpc.new {
  cidrBlock: "10.0.0.0/16",
  region: AWS.Us-west-2
}

subnet1: EC2.Subnet.new {
  cidrBlock: '10.0.1.0/24',
  vpc: my-vpc,
  availabilityZone: AWS.A,
  mapPublicIpOnLaunch: True
}

subnet2: EC2.Subnet.new {
  cidrBlock: '10.0.2.0/24',
  vpc: my-vpc,
  availabilityZone: AWS.B,
  mapPublicIpOnLaunch: True
}

my-igw: EC2.InternetGateway.new {
  vpc: my-vpc
}

route-table: EC2.RouteTable.new {
    vpc: my-vpc,
    routes: [
        EC2.Route.new {
            destinationCidrBlock: "0.0.0.0/0",
            target: EC2.GatewayTarget(my-igw)
        }
    ],
    associations: [subnet1]
}

my-instance: EC2.Instance.new {
    instanceType: EC2.T2_micro,
    subnet: subnet1,
    image: "ami-7172b611",
    securityGroups: [public-ec2-sg]
}

public-ec2-sg: EC2.SecurityGroup.new {
    description: "Allow traffic from the internet",
    ipPermissions: [
        EC2.IpPermission.http(EC2.IpPermission.Target.all)
    ],
    vpc: my-vpc
}

my-elb: ELB.LoadBalancer.new {
    loadBalancerName: "my-load-balancer",
    securityGroups: [elb-sg],
    subnets: [
        subnet1,
        subnet2
    ],
    listeners: [
        ELB.Listener.new {
            protocol: ELB.HTTP,
            loadBalancerPort: 80,
            instancePort: 8080
        }
    ],
    healthCheck: ELB.HealthCheck.tcp {
        port: 3000,
        interval: 15,
        timeout: 3,
        unhealthyThreshold: 3,
        healthyThreshold: 3
    }
}

elb-sg: EC2.SecurityGroup.new {
    description: "Allow traffic from the internet",
    ipPermissions: [
        EC2.IpPermission.http(EC2.IpPermission.Target.all)
    ],
    vpc: my-vpc
}

And here we are. With this function, an AutoScaling Group is added. Everything you see here should be familiar from previous tutorial compositions.

my-asg: AutoScaling.AutoScalingGroup.new {
    subnets: [subnet1, subnet2],
    loadBalancers: [my-elb],
    minSize: 2,
    maxSize: 5,
    healthCheckType: AutoScaling.Ec2,
    launchConfiguration: AutoScaling.LaunchConfiguration.new {
        image: "ami-b7b366d7",
        securityGroups: [asg-sg],
        instanceType: EC2.T2_micro,
        associatePublicIpAddress: True
    },
    defaultCooldown: 50
}

asg-sg: EC2.SecurityGroup.new {
    description: "Allow traffic from ELB",
    ipPermissions: [
        EC2.IpPermission.new {
            ipProtocol: "tcp",
            fromPort: 8080,
            toPort: 8080,
            target: EC2.SecurityGroups([elb-sg])
        }
    ],
    vpc: my-vpc
}

If you had trouble with some of that, consider reviewing the previous tutorials. You’ve now seen everything you’d need to know to write compositions!

Running and Compiling the Composition

You can run this composition with the following command:

fugue run Tutorial8.lw

This will create the following infrastructure in your AWS account:

  • VPC
  • 2 subnets
  • Internet gateway
  • Route table
  • EC2 instance
  • Security group
  • ELB
  • ASG

Optionally, if you have a process running from previous tutorials, you can simply update the process like this:

fugue update [ALIAS or FID] Tutorial8.lw

To just check compilation, invoke lwc, the Ludwig compiler:

lwc Tutorial8.lw

You’re Done!

Congratulations! You’ve finished the Ludwig Tutorial. If you’d like to learn more about Ludwig, check out Advanced Ludwig. If you’d prefer to peruse other walkthroughs and examples, see Examples. Or you can jump right into the Standard Library Reference to start creating your own compositions. As always, feel free to reach out to support@fugue.co with any questions.