1818
1919import com .amazonaws .services .lambda .runtime .Context ;
2020import com .amazonaws .services .lambda .runtime .RequestHandler ;
21- import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
22- import com .fasterxml .jackson .databind .node .ObjectNode ;
2321import org .slf4j .Logger ;
2422import org .slf4j .LoggerFactory ;
2523import software .amazon .awssdk .services .autoscaling .AutoScalingClient ;
2624import 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 ;
3226import java .util .*;
3327import 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}
0 commit comments