Skip to content

Commit d018a7e

Browse files
committed
Merge branch 'multi-container' of https://github.com/awslabs/aws-saas-boost into multi-container
2 parents d576f4e + 8ade20c commit d018a7e

File tree

19 files changed

+427
-306
lines changed

19 files changed

+427
-306
lines changed

resources/custom-resources/set-instance-protection/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ limitations under the License.
3232
</license>
3333
</licenses>
3434
<properties>
35-
<checkstyle.maxAllowedViolations>4</checkstyle.maxAllowedViolations>
35+
<checkstyle.maxAllowedViolations>0</checkstyle.maxAllowedViolations>
3636
</properties>
3737

3838
<build>
@@ -88,6 +88,13 @@ limitations under the License.
8888
<!-- Don't bundle our layer so we get the shared one at runtime -->
8989
<scope>provided</scope>
9090
</dependency>
91+
<dependency>
92+
<groupId>com.amazon.aws.partners.saasfactory.saasboost</groupId>
93+
<artifactId>CloudFormationUtils</artifactId>
94+
<version>1.0.0</version>
95+
<!-- Don't bundle our layer so we get the shared one at runtime -->
96+
<scope>provided</scope>
97+
</dependency>
9198
<dependency>
9299
<groupId>software.amazon.awssdk</groupId>
93100
<artifactId>autoscaling</artifactId>

resources/custom-resources/set-instance-protection/src/main/java/com/amazon/aws/partners/saasfactory/saasboost/SetInstanceProtection.java

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818

1919
import com.amazonaws.services.lambda.runtime.Context;
2020
import com.amazonaws.services.lambda.runtime.RequestHandler;
21-
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
22-
import com.fasterxml.jackson.databind.node.ObjectNode;
2321
import org.slf4j.Logger;
2422
import org.slf4j.LoggerFactory;
2523
import software.amazon.awssdk.services.autoscaling.AutoScalingClient;
2624
import software.amazon.awssdk.services.autoscaling.model.*;
2725

28-
import java.io.IOException;
29-
import java.io.OutputStreamWriter;
30-
import java.net.HttpURLConnection;
31-
import java.net.URL;
3226
import java.util.*;
3327
import java.util.concurrent.*;
3428

@@ -38,10 +32,8 @@ public class SetInstanceProtection implements RequestHandler<Map<String, Object>
3832
private final AutoScalingClient autoScaling;
3933

4034
public SetInstanceProtection() {
41-
long startTimeMillis = System.currentTimeMillis();
4235
LOGGER.info("Version Info: {}", Utils.version(this.getClass()));
4336
autoScaling = Utils.sdkClient(AutoScalingClient.builder(), AutoScalingClient.SERVICE_NAME);
44-
LOGGER.info("Constructor init: {}", System.currentTimeMillis() - startTimeMillis);
4537
}
4638

4739
@Override
@@ -53,8 +45,9 @@ public Object handleRequest(Map<String, Object> event, Context context) {
5345
final String autoScalingGroup = (String) resourceProperties.get("AutoScalingGroup");
5446
final Boolean enableInstanceProtection = Boolean.valueOf((String) resourceProperties.get("Enable"));
5547
ExecutorService service = Executors.newSingleThreadExecutor();
56-
ObjectNode responseData = JsonNodeFactory.instance.objectNode();
57-
LOGGER.info("Setting instance protection to {} for Autoscaling group {}", enableInstanceProtection, autoScalingGroup);
48+
Map<String, Object> responseData = new HashMap<>();
49+
LOGGER.info("Setting instance protection to {} for Autoscaling group {}", enableInstanceProtection,
50+
autoScalingGroup);
5851
try {
5952
Runnable r = () -> {
6053
if ("Delete".equalsIgnoreCase(requestType) || "Update".equalsIgnoreCase(requestType)) {
@@ -64,7 +57,8 @@ public Object handleRequest(Map<String, Object> event, Context context) {
6457
request.autoScalingGroupNames(autoScalingGroup)
6558
);
6659
if (response.hasAutoScalingGroups()) {
67-
LOGGER.info("Auto scaling found {} groups for {}", response.autoScalingGroups().size(), autoScalingGroup);
60+
LOGGER.info("AutoScaling found {} groups for {}", response.autoScalingGroups().size(),
61+
autoScalingGroup);
6862
if (!response.autoScalingGroups().isEmpty()) {
6963
AutoScalingGroup asgGroup = response.autoScalingGroups().get(0);
7064
List<String> instancesToUpdate = new ArrayList<>();
@@ -75,32 +69,34 @@ public Object handleRequest(Map<String, Object> event, Context context) {
7569
.protectedFromScaleIn(enableInstanceProtection)
7670
.autoScalingGroupName(autoScalingGroup)
7771
);
78-
LOGGER.info("Disabled instance protection on {} instances.", instancesToUpdate.size());
72+
LOGGER.info("{} instance protection on {} instances.",
73+
((enableInstanceProtection) ? "Enabled" : "Disabled"),
74+
instancesToUpdate.size()
75+
);
76+
CloudFormationResponse.send(event, context, "SUCCESS", responseData);
7977
} catch (AutoScalingException e) {
8078
LOGGER.error("autoscaling:SetInstanceProtection error", e);
8179
LOGGER.error(Utils.getFullStackTrace(e));
82-
responseData.put("Reason", "Error " + e.getMessage());
83-
sendResponse(event, context, "FAILED", responseData);
80+
responseData.put("Reason", e.getMessage());
81+
CloudFormationResponse.send(event, context, "FAILED", responseData);
8482
}
8583
} else {
8684
LOGGER.info("No auto scaling groups matched.");
8785
}
8886
}
8987
} catch (AutoScalingException e) {
90-
LOGGER.error("DisableInstanceProtection::Error " + e.getMessage());
88+
LOGGER.error("autoscaling:describeAutoScalingGroups error", e);
9189
LOGGER.error(Utils.getFullStackTrace(e));
92-
responseData.put("Reason", "Error " + e.getMessage());
93-
sendResponse(event, context, "FAILED", responseData);
90+
responseData.put("Reason", e.getMessage());
91+
CloudFormationResponse.send(event, context, "FAILED", responseData);
9492
}
95-
LOGGER.info("responseDate: " + Utils.toJson(responseData));
96-
sendResponse(event, context, "SUCCESS", responseData);
9793
} else if ("Create".equalsIgnoreCase(requestType)) {
9894
LOGGER.info("CREATE");
99-
sendResponse(event, context, "SUCCESS", responseData);
95+
CloudFormationResponse.send(event, context, "SUCCESS", responseData);
10096
} else {
101-
LOGGER.error("FAILED unknown requestType " + requestType);
97+
LOGGER.error("FAILED unknown requestType {}", requestType);
10298
responseData.put("Reason", "Unknown RequestType " + requestType);
103-
sendResponse(event, context, "FAILED", responseData);
99+
CloudFormationResponse.send(event, context, "FAILED", responseData);
104100
}
105101
};
106102
Future<?> f = service.submit(r);
@@ -111,52 +107,11 @@ public Object handleRequest(Map<String, Object> event, Context context) {
111107
String stackTrace = Utils.getFullStackTrace(e);
112108
LOGGER.error(stackTrace);
113109
responseData.put("Reason", stackTrace);
114-
sendResponse(event, context, "FAILED", responseData);
110+
CloudFormationResponse.send(event, context, "FAILED", responseData);
115111
} finally {
116112
service.shutdown();
117113
}
118114
return null;
119115
}
120116

121-
public final Object sendResponse(final Map<String, Object> event, final Context context, final String responseStatus, ObjectNode responseData) {
122-
String responseUrl = (String) event.get("ResponseURL");
123-
LOGGER.info("ResponseURL: {}", responseUrl);
124-
125-
try {
126-
URL url = new URL(responseUrl);
127-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
128-
connection.setDoOutput(true);
129-
connection.setRequestProperty("Content-Type", "");
130-
connection.setRequestMethod("PUT");
131-
132-
ObjectNode responseBody = JsonNodeFactory.instance.objectNode();
133-
responseBody.put("Status", responseStatus);
134-
responseBody.put("RequestId", (String) event.get("RequestId"));
135-
responseBody.put("LogicalResourceId", (String) event.get("LogicalResourceId"));
136-
responseBody.put("StackId", (String) event.get("StackId"));
137-
responseBody.put("PhysicalResourceId", (String) event.get("LogicalResourceId"));
138-
if (!"FAILED".equals(responseStatus)) {
139-
responseBody.set("Data", responseData);
140-
} else {
141-
responseBody.put("Reason", responseData.get("Reason").asText());
142-
}
143-
LOGGER.info("Response Body: " + responseBody.toString());
144-
145-
try (OutputStreamWriter response = new OutputStreamWriter(connection.getOutputStream())) {
146-
response.write(responseBody.toString());
147-
} catch (IOException ioe) {
148-
LOGGER.error("Failed to call back to CFN response URL");
149-
LOGGER.error(Utils.getFullStackTrace(ioe));
150-
}
151-
152-
LOGGER.info("Response Code: {}", connection.getResponseCode());
153-
connection.disconnect();
154-
} catch (IOException e) {
155-
LOGGER.error("Failed to open connection to CFN response URL");
156-
LOGGER.error(Utils.getFullStackTrace(e));
157-
}
158-
159-
return null;
160-
}
161-
162117
}

resources/saas-boost-core.yaml

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Resources:
134134
- Effect: Allow
135135
Action:
136136
- sts:AssumeRole
137-
Resource: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
137+
Resource: !GetAtt SaaSBoostSystemRole.Arn
138138
WorkloadDeployLambda:
139139
Type: AWS::Lambda::Function
140140
DependsOn: WorkloadDeployLogs
@@ -154,10 +154,11 @@ Resources:
154154
Environment:
155155
Variables:
156156
SAAS_BOOST_ENV: !Ref Environment
157-
API_TRUST_ROLE: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
157+
API_TRUST_ROLE: !GetAtt SaaSBoostSystemRole.Arn
158158
API_GATEWAY_HOST: !Sub ${SaaSBoostPrivateApi}.execute-api.${AWS::Region}.amazonaws.com
159159
API_GATEWAY_STAGE: !Ref PrivateApiStage
160160
CODE_PIPELINE_BUCKET: !Ref CodePipelineBucket
161+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
161162
Tags:
162163
- Key: "Application"
163164
Value: "SaaSBoost"
@@ -257,6 +258,7 @@ Resources:
257258
Variables:
258259
SAAS_BOOST_ENV: !Ref Environment
259260
SAAS_BOOST_EVENT_BUS: !Sub '{{resolve:ssm:/saas-boost/${Environment}/EVENT_BUS}}'
261+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
260262
Tags:
261263
- Key: "Application"
262264
Value: "SaaSBoost"
@@ -314,6 +316,7 @@ Resources:
314316
Variables:
315317
SAAS_BOOST_ENV: !Ref Environment
316318
SAAS_BOOST_EVENT_BUS: !Sub '{{resolve:ssm:/saas-boost/${Environment}/EVENT_BUS}}'
319+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
317320
Tags:
318321
- Key: "Application"
319322
Value: "SaaSBoost"
@@ -469,6 +472,9 @@ Resources:
469472
S3Key: !Sub ${LambdaSourceFolder}/EcsServiceUpdate-lambda.zip
470473
Layers:
471474
- !Ref SaaSBoostUtilsLayer
475+
Environment:
476+
Variables:
477+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
472478
Tags:
473479
- Key: "Application"
474480
Value: "SaaSBoost"
@@ -496,7 +502,7 @@ Resources:
496502
- SaaSBoostPublicApi
497503
- SaaSBoostPrivateApi
498504
Properties:
499-
RoleName: !Sub sb-private-api-trust-role-${Environment}-${AWS::Region}
505+
RoleName: !Sub sb-${Environment}-private-api-trust-role-${AWS::Region}
500506
Path: '/'
501507
AssumeRolePolicyDocument:
502508
Version: 2012-10-17
@@ -507,7 +513,7 @@ Resources:
507513
Action:
508514
- sts:AssumeRole
509515
Policies:
510-
- PolicyName: !Sub sb-private-api-trust-policy-${Environment}-${AWS::Region}
516+
- PolicyName: !Sub sb-${Environment}-private-api-trust-policy-${AWS::Region}
511517
PolicyDocument:
512518
Version: 2012-10-17
513519
Statement:
@@ -517,10 +523,16 @@ Resources:
517523
Resource:
518524
- !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SaaSBoostPrivateApi}/*/*/*
519525
- !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SaaSBoostPublicApi}/*/*/*
526+
SSMParamPrivateApiRole:
527+
Type: AWS::SSM::Parameter
528+
Properties:
529+
Name: !Sub /saas-boost/${Environment}/PRIVATE_API_TRUST_ROLE
530+
Type: String
531+
Value: !GetAtt SaaSBoostSystemRole.Arn
520532
SystemRestClientExecRole:
521533
Type: AWS::IAM::Role
522534
Properties:
523-
RoleName: !Sub sb-private-api-client-role-${Environment}-${AWS::Region}
535+
RoleName: !Sub sb-${Environment}-private-api-client-role-${AWS::Region}
524536
Path: '/'
525537
AssumeRolePolicyDocument:
526538
Version: 2012-10-17
@@ -532,7 +544,7 @@ Resources:
532544
Action:
533545
- sts:AssumeRole
534546
Policies:
535-
- PolicyName: !Sub sb-private-api-client-policy-${Environment}-${AWS::Region}
547+
- PolicyName: !Sub sb-${Environment}-private-api-client-policy-${AWS::Region}
536548
PolicyDocument:
537549
Version: 2012-10-17
538550
Statement:
@@ -550,7 +562,7 @@ Resources:
550562
- Effect: Allow
551563
Action:
552564
- sts:AssumeRole
553-
Resource: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
565+
Resource: !GetAtt SaaSBoostSystemRole.Arn
554566
SystemRestApiClientLogs:
555567
Type: AWS::Logs::LogGroup
556568
Properties:
@@ -574,9 +586,10 @@ Resources:
574586
Environment:
575587
Variables:
576588
SAAS_BOOST_ENV: !Ref Environment
577-
API_TRUST_ROLE: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
589+
API_TRUST_ROLE: !GetAtt SaaSBoostSystemRole.Arn
578590
API_GATEWAY_HOST: !Sub ${SaaSBoostPrivateApi}.execute-api.${AWS::Region}.amazonaws.com
579591
API_GATEWAY_STAGE: !Ref PrivateApiStage
592+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
580593
Tags:
581594
- Key: "Application"
582595
Value: "SaaSBoost"
@@ -644,7 +657,7 @@ Resources:
644657
- Effect: Allow
645658
Action:
646659
- sts:AssumeRole
647-
Resource: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
660+
Resource: !GetAtt SaaSBoostSystemRole.Arn
648661
EcsShutdownServicesLogs:
649662
Type: AWS::Logs::LogGroup
650663
Properties:
@@ -669,9 +682,10 @@ Resources:
669682
Environment:
670683
Variables:
671684
SAAS_BOOST_ENV: !Ref Environment
672-
API_TRUST_ROLE: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
685+
API_TRUST_ROLE: !GetAtt SaaSBoostSystemRole.Arn
673686
API_GATEWAY_HOST: !Sub ${SaaSBoostPrivateApi}.execute-api.${AWS::Region}.amazonaws.com
674687
API_GATEWAY_STAGE: !Ref PrivateApiStage
688+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
675689
Tags:
676690
- Key: "Application"
677691
Value: "SaaSBoost"
@@ -720,7 +734,7 @@ Resources:
720734
- Effect: Allow
721735
Action:
722736
- sts:AssumeRole
723-
Resource: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
737+
Resource: !GetAtt SaaSBoostSystemRole.Arn
724738
EcsStartupServicesLogs:
725739
Type: AWS::Logs::LogGroup
726740
Properties:
@@ -745,9 +759,10 @@ Resources:
745759
Environment:
746760
Variables:
747761
SAAS_BOOST_ENV: !Ref Environment
748-
API_TRUST_ROLE: !Sub arn:aws:iam::${AWS::AccountId}:role/sb-private-api-trust-role-${Environment}-${AWS::Region}
762+
API_TRUST_ROLE: !GetAtt SaaSBoostSystemRole.Arn
749763
API_GATEWAY_HOST: !Sub ${SaaSBoostPrivateApi}.execute-api.${AWS::Region}.amazonaws.com
750764
API_GATEWAY_STAGE: !Ref PrivateApiStage
765+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
751766
Tags:
752767
- Key: "Application"
753768
Value: "SaaSBoost"
@@ -794,7 +809,7 @@ Resources:
794809
- Effect: Allow
795810
Action:
796811
- autoscaling:SetInstanceProtection
797-
Resource: !Sub arn:aws:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/tenant-*
812+
Resource: !Sub arn:aws:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/sb-${Environment}-tenant-*
798813
SetInstanceProtectionLogs:
799814
Type: AWS::Logs::LogGroup
800815
Properties:
@@ -816,6 +831,10 @@ Resources:
816831
S3Key: !Sub ${LambdaSourceFolder}/SetInstanceProtection-lambda.zip
817832
Layers:
818833
- !Ref SaaSBoostUtilsLayer
834+
- !Ref CloudFormationUtilsLayer
835+
Environment:
836+
Variables:
837+
JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
819838
Tags:
820839
- Key: "Application"
821840
Value: "SaaSBoost"
@@ -826,9 +845,6 @@ Resources:
826845
# Macro transform will add as many AWS::ECR::Repository resources as necessary
827846
# based on the length of the list of ApplicationServices passed as a parameter
828847
Outputs:
829-
CodePipelineIamRole:
830-
Description: SaaS Boost tenant deploy CodePipeline IAM role
831-
Value: !GetAtt TenantCodePipelineRole.Arn
832848
SaaSBoostPublicApi:
833849
Description: SaaS Boost Public API
834850
Value: !Ref SaaSBoostPublicApi

0 commit comments

Comments
 (0)