-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ElasticLoadBalancingV2 ListenerRule (#1427)
- Loading branch information
Showing
3 changed files
with
362 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
from troposphere import Parameter, Template | ||
import troposphere.elasticloadbalancingv2 as elb | ||
|
||
|
||
def main(): | ||
template = Template() | ||
|
||
subnetA = template.add_parameter(Parameter("subnetA", Type="String")) | ||
subnetB = template.add_parameter(Parameter("subnetB", Type="String")) | ||
|
||
alb = template.add_resource( | ||
elb.LoadBalancer( | ||
"ALB", | ||
Scheme="internet-facing", | ||
Subnets=[subnetA.ref(), subnetB.ref()], | ||
) | ||
) | ||
|
||
listener = template.add_resource( | ||
elb.Listener( | ||
"Listener", | ||
Port="80", | ||
Protocol="HTTP", | ||
LoadBalancerArn=alb.ref(), | ||
DefaultActions=[ | ||
elb.Action( | ||
Type="fixed-response", | ||
FixedResponseConfig=elb.FixedResponseConfig( | ||
StatusCode="200", | ||
MessageBody=( | ||
"This is a fixed response for the default " | ||
"ALB action" | ||
), | ||
ContentType="text/plain", | ||
), | ||
) | ||
], | ||
) | ||
) | ||
|
||
template.add_resource( | ||
[ | ||
elb.ListenerRule( | ||
"ListenerRuleApi", | ||
ListenerArn=listener.ref(), | ||
Conditions=[ | ||
elb.Condition( | ||
Field="host-header", Values=["api.example.com"] | ||
), | ||
elb.Condition( | ||
Field="http-header", | ||
HttpHeaderConfig=elb.HttpHeaderConfig( | ||
HttpHeaderName="X-Action", Values=["Create"] | ||
), | ||
), | ||
elb.Condition( | ||
Field="path-pattern", | ||
PathPatternConfig=elb.PathPatternConfig( | ||
Values=["/api/*"] | ||
), | ||
), | ||
elb.Condition( | ||
Field="http-request-method", | ||
HttpRequestMethodConfig=elb.HttpRequestMethodConfig( | ||
Values=["POST"] | ||
), | ||
), | ||
], | ||
Actions=[ | ||
elb.Action( | ||
Type="fixed-response", | ||
FixedResponseConfig=elb.FixedResponseConfig( | ||
StatusCode="200", | ||
MessageBody=( | ||
"This is a fixed response for any API POST " | ||
"request with header X-Action: Create" | ||
), | ||
ContentType="text/plain", | ||
), | ||
) | ||
], | ||
Priority="10", | ||
), | ||
elb.ListenerRule( | ||
"ListenerRuleWeb", | ||
ListenerArn=listener.ref(), | ||
Conditions=[ | ||
elb.Condition( | ||
Field="host-header", | ||
HostHeaderConfig=elb.HostHeaderConfig( | ||
Values=["www.example.com"] | ||
), | ||
), | ||
elb.Condition( | ||
Field="path-pattern", | ||
PathPatternConfig=elb.PathPatternConfig( | ||
Values=["/web/*"] | ||
), | ||
), | ||
], | ||
Actions=[ | ||
elb.Action( | ||
Type="fixed-response", | ||
FixedResponseConfig=elb.FixedResponseConfig( | ||
StatusCode="200", | ||
MessageBody=( | ||
"This is a fixed response for any WEB " | ||
"request" | ||
), | ||
ContentType="text/plain", | ||
), | ||
) | ||
], | ||
Priority="20", | ||
), | ||
elb.ListenerRule( | ||
"ListenerRuleMetrics", | ||
ListenerArn=listener.ref(), | ||
Conditions=[ | ||
elb.Condition(Field="path-pattern", Values=["/metrics/*"]) | ||
], | ||
Actions=[ | ||
elb.Action( | ||
Type="redirect", | ||
RedirectConfig=elb.RedirectConfig( | ||
StatusCode="HTTP_301", Protocol="HTTPS", Port="443" | ||
), | ||
) | ||
], | ||
Priority="30", | ||
), | ||
elb.ListenerRule( | ||
"ListenerRuleSourceIp", | ||
ListenerArn=listener.ref(), | ||
Conditions=[ | ||
elb.Condition( | ||
Field="source-ip", | ||
SourceIpConfig=elb.SourceIpConfig( | ||
Values=["52.30.12.16/28"] | ||
), | ||
) | ||
], | ||
Actions=[ | ||
elb.Action( | ||
Type="fixed-response", | ||
FixedResponseConfig=elb.FixedResponseConfig( | ||
StatusCode="200", | ||
MessageBody=( | ||
"The request came from IP range " | ||
"52.30.12.16/28" | ||
), | ||
ContentType="text/plain", | ||
), | ||
) | ||
], | ||
Priority="40", | ||
), | ||
] | ||
) | ||
|
||
print(template.to_json()) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
191 changes: 191 additions & 0 deletions
191
tests/examples_output/ApplicationLB_FixedActions.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
{ | ||
"Parameters": { | ||
"subnetA": { | ||
"Type": "String" | ||
}, | ||
"subnetB": { | ||
"Type": "String" | ||
} | ||
}, | ||
"Resources": { | ||
"ALB": { | ||
"Properties": { | ||
"Scheme": "internet-facing", | ||
"Subnets": [ | ||
{ | ||
"Ref": "subnetA" | ||
}, | ||
{ | ||
"Ref": "subnetB" | ||
} | ||
] | ||
}, | ||
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer" | ||
}, | ||
"Listener": { | ||
"Properties": { | ||
"DefaultActions": [ | ||
{ | ||
"FixedResponseConfig": { | ||
"ContentType": "text/plain", | ||
"MessageBody": "This is a fixed response for the default ALB action", | ||
"StatusCode": "200" | ||
}, | ||
"Type": "fixed-response" | ||
} | ||
], | ||
"LoadBalancerArn": { | ||
"Ref": "ALB" | ||
}, | ||
"Port": "80", | ||
"Protocol": "HTTP" | ||
}, | ||
"Type": "AWS::ElasticLoadBalancingV2::Listener" | ||
}, | ||
"ListenerRuleApi": { | ||
"Properties": { | ||
"Actions": [ | ||
{ | ||
"FixedResponseConfig": { | ||
"ContentType": "text/plain", | ||
"MessageBody": "This is a fixed response for any API POST request with header X-Action: Create", | ||
"StatusCode": "200" | ||
}, | ||
"Type": "fixed-response" | ||
} | ||
], | ||
"Conditions": [ | ||
{ | ||
"Field": "host-header", | ||
"Values": [ | ||
"api.example.com" | ||
] | ||
}, | ||
{ | ||
"Field": "http-header", | ||
"HttpHeaderConfig": { | ||
"HttpHeaderName": "X-Action", | ||
"Values": [ | ||
"Create" | ||
] | ||
} | ||
}, | ||
{ | ||
"Field": "path-pattern", | ||
"PathPatternConfig": { | ||
"Values": [ | ||
"/api/*" | ||
] | ||
} | ||
}, | ||
{ | ||
"Field": "http-request-method", | ||
"HttpRequestMethodConfig": { | ||
"Values": [ | ||
"POST" | ||
] | ||
} | ||
} | ||
], | ||
"ListenerArn": { | ||
"Ref": "Listener" | ||
}, | ||
"Priority": "10" | ||
}, | ||
"Type": "AWS::ElasticLoadBalancingV2::ListenerRule" | ||
}, | ||
"ListenerRuleMetrics": { | ||
"Properties": { | ||
"Actions": [ | ||
{ | ||
"RedirectConfig": { | ||
"Port": "443", | ||
"Protocol": "HTTPS", | ||
"StatusCode": "HTTP_301" | ||
}, | ||
"Type": "redirect" | ||
} | ||
], | ||
"Conditions": [ | ||
{ | ||
"Field": "path-pattern", | ||
"Values": [ | ||
"/metrics/*" | ||
] | ||
} | ||
], | ||
"ListenerArn": { | ||
"Ref": "Listener" | ||
}, | ||
"Priority": "30" | ||
}, | ||
"Type": "AWS::ElasticLoadBalancingV2::ListenerRule" | ||
}, | ||
"ListenerRuleSourceIp": { | ||
"Properties": { | ||
"Actions": [ | ||
{ | ||
"FixedResponseConfig": { | ||
"ContentType": "text/plain", | ||
"MessageBody": "The request came from IP range 52.30.12.16/28", | ||
"StatusCode": "200" | ||
}, | ||
"Type": "fixed-response" | ||
} | ||
], | ||
"Conditions": [ | ||
{ | ||
"Field": "source-ip", | ||
"SourceIpConfig": { | ||
"Values": [ | ||
"52.30.12.16/28" | ||
] | ||
} | ||
} | ||
], | ||
"ListenerArn": { | ||
"Ref": "Listener" | ||
}, | ||
"Priority": "40" | ||
}, | ||
"Type": "AWS::ElasticLoadBalancingV2::ListenerRule" | ||
}, | ||
"ListenerRuleWeb": { | ||
"Properties": { | ||
"Actions": [ | ||
{ | ||
"FixedResponseConfig": { | ||
"ContentType": "text/plain", | ||
"MessageBody": "This is a fixed response for any WEB request", | ||
"StatusCode": "200" | ||
}, | ||
"Type": "fixed-response" | ||
} | ||
], | ||
"Conditions": [ | ||
{ | ||
"Field": "host-header", | ||
"HostHeaderConfig": { | ||
"Values": [ | ||
"www.example.com" | ||
] | ||
} | ||
}, | ||
{ | ||
"Field": "path-pattern", | ||
"PathPatternConfig": { | ||
"Values": [ | ||
"/web/*" | ||
] | ||
} | ||
} | ||
], | ||
"ListenerArn": { | ||
"Ref": "Listener" | ||
}, | ||
"Priority": "20" | ||
}, | ||
"Type": "AWS::ElasticLoadBalancingV2::ListenerRule" | ||
} | ||
} | ||
} |
Oops, something went wrong.