-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure_private_marketplace.yaml
342 lines (311 loc) · 15.5 KB
/
configure_private_marketplace.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# 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: This template creates and manages private marketplace experiences from configuration files in Amazon S3
Parameters:
ExperiencesS3BucketName:
Type: String
Description: Amazon S3 bucket that holds the configuration for private marketplace experiences
Resources:
ConfigurePrivateMarketplaceLambdaRole:
Type: AWS::IAM::Role
Properties:
Description: Lambda execution role for enabling service access for AWS Organization
Path: /
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: '2012-10-17'
Policies:
- PolicyDocument:
Statement:
- Action:
- s3:ListBucket
- S3:GetObject
- S3:PutObject
- S3:ListObjects
Effect: Allow
Resource:
- !Sub arn:aws:s3:::${ExperiencesS3BucketName}
- !Sub arn:aws:s3:::${ExperiencesS3BucketName}/*
Version: '2012-10-17'
PolicyName: !Join
- '-'
- - !Ref 'AWS::StackName'
- PMPS3EnablementLambdas3Policy
- PolicyDocument:
Statement:
- Action:
- aws-marketplace:StartChangeSet
- aws-marketplace:DescribeChangeSet
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: !Join
- '-'
- - !Ref 'AWS::StackName'
- PMPMPEnablementLambdampPolicy
ManagedPolicyArns:
- !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AsyncConfigurePrivateMarketplaceLambda:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Role: !GetAtt 'ConfigurePrivateMarketplaceLambdaRole.Arn'
Timeout: 900
ReservedConcurrentExecutions: 3
Code:
ZipFile: |
import sys
import os
import time
import json
import boto3
import urllib.parse
REGION = 'us-east-1'
MARKETPLACE_CATALOG = 'AWSMarketplace'
PMP_S3_BUCKET = os.environ['PMP_S3_BUCKET']
EXPERIENCE_FILE_NAME_CHAR = '#'
PMP_STATUS_FILE_PREFIX = 'STATUS_'
CHANGESET_ID= 'ChangeSetId'
EXPERIENCE_ID = "Experience Id"
CHANGESET_IN_PROGRESS = 'In Progress'
CHANGESET_STATUS = 'ChangeSetStatus'
ERROR_MESSAGE = 'Error Message'
EXPERIENCE_NAME = "Name"
EXPERIENCE_ASSOCIATE_PRINCIPALS = "AssociatePrincipals"
EXPERIENCE_DISASSOCIATE_PRINCIPALS = "DisassociatePrincipals"
EXPERIENCE_ALLOW_PRODUCTS = "AllowProducts"
EXPERIENCE_DENY_PRODUCTS = "DenyProducts"
EXPERIENCE_STATUS = "Status"
EXPERIENCE_POLICY_RESOURCE_REQUESTS = "PolicyResourceRequests"
EXPERIENCE_CREATION_ENTITY_ID = "$ExperienceCreationChange.Entity.Identifier"
s3_client = boto3.client('s3')
mp_client = boto3.client('marketplace-catalog', region_name = REGION)
def lambda_handler(event, context):
try:
print("Entering AsyncConfigurePrivateMarketplaceLambda handler")
# Check if event contains Records
if not event.get('Records'):
print("Empty event received. Exit!")
return
# Get the s3 file that was uploaded
if not event['Records'][0].get('s3'):
print("Event does not have s3 details. Exit!")
return
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
print("Reading configuration details from s3 bucket:", bucket " s3 file:", key)
experience_content = s3_client.get_object(
Bucket = bucket,
Key = key)['Body'].read()
details = json.loads(experience_content)
print('Configuration details:', details)
# Status file name is status_<experience_file_prefix>
experience_file_prefix = key.split(EXPERIENCE_FILE_NAME_CHAR)[0]
s3_status_file_name = PMP_STATUS_FILE_PREFIX experience_file_prefix
print("Status file:" s3_status_file_name)
status_content = '{}'
try:
status_content = s3_client.get_object(
Bucket = PMP_S3_BUCKET,
Key = s3_status_file_name)['Body'].read()
except s3_client.exceptions.NoSuchKey:
print("Status file does not exist")
json.loads(status_content)
status_details = json.loads(status_content)
# Do not invoke changeset if its already in progress
if CHANGESET_STATUS in status_details and status_details[CHANGESET_STATUS] == CHANGESET_IN_PROGRESS:
print("Changeset status is in progress. Throw an error ")
raise Exception("The changeset is already in progress for " PMP_S3_FILE ". You can rerun this once status is successful.")
# Create or manage based on whether the experience has been created already
if EXPERIENCE_ID in status_details:
manage_private_marketplace(details, status_details, s3_status_file_name)
else:
create_private_marketplace(details, status_details, s3_status_file_name)
except Exception as e:
print("ERROR: Lambda failed with an exception", e)
def build_change_set_list(details, status_details, entity_id):
changeset_list = []
if(entity_id == EXPERIENCE_CREATION_ENTITY_ID):
build_and_append_create_changesets(changeset_list, details, entity_id)
build_and_append_audience_changeset(changeset_list, details, 'AssociateAudience', EXPERIENCE_ASSOCIATE_PRINCIPALS, entity_id)
build_and_append_audience_changeset(changeset_list, details, 'DisassociateAudience', EXPERIENCE_DISASSOCIATE_PRINCIPALS, entity_id)
build_and_append_product_procurement_changeset(changeset_list, details, 'AllowProductProcurement', EXPERIENCE_ALLOW_PRODUCTS, entity_id)
build_and_append_product_procurement_changeset(changeset_list, details, 'DenyProductProcurement', EXPERIENCE_DENY_PRODUCTS, entity_id)
build_and_append_status_update_changeset(changeset_list, details, 'UpdateExperience', EXPERIENCE_STATUS, entity_id)
build_and_append_requests_update_changeset(changeset_list, details, 'UpdateProcurementPolicy', EXPERIENCE_POLICY_RESOURCE_REQUESTS, entity_id)
return changeset_list
def manage_private_marketplace(details, status_details, s3_file):
print("Updating private marketplace experience", details[EXPERIENCE_NAME])
entity_id = status_details[EXPERIENCE_ID]
changeset_list = build_change_set_list(details, status_details, entity_id)
response = mp_client.start_change_set(
Catalog=MARKETPLACE_CATALOG,
ChangeSet=changeset_list,
ChangeSetName='Private Marketplace Automation Update Experience ' details[EXPERIENCE_NAME]
)
print("Successfully invoked experience management")
write_status_to_s3(response, status_details, s3_file)
def create_private_marketplace(details, status_details, s3_file):
if not details.get(EXPERIENCE_NAME):
raise Exception("Cannot create Experience without specifying a name")
print("Creating and configuring private marketplace experience", details[EXPERIENCE_NAME])
entity_id = EXPERIENCE_CREATION_ENTITY_ID
changeset_list = build_change_set_list(details, status_details, entity_id)
response = mp_client.start_change_set(
Catalog=MARKETPLACE_CATALOG,
ChangeSet=changeset_list,
ChangeSetName='Private Marketplace Automation Create Experience ' details[EXPERIENCE_NAME]
)
print("Successfully invoked experience creation")
write_status_to_s3(response, status_details, s3_file)
def write_status_to_s3(response, status_details, s3_file):
change_set_id = response['ChangeSetId']
print("ChangeSet " change_set_id " created")
# Write Changeset status to in progress in a status file in S3 bucket
changeset_id_json = {CHANGESET_ID: change_set_id}
status_details.update(changeset_id_json)
changeset_status_update = {CHANGESET_STATUS: CHANGESET_IN_PROGRESS}
status_details.update(changeset_status_update)
# Clear any error messages from previous run
if ERROR_MESSAGE in status_details:
status_details.pop(ERROR_MESSAGE)
print("Updated details to be written are ", status_details)
s3_client.put_object(
Bucket=PMP_S3_BUCKET,
Key=s3_file,
Body=json.dumps(status_details).encode()
)
print('Updated changeset ID', change_set_id, 'in s3 file', s3_file)
def build_and_append_create_changesets(changeset_list, details, entity_id):
# CreateExperience
changeset_list.append({
'ChangeType': 'CreateExperience',
'Entity': {
'Type': '[email protected]',
},
"Details": json.dumps({
'Name': details[EXPERIENCE_NAME],
'Description': 'Private Marketplace Experience created from PMP Automation: ' details[EXPERIENCE_NAME],
}),
'ChangeName': 'ExperienceCreationChange'
})
# CreateProcurementPolicy
changeset_list.append({
'ChangeType': 'CreateProcurementPolicy',
'Details': json.dumps({
'Name': 'Procurement policy for default experience'
}),
"Entity": {
"Type": "[email protected]",
"Identifier": entity_id
},
'ChangeName': 'CreateProcurementPolicyChange'
})
def build_and_append_audience_changeset(changeset_list, details, changeset_name, details_field, entity_id):
if details.get(details_field):
changeset_list.append({
'ChangeType': changeset_name,
'Details': json.dumps({
'Principals': details[details_field]
}),
"Entity": {
"Type": "[email protected]",
"Identifier": entity_id
},
'ChangeName': changeset_name 'Change'
})
def build_and_append_product_procurement_changeset(changeset_list, details, changeset_name, details_field, entity_id):
if details.get(details_field):
changeset_list.append({
'ChangeType': changeset_name,
'Details': json.dumps({
'Products': [
{'Ids': details[details_field]}
]
}),
"Entity": {
"Type": "[email protected]",
"Identifier": entity_id
},
'ChangeName': changeset_name 'Change'
})
def build_and_append_status_update_changeset(changeset_list, details, changeset_name, details_field, entity_id):
if details.get(details_field):
changeset_list.append({
'ChangeType': changeset_name,
'Details': json.dumps({
'Status': details[details_field],
}),
'Entity': {
'Type': '[email protected]',
'Identifier': entity_id
},
'ChangeName': changeset_name 'Change'
})
def build_and_append_requests_update_changeset(changeset_list, details, changeset_name, details_field, entity_id):
if details.get(details_field):
changeset_list.append({
'ChangeType': changeset_name,
'Details': json.dumps({
"Configuration":
{
"PolicyResourceRequests": details[details_field]
}
}),
'Entity': {
'Type': '[email protected]',
'Identifier': entity_id
},
'ChangeName': changeset_name 'Change'
})
Runtime: python3.9
Environment:
Variables:
PMP_S3_BUCKET: !Ref ExperiencesS3BucketName
S3BucketForPMPConfiguration:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref ExperiencesS3BucketName
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
NotificationConfiguration:
LambdaConfigurations:
- Event: s3:ObjectCreated:Put
Filter:
S3Key:
Rules:
- Name: prefix
Value: Experience_
Function: !GetAtt AsyncConfigurePrivateMarketplaceLambda.Arn
S3LamdbaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Fn::GetAtt:
- AsyncConfigurePrivateMarketplaceLambda
- Arn
Action: lambda:InvokeFunction
Principal: s3.amazonaws.com
SourceArn:
Fn::Sub: arn:aws:s3:::${ExperiencesS3BucketName}