Skip to content

Commit dbb7804

Browse files
committed
Make settings service more event based for communication with onboarding
Fix hosted zone bug by reusing an existing hosted zone for the domain name.
1 parent d3d1038 commit dbb7804

File tree

19 files changed

+607
-1458
lines changed

19 files changed

+607
-1458
lines changed

functions/core-stack-listener/src/main/java/com/amazon/aws/partners/saasfactory/saasboost/CoreStackListener.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,37 +78,38 @@ public Object handleRequest(SNSEvent event, Context context) {
7878
ListStackResourcesResponse resources = cfn.listStackResources(req -> req
7979
.stackName(cloudFormationEvent.getStackId())
8080
);
81+
Map<String, Object> appConfig = new HashMap<>();
82+
Map<String, Object> services = new HashMap<>();
8183
for (StackResourceSummary resource : resources.stackResourceSummaries()) {
8284
// LOGGER.debug("Processing resource {} {} {} {}", resource.resourceType(),
8385
// resource.resourceStatusAsString(), resource.logicalResourceId(),
8486
// resource.physicalResourceId());
85-
if ("CREATE_COMPLETE".equals(resource.resourceStatusAsString())) {
86-
if ("AWS::ECR::Repository".equals(resource.resourceType())) {
87-
String ecrRepo = resource.physicalResourceId();
88-
String serviceName = resource.logicalResourceId();
89-
LOGGER.info("Publishing appConfig update event for ECR repository {} {}", serviceName,
90-
ecrRepo);
91-
Map<String, Object> systemApiRequest = new HashMap<>();
92-
systemApiRequest.put("resource", "settings/config/" + serviceName + "/ECR_REPO");
93-
systemApiRequest.put("method", "PUT");
94-
systemApiRequest.put("body", Utils.toJson(Map.of("value", ecrRepo)));
95-
Utils.publishEvent(eventBridge, SAAS_BOOST_EVENT_BUS, EVENT_SOURCE, SYSTEM_API_CALL,
96-
systemApiRequest);
97-
} else if ("AWS::Route53::HostedZone".equals(resource.resourceType())) {
98-
// Make this an event vs directly calling the Settings Service API because when this
99-
// CloudFormation stack first completes, the Settings Service may not even exist yet
100-
// Could also look at matching against UPDATE_COMPLETE
101-
// String hostedZoneId = resource.physicalResourceId();
102-
// LOGGER.info("Publishing appConfig update event for Route53 hosted zone {}", hostedZoneId);
103-
// Map<String, Object> systemApiRequest = new HashMap<>();
104-
// systemApiRequest.put("resource", "settings/HOSTED_ZONE");
105-
// systemApiRequest.put("method", "PUT");
106-
// //systemApiRequest.put("body", Utils.toJson(Map.of("value", ecrRepo)));
107-
// Utils.publishEvent(eventBridge, SAAS_BOOST_EVENT_BUS, EVENT_SOURCE, SYSTEM_API_CALL,
108-
// systemApiRequest);
87+
if ("CREATE_COMPLETE".equals(resource.resourceStatusAsString())
88+
&& "AWS::ECR::Repository".equals(resource.resourceType())) {
89+
String ecrRepo = resource.physicalResourceId();
90+
String serviceName = resource.logicalResourceId();
91+
LOGGER.info("Publishing appConfig update event for ECR repository {} {}", serviceName,
92+
ecrRepo);
93+
services.put(serviceName, Map.of("containerRepo", ecrRepo));
94+
} else if ("CREATE_COMPLETE".equals(resource.resourceStatusAsString())
95+
|| "UPDATE_COMPLETE".equals(resource.resourceStatusAsString())) {
96+
if ("AWS::Route53::HostedZone".equals(resource.resourceType())) {
97+
// When CloudFormation stack first completes, the Settings Service won't even exist yet.
98+
String hostedZoneId = resource.physicalResourceId();
99+
LOGGER.info("Publishing appConfig update event for Route53 hosted zone {}", hostedZoneId);
100+
appConfig.put("hostedZone", hostedZoneId);
109101
}
110102
}
111103
}
104+
// Only fire one event for all the app config resources changes by this stack
105+
if (!services.isEmpty()) {
106+
appConfig.put("services", services);
107+
}
108+
if (!appConfig.isEmpty()) {
109+
Utils.publishEvent(eventBridge, SAAS_BOOST_EVENT_BUS, EVENT_SOURCE,
110+
"Application Configuration Resource Changed",
111+
appConfig);
112+
}
112113
} catch (SdkServiceException cfnError) {
113114
LOGGER.error("cfn:ListStackResources error", cfnError);
114115
LOGGER.error(Utils.getFullStackTrace(cfnError));
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License").
5+
# You may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
if [ -z $1 ]; then
17+
echo "Usage: $0 <Environment> [Lambda Folder]"
18+
exit 2
19+
fi
20+
21+
MY_AWS_REGION=$(aws configure list | grep region | awk '{print $2}')
22+
echo "AWS Region = $MY_AWS_REGION"
23+
24+
ENVIRONMENT=$1
25+
LAMBDA_STAGE_FOLDER=$2
26+
if [ -z $LAMBDA_STAGE_FOLDER ]; then
27+
LAMBDA_STAGE_FOLDER="lambdas"
28+
fi
29+
LAMBDA_CODE=CoreStackListener-lambda.zip
30+
31+
#set this for V2 AWS CLI to disable paging
32+
export AWS_PAGER=""
33+
34+
SAAS_BOOST_BUCKET=$(aws --region $MY_AWS_REGION ssm get-parameter --name "/saas-boost/${ENVIRONMENT}/SAAS_BOOST_BUCKET" --query 'Parameter.Value' --output text)
35+
echo "SaaS Boost Bucket = $SAAS_BOOST_BUCKET"
36+
if [ -z $SAAS_BOOST_BUCKET ]; then
37+
echo "Can't find SAAS_BOOST_BUCKET in Parameter Store"
38+
exit 1
39+
fi
40+
41+
# Do a fresh build of the project
42+
mvn
43+
if [ $? -ne 0 ]; then
44+
echo "Error building project"
45+
exit 1
46+
fi
47+
48+
# And copy it up to S3
49+
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/
50+
51+
FUNCTIONS=("sb-${ENVIRONMENT}-core-stack-listener"
52+
)
53+
54+
for FUNCTION in ${FUNCTIONS[@]}; do
55+
#echo $FUNCTION
56+
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
57+
done

resources/saas-boost-core.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@ Parameters:
5454
Description: The domain your workload is hosted at formatted as [env].[domain].[com]
5555
Type: String
5656
Default: ''
57+
HostedZone:
58+
Description: The existing Route53 hosted zone id for the domain name
59+
Type: String
60+
Default: ''
5761
Conditions:
58-
HasDomainName: !Not [!Equals [!Ref DomainName, '']]
62+
CreateHostedZone: !And
63+
- !Not [!Equals [!Ref DomainName, '']]
64+
- !Equals [!Ref HostedZone, '']
5965
Resources:
6066
# Route 53 hosted zone. This hosted zone's name servers will need to be added to primary
61-
HostedZone:
67+
PublicDomainHostedZone:
6268
Type: AWS::Route53::HostedZone
63-
Condition: HasDomainName
69+
Condition: CreateHostedZone
6470
Properties:
6571
HostedZoneConfig:
6672
Comment: !Sub ${DomainName} Public DNS zone

0 commit comments

Comments
 (0)