Ludwig Tutorial 5: EC2 Instance, Security 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.

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

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
}

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

With this function, an EC2 instance is added.

my-instance: EC2.Instance.new {

As with regions in AWS, instance types are enumerated in the EC2 module.

instanceType: EC2.T2_micro,
subnet: subnet1,
image: "ami-7172b611",

Note that the value here, a list of SecurityGroup values, refers to a value that comes later in the source code (public-ec2-sg). In Ludwig, all values and references are read before any dereferencing or evaluation, so the order of bindings does not matter.

    securityGroups: [public-ec2-sg]
}

With this function, a Security Group is added.

public-ec2-sg: EC2.SecurityGroup.new {
    description: "Allow traffic from the internet",
    ipPermissions: [

A little more complex function. The http function creates a rule for TCP port 80, and EC2.IpPermission.Target.all is a convenience value in the standard library for the CIDR block 0.0.0.0/0.

        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 Tutorial5.lw

This will create the following infrastructure in your AWS account:

  • VPC
  • Subnet
  • Internet gateway
  • Route table
  • EC2 instance
  • Security group

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

fugue update [ALIAS or FID] Tutorial5.lw

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

lwc Tutorial5.lw