/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public void FunctionHandler(JObject input, ILambdaContext context) { LambdaLogger.Log($"Received input as {input.ToString()}"); var request = input.ToObject <cfnRequest>(); var response = new cfnResponse(); //build all the common responses from the request response.StackId = request.stackId; response.RequestId = request.requestId; response.LogicalResourceId = request.logicalResourceId; response.NoEcho = false; response.PhysicalResourceId = "My_custom_physical_resource_id"; response.Reason = "See CloudWatch logs for detail"; // Sample - gather custom properties from the CloudFormation custom resource var resourceProperties = request.resourceProperties.ToObject <ResourceProperties>(); var awsregion = resourceProperties.Region; var serviceToken = resourceProperties.ServiceToken; switch (request.resourceType.ToLower()) { case "create": LambdaLogger.Log("Received Create request"); // Sample: do some stuff, then set the response.Status field response.Status = "SUCCESS"; break; case "delete": LambdaLogger.Log("Received Create request"); response.Status = "SUCCESS"; break; case "update": LambdaLogger.Log("Received Create request"); response.Status = "SUCCESS"; break; } LambdaLogger.Log($"Uploading response to {request.responseUrl} "); Uploader.UploadResponse(request.responseUrl, response); LambdaLogger.Log("Finished"); }
// Class to upload response JSON to the pre-signed URL public static void UploadResponse(string url, cfnResponse response) { string json = JsonConvert.SerializeObject(response); byte[] byteArray = Encoding.UTF8.GetBytes(json); LambdaLogger.Log($"trying to upload json {json}"); HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest; httpRequest.Method = "PUT"; //If ContentType is set to anything but "" your upload will fail httpRequest.ContentType = ""; httpRequest.ContentLength = byteArray.Length; LambdaLogger.Log($"Starting upload of {byteArray.Length}"); using (Stream datastream = httpRequest.GetRequestStream()) { datastream.Write(byteArray, 0, byteArray.Length); } HttpWebResponse result = httpRequest.GetResponse() as HttpWebResponse; LambdaLogger.Log($"Result of upload is {result.StatusCode}"); }