Ludwig Tutorial 7: Load Balancer

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

Now an ELB is introduced. ELB data and functions are in the Fugue.AWS.ELB module.

import Fugue.AWS.ELB as ELB

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
}

Here, a load balancer is added. All of the syntax or idioms here are shown in previous examples.

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
        }
    ],

Note that there is a tcp function in the HealthCheck module. This is a common idiom in the Fugue Standard Library. There is also a new function that allows more full control, but this more succinctly expressive function is preferred. There are also http, https, and other similar functions.

And now, we wrap it up.

    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
}

Running and Compiling the Composition

You can run this composition with the following command:

fugue run Tutorial7.lw

This will create the following infrastructure in your AWS account:

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

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

fugue update [ALIAS or FID] Tutorial7.lw

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

lwc Tutorial7.lw