-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation.yaml
206 lines (192 loc) · 5.28 KB
/
cloudformation.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
---
Transform: AWS::Serverless-2016-10-31
AWSTemplateFormatVersion: '2010-09-09'
Description: Template of Register Application Load Balancer as IP target for Network
Load Balancer
Parameters:
EIP1Allocation1:
Type: String
Description: Elastic IP Allocation id for NLB
Subnet1:
Type: String
Description: Subnet for EIP for NLB
VPCID:
Type: String
Description: VPC ID for NLB
InternalALBDNSName:
Type: String
Default: ''
Description: Enter the DNS name of your internal Application Load Balancer name.
e.g.application-load-balancer.elb.us-east-1.amazonaws.com
S3BucketName:
Type: String
Default: ''
Description: Enter the S3 Bucket name that you want to store Application Load
Balancer's node IP addresses
ALBListenerPort:
Type: String
Default: '80'
Description: Enter the traffic listener port of your internal Application Load
Balancer
MAXDNSLookupPerInvocation:
Type: Number
Default: 50
Description: Please specify the max DNS lookup this function can perform per invocation
InvocationBeforeDeregistration:
Type: Number
Default: 3
Description: Please specify how many invocations are needed before a IP is deregistered
CWMetricFlagIPCount:
Type: String
Default: 'True'
AllowedValues:
- 'True'
- 'False'
Description: Please specify if you want the Lambda function to create a CloudWatch
metric to track IP count of your internal Application Load Balancer
LambdaBucket:
Type: String
Default: exampleloadbalancer-eu-central-1
LambdaKey:
Type: String
Default: blog-posts/static-ip-for-application-load-balancer/populate_NLB_TG_with_ALB.zip
Resources:
############## LAMBDA RESOURCES ################
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri:
Bucket:
Ref: LambdaBucket
Key:
Ref: LambdaKey
Description: Register Application Load Balancer to Network Load Balancer
Handler: populate_NLB_TG_with_ALB.lambda_handler
Timeout: 300
Role:
Fn::GetAtt:
- LambdaIAMRole
- Arn
Runtime: python2.7
Environment:
Variables:
ALB_DNS_NAME:
Ref: InternalALBDNSName
NLB_TG_ARN:
Ref: NLBTargetGroup
S3_BUCKET:
Ref: S3BucketName
MAX_LOOKUP_PER_INVOCATION:
Ref: MAXDNSLookupPerInvocation
INVOCATIONS_BEFORE_DEREGISTRATION:
Ref: InvocationBeforeDeregistration
CW_METRIC_FLAG_IP_COUNT:
Ref: CWMetricFlagIPCount
ALB_LISTENER:
Ref: ALBListenerPort
ScheduledRule:
Type: AWS::Events::Rule
Properties:
Description: ScheduledRule
ScheduleExpression: rate(1 minute)
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- LambdaFunction
- Arn
Id: TargetFunctionV1
DependsOn:
- LambdaFunction
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Fn::GetAtt:
- LambdaFunction
- Arn
Principal: events.amazonaws.com
Action: lambda:InvokeFunction
SourceArn:
Fn::GetAtt:
- ScheduledRule
- Arn
LambdaIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: Lambda-ALBasTarget
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: LambdaLogging
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- arn:aws:logs:*:*:*
- Sid: S3
Action:
- s3:Get*
- s3:PutObject
- s3:CreateBucket
- s3:ListBucket
- s3:ListAllMyBuckets
Effect: Allow
Resource: "*"
- Sid: ELB
Action:
- elasticloadbalancing:Describe*
- elasticloadbalancing:RegisterTargets
- elasticloadbalancing:DeregisterTargets
Effect: Allow
Resource: "*"
- Sid: CW
Action:
- cloudwatch:putMetricData
Effect: Allow
Resource: "*"
############## LOAD BALANCER RESOURCES ################
NLB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
SubnetMappings:
- AllocationId:
Ref: EIP1Allocation1
SubnetId:
Ref: Subnet1
Type: network
IpAddressType: ipv4
NLBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
TargetType: ip
Name: NLBTargets
Port: 80
Protocol: TCP
# TargetGroupAttributes:
# - Key: deregistration_delay.timeout_seconds
# Value: '20'
VpcId:
Ref: VPCID
Listener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: NLBTargetGroup
LoadBalancerArn:
Ref: NLB
Port: 80
Protocol: TCP