Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
189 lines (170 sloc) 4.97 KB
import Fugue.AWS as AWS
import Fugue.AWS.AutoScaling as AutoScaling
import Fugue.AWS.DynamoDB as DynamoDB
import Fugue.AWS.EC2 as EC2
import Fugue.AWS.ELB as ELB
import Fugue.AWS.IAM as IAM
import Fugue.AWS.Pattern.Network as Network
type DemoApp: {
elb: ELB.LoadBalancer,
ddb: DynamoDB.Table,
network: Network.Network,
asg: AutoScaling.AutoScalingGroup
}
fun new {
region: Optional<AWS.Region>, #default AWS.US-west-2
asg-min: Optional<Int>, #default 1
asg-max: Optional<Int>, #default 4
ddb-read: Optional<Int>, #default 10
ddb-write: Optional<Int>, #default 10
instance-type: Optional<EC2.InstanceType> #default T2_micro
} -> DemoApp:
#########################
# Region
#########################
let demo-app-region: Optional.unpack(AWS.Us-west-2, region)
#########################
# Tags
#########################
let demo-app-tag: AWS.Tag({
key: 'Application',
value: 'Fugue Demo App'
})
#########################
# Network (VPC)
#########################
let demo-app-network:
Network.new {
name: "demo-app-network",
region: demo-app-region,
cidr: "10.0.0.0/16",
publicSubnets: [
(AWS.A, "10.0.1.0/24"),
(AWS.B, "10.0.2.0/24"),
],
privateSubnets: [],
}
#########################
# DYNAMODB TABLE
#########################
let demo-app-read: Optional.unpack(10, ddb-read)
let demo-app-write: Optional.unpack(10, ddb-write)
let demo-app-table: DynamoDB.Table.new {
name: "demo-app-table",
attributes: {"PropertyName": DynamoDB.S},
schema: {"PropertyName": DynamoDB.HASH},
provisionedThroughput: {
read: demo-app-read,
write: demo-app-write,
},
region: demo-app-region,
}
#########################
# AUTOSCALING GROUP
#########################
let demo-app-min: Optional.unpack(1, asg-min)
let demo-app-max: Optional.unpack(4, asg-max)
let demo-app-asg: AutoScaling.AutoScalingGroup.new {
defaultCooldown: 300,
healthCheckType: AutoScaling.Ec2,
subnets: demo-app-network.publicSubnets,
loadBalancers: [demo-app-elb],
minSize: demo-app-min,
maxSize: demo-app-max,
launchConfiguration: demo-app-lc,
tags: [demo-app-tag],
terminationPolicies: [AutoScaling.ClosestToNextInstanceHour],
enabledMetrics: [
AutoScaling.GroupInServiceInstances,
AutoScaling.GroupTotalInstances
]
}
#########################
### ASG LAUNCH CONFIG
#########################
let demo-app-instanceType: Optional.unpack(EC2.T2_micro, instance-type)
let demo-app-lc: AutoScaling.LaunchConfiguration.new {
image: "ami-b7b366d7",
securityGroups: [demo-app-web-sg],
instanceType: demo-app-instanceType,
associatePublicIpAddress: True,
iamInstanceProfile: demo-app-instance-profile
}
#########################
### IAM Role
#########################
let demo-app-role: IAM.Role.new {
roleName: 'demo-app-role',
assumeRolePolicyDocument: IAM.Policy.AssumeRole.ec2,
rolePolicies: [demo-app-role-policy]
}
let demo-app-instance-profile: IAM.InstanceProfile.new {
instanceProfileName: 'demo-app-instance-profile',
roles: [demo-app-role]
}
let demo-app-role-policy: IAM.Policy.new {
policyName: 'ddb-full',
policyDocument: ddb-policy-document
}
#########################
# LOAD BALANCER
#########################
let demo-app-elb: ELB.LoadBalancer.new {
loadBalancerName: "demo-app-elb",
subnets: demo-app-network.publicSubnets,
healthCheck: ELB.HealthCheck({
target: "TCP:3000",
interval: 15,
timeout: 3,
unhealthyThreshold: 3,
healthyThreshold: 3
}),
securityGroups: [demo-app-elb-sg],
listeners: [demo-app-listener],
tags: [demo-app-tag],
}
#########################
### LISTENER
#########################
let demo-app-listener: ELB.Listener.new {
protocol: ELB.HTTP,
loadBalancerPort: 80,
instancePort: 3000
}
#########################
# ELB SG
#########################
let demo-app-elb-sg: EC2.SecurityGroup.new {
description: "Allow http/s traffic from the Internet",
ipPermissions: [
EC2.IpPermission.http(EC2.IpPermission.Target.all),
EC2.IpPermission.https(EC2.IpPermission.Target.all),
],
tags: [demo-app-tag],
vpc: demo-app-network.vpc
}
#########################
# WEB SG
#########################
let demo-app-web-sg: EC2.SecurityGroup.new {
description: "Allow http traffic from the ELB SG",
ipPermissions: [
EC2.IpPermission.tcp(3000, EC2.IpPermission.Target.securityGroup(demo-app-elb-sg)),
],
tags: [demo-app-tag],
vpc: demo-app-network.vpc
}
#########################
# CONVENIENCE / CLARITY
#########################
let ddb-policy-document: '{"Version": "2012-10-17", "Statement": [ { "Action": [ "dynamodb:*" ], "Effect": "Allow", "Resource": "*" }]}'
#########################
# RETURN
#########################
let return: {
elb: demo-app-elb,
ddb: demo-app-table,
network: demo-app-network,
asg: demo-app-asg
}
return