We will set up application loadbalancer for provisioned two EC2 instances.
The stack consists of resource for application loadbalancer which allows HTTP access to public and forward connection to attached EC2 instances.
In below template, "VPC", "SUBNETS", "SecurityGroup" and "WebServers" are defined as parameters which we can specify and refer provisioned stacks.
On outputs, these loadbalancer resource is exported for future usage.
Key resource types are below:
AWS::ElasticLoadBalancingV2::LoadBalancer | Loadbalancer which is required to specified subnets and security groups |
AWS::ElasticLoadBalancingV2::Listener | Listener mainly defines listening port of loadbalancer |
AWS::ElasticLoadBalancingV2::TargetGroup | It defines specific targets to forward the request which is received by loadbalancer. |
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Application Loadbalancer.",
"Parameters": {
"VPC": {
"Type": "String"
},
"SUBNETS": {
"Type": "String"
},
"SecurityGroup": {
"Type": "String"
},
"WebServers": {
"Type": "String"
}
},
"Resources": {
"ApplicationLoadBalancer": {
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": {
"Subnets": [
{
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet01"
}
},
{
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet02"
}
}
],
"Name": "ALB01",
"SecurityGroups": [{ "Fn::ImportValue": {
"Fn::Sub": "${SecurityGroup}-WebServerSecurityGroup"
}
}]
}
},
"ALBListener": {
"Type": "AWS::ElasticLoadBalancingV2::Listener",
"Properties": {
"DefaultActions": [
{
"Type": "forward",
"TargetGroupArn": {
"Ref": "ALBTargetGroup"
}
}
],
"LoadBalancerArn": {
"Ref": "ApplicationLoadBalancer"
},
"Port": "80",
"Protocol": "HTTP"
}
},
"ALBTargetGroup": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"HealthCheckIntervalSeconds": 10,
"HealthCheckTimeoutSeconds": 5,
"HealthyThresholdCount": 2,
"Port": 80,
"Protocol": "HTTP",
"UnhealthyThresholdCount": 5,
"VpcId": {
"Fn::ImportValue": {
"Fn::Sub": "${VPC}-VPCID"
}
},
"TargetGroupAttributes": [
{
"Key": "stickiness.enabled",
"Value": "true"
},
{
"Key": "stickiness.type",
"Value": "lb_cookie"
},
{
"Key": "stickiness.lb_cookie.duration_seconds",
"Value": "30"
}
],
"Targets": [
{
"Id": {
"Fn::ImportValue": {
"Fn::Sub": "${WebServers}-WebServer01"
}
},
"Port": 80
},
{
"Id": {
"Fn::ImportValue": {
"Fn::Sub": "${WebServers}-WebServer02"
}
},
"Port": 80
}
]
}
}
},
"Outputs": {
"ApplicationLoadBalancer": {
"Description": "ALB output value",
"Value": {
"Ref": "ApplicationLoadBalancer"
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}-ApplicationLoadBalancer"
}
}
}
}
}
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Application Loadbalancer.",
"Parameters": {
"VPC": {
"Type": "String"
},
"SUBNETS": {
"Type": "String"
},
"SecurityGroup": {
"Type": "String"
},
"WebServers": {
"Type": "String"
}
},
"Resources": {
"ApplicationLoadBalancer": {
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": {
"Subnets": [
{
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet01"
}
},
{
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet02"
}
}
],
"Name": "ALB01",
"SecurityGroups": [{ "Fn::ImportValue": {
"Fn::Sub": "${SecurityGroup}-WebServerSecurityGroup"
}
}]
}
},
"ALBListener": {
"Type": "AWS::ElasticLoadBalancingV2::Listener",
"Properties": {
"DefaultActions": [
{
"Type": "forward",
"TargetGroupArn": {
"Ref": "ALBTargetGroup"
}
}
],
"LoadBalancerArn": {
"Ref": "ApplicationLoadBalancer"
},
"Port": "80",
"Protocol": "HTTP"
}
},
"ALBTargetGroup": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"HealthCheckIntervalSeconds": 10,
"HealthCheckTimeoutSeconds": 5,
"HealthyThresholdCount": 2,
"Port": 80,
"Protocol": "HTTP",
"UnhealthyThresholdCount": 5,
"VpcId": {
"Fn::ImportValue": {
"Fn::Sub": "${VPC}-VPCID"
}
},
"TargetGroupAttributes": [
{
"Key": "stickiness.enabled",
"Value": "true"
},
{
"Key": "stickiness.type",
"Value": "lb_cookie"
},
{
"Key": "stickiness.lb_cookie.duration_seconds",
"Value": "30"
}
],
"Targets": [
{
"Id": {
"Fn::ImportValue": {
"Fn::Sub": "${WebServers}-WebServer01"
}
},
"Port": 80
},
{
"Id": {
"Fn::ImportValue": {
"Fn::Sub": "${WebServers}-WebServer02"
}
},
"Port": 80
}
]
}
}
},
"Outputs": {
"ApplicationLoadBalancer": {
"Description": "ALB output value",
"Value": {
"Ref": "ApplicationLoadBalancer"
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}-ApplicationLoadBalancer"
}
}
}
}
}
See more detailed information of each element on here.
Here's a video tutorial for this.
No comments:
Post a Comment