async Task <Immutable <CfnResponse> > CreateSeed(CfnRequest cfnRequest, Immutable <CfnResponse> cfnResponse, ILambdaContext context) { try { var seedData = cfnRequest.ResourceProperties.SeedData; if (seedData == null || seedData.Count() == 0 || (seedData.Count() == 1 && string.IsNullOrEmpty(seedData.First()))) { return(cfnResponse); } var request = seedData.Select(seed => { return(new SaveItemRequest { Id = "U1", Name = "This is a custom data inserted using a custom resource", Key = seed }); }); var result = await _itemService.BatchWrite(request); if (result != null) { LogHandler.LogMessage(context, "Db seeding successful"); return(cfnResponse.Modify(r => r.Status = "SUCCESS")); } else { LogHandler.LogMessage(context, "Db seeding failed"); return(cfnResponse.ToBuilder() .Modify(r => r.Status = "FAILED") .Modify(r => r.Reason = $"Resource: {_tableName} not found") .ToImmutable()); } } catch (Exception ex) { LogHandler.LogMessage(context, ex.StackTrace); return(cfnResponse.ToBuilder() .Modify(r => r.Status = "FAILED") .Modify(r => r.Reason = ex.Message) .ToImmutable()); } }
async Task <Immutable <CfnResponse> > DeleteTable(CfnRequest cfnRequest, Immutable <CfnResponse> cfnResponse, ILambdaContext context) { try { await _itemService.DeleteTable(); await _userService.DeleteTable(); LogHandler.LogMessage(context, $"Request {cfnRequest.RequestType} executed Successfully"); return(cfnResponse.Modify(r => r.Status = "SUCCESS")); } catch (Exception ex) { LogHandler.LogMessage(context, $"Message: {ex.Message} /Trace: {ex.StackTrace}"); return(cfnResponse.ToBuilder() .Modify(r => r.Status = "FAILED") .Modify(r => r.Reason = ex.Message) .ToImmutable()); } }
// Explicit call to cfn ResponseURL to confirm creation of custom resource void SendResponseToCfn(CfnRequest cfnRequest, Immutable <CfnResponse> cfnResponse) { var serializerSettings = new JsonSerializerSettings(); serializerSettings.Converters.Add(new ImmutableJsonConverter()); string json = JsonConvert.SerializeObject(cfnResponse, serializerSettings); byte[] byteArray = Encoding.UTF8.GetBytes(json); var httpRequest = WebRequest.Create(cfnRequest.ResponseURL) as HttpWebRequest; httpRequest.Method = "PUT"; httpRequest.ContentType = ""; httpRequest.ContentLength = byteArray.Length; using (Stream datastream = httpRequest.GetRequestStream()) { datastream.Write(byteArray, 0, byteArray.Length); } httpRequest.GetResponse(); }