-
Notifications
You must be signed in to change notification settings - Fork 2
/
az-mapping.yaml
181 lines (166 loc) · 6.16 KB
/
az-mapping.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AWSTemplateFormatVersion: '2010-09-09'
Description: Map zone-ids to zone-names to support multi-account zonal consistency for VPCs
Parameters:
AZIds:
Type: CommaDelimitedList
Description: An ordered list of the AZ zone IDs that will be used for mapping. (example default values provided for us-east-1)
Default: use1-az6,use1-az4,use1-az5
LogsRetentionInDays:
Type: Number
Description: Number of days to retain log events in CloudWatch log group
AllowedValues:
- 1
- 3
- 5
- 7
- 14
- 30
- 60
- 90
- 120
- 150
- 180
- 365
- 400
- 545
- 731
- 1827
- 3653
Resources:
LambdaIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
MaxSessionDuration: 3600
Path: /
Policies:
# Allow storing and deleting to the parameter store
- PolicyName: ParameterStorePutDelete
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:PutParameter
- ssm:DeleteParameter
Resource:
- !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/az-mapping/*"
# Allow to describe AZs to determine zone-id mapping. * is required for the resource policy
- PolicyName: DescribeAZs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ec2:DescribeAvailabilityZones
Resource:
- '*'
# Allow Lambda to write to its specific log group
- PolicyName: LambdaExecutionRoleForLogGroup
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/vpc-mappings-${AWS::AccountId}:*"
AZMappingFunction:
Type: AWS::Lambda::Function
Properties:
Description: Stores VPC mappings into parameter store
FunctionName: !Sub "vpc-mappings-${AWS::AccountId}"
Handler: index.lambda_handler
Runtime: python3.8
Role: !GetAtt LambdaIAMRole.Arn
Timeout: 5
Code:
ZipFile: |
import boto3
import cfnresponse
def lambda_handler(event, context):
azIds = event['ResourceProperties']['azIds']
if event['RequestType'] == 'Create':
create(azIds)
elif event['RequestType'] == 'Delete':
delete(azIds)
elif event['RequestType'] == 'Update':
delete(azIds)
create(azIds)
else:
print('no changes')
response_data = {}
cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data)
# Use the ordered parameter list of azIds (zone ids) to create SSM Parameters
# for the AZ Mapping. Store the resulting zone name mapping along with the zone-id
def create(azIds):
ssm = boto3.client('ssm')
azs = getAZs(azIds)
for az in azs:
azId = az.get('ZoneId')
azName = az.get('ZoneName')
# AZ Number should match the ordered zone id parameter
azNumber = azIds.index(azId) + 1
ssm.put_parameter(
Name='/az-mapping/az' + str(azNumber),
Description=azId,
Value=azName,
Type='String',
Overwrite=True,
Tier='Standard')
# Delete the az mapping parameters
def delete(azIds):
ssm = boto3.client('ssm')
azNumber=1
azs = getAZs(azIds)
for az in azs:
ssm.delete_parameter(
Name='/az-mapping/az' + str(azNumber)
)
azNumber = azNumber + 1
# Get the AZ objects that match the given zone IDs
def getAZs(azIds):
ec2c = boto3.client('ec2')
r = ec2c.describe_availability_zones(
ZoneIds=azIds
)
azs = r.get('AvailabilityZones')
return azs
## Although this log group would be otherwise created, we do it here to define the retention for cost purposes.
AZMappingFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${AZMappingFunction}
RetentionInDays: !Ref LogsRetentionInDays
InvokeAZMappingFunction:
Type: Custom::LambdaAzMappings
DependsOn: AZMappingFunctionLogGroup
Properties:
ServiceToken: !GetAtt AZMappingFunction.Arn
azIds: !Ref AZIds
Outputs:
Version:
Description: The version of the az-mapping solution.
Value: 1.0