public async Task <CloudFormationResponse> FunctionHandler(CloudFormationEvent <StartCodeBuild> @event, ILambdaContext context)
        {
            var json = JsonConvert.SerializeObject(@event, Formatting.Indented);

            context.Logger.LogLine(json);
            object data;

            try
            {
                switch (@event.RequestType)
                {
                case RequestType.Create:
                    data = await Create(@event, context);

                    break;

                case RequestType.Update:
                    data = await Update(@event, context);

                    break;

                case RequestType.Delete:
                    data = await Delete(@event, context);

                    break;
                }

                return(await CloudFormationResponse.CompleteCloudFormationResponse(new { SomeData = "some data" }, @event, context));
            }
            catch (Exception ex)
            {
                return(await CloudFormationResponse.CompleteCloudFormationResponse(new { Error = ex.ToString() }, @event, context));
            }
        }
Esempio n. 2
0
        public void TestCloudformationCustomEventLambdaFunction()
        {
            var json = File.ReadAllText(@"SampleTestData\CloudformationCustomResourceEvent1.json");
            var cloudformationCustomResourceEvent = JsonConvert.DeserializeObject <LoadMasterDataCustomCloudformationEvent>(json);
            var context = new TestLambdaContext();

            // Invoke the lambda function and confirm the content type was returned.
            StartupProgram         lambdaObject = new StartupProgram(true);
            CloudFormationResponse response     = lambdaObject.LoadMasterData(cloudformationCustomResourceEvent, context);

            Assert.Equal(Constants.CloudformationSuccessCode, response.Status);
        }
Esempio n. 3
0
        public async Task <bool> PostToS3Async(String presignedUrl, CloudFormationResponse data)
        {
            String        jsonData = JObject.FromObject(data).ToString(Newtonsoft.Json.Formatting.None);
            StringContent val      = new StringContent(jsonData);

            val.Headers.Clear();
            val.Headers.TryAddWithoutValidation("content-type", "");
            val.Headers.TryAddWithoutValidation("content-length", jsonData.Length.ToString());

            var client = new HttpClient();

            client.DefaultRequestHeaders.Clear();

            await client.PutAsync(presignedUrl, val);

            return(true);
        }
        public string Send(JObject input, ILambdaContext context, OpsStatus Status, Object data)
        {
            CloudFormationResponse cf = new CloudFormationResponse();

            cf.Status             = Status == OpsStatus.Success?"SUCCESS":"FAILED"; //Values should be either SUCCESS or FAILED
            cf.PhysicalResourceId = context.LogStreamName;
            cf.StackId            = input["StackId"].ToString();
            cf.RequestId          = input["RequestId"].ToString();
            cf.LogicalResourceId  = input["LogicalResourceId"].ToString();
            cf.Data = data;

            Console.WriteLine(JObject.FromObject(cf).ToString(Newtonsoft.Json.Formatting.None));

            var t = PostToS3Async(input["ResponseURL"].ToString(), cf);

            t.Wait();
            return(t.Result.ToString());
        }
Esempio n. 5
0
        private string Notify(string response, DataResponse data, JObject input, ILambdaContext context)
        {
            string responseUrl       = input["ResponseURL"].ToString();
            string stackId           = input["StackId"].ToString();
            string requestId         = input["RequestId"].ToString();
            string logicalResourceId = input["LogicalResourceId"].ToString();

            CloudFormationResponse cf = new CloudFormationResponse();

            cf.Status             = response; //Values should be either SUCCESS or FAILED
            cf.PhysicalResourceId = context.LogStreamName;
            cf.StackId            = stackId;
            cf.RequestId          = requestId;
            cf.LogicalResourceId  = logicalResourceId;

            //This can be the dataset you wish to return
            cf.Data = data;
            Console.WriteLine(JObject.FromObject(cf).ToString(Newtonsoft.Json.Formatting.Indented));

            var t = PostToS3Async(responseUrl, cf);

            t.Wait();
            return(t.Result.ToString());
        }
        public CloudFormationResponse LoadMasterData(LoadMasterDataCustomCloudformationEvent request, ILambdaContext context)
        {
            try
            {
                string UniqueIdGenerated  = SecurityHelper.GetSha256Hash($"{request.StackId}:{request.LogicalResourceId}");
                DynamoDBMasterItem1 item1 = new DynamoDBMasterItem1(
                    UniqueIdGenerated,
                    Convert.ToString(Guid.NewGuid()),
                    "Rohit Srivastava",
                    "Senior Consultant",
                    "29",
                    "Advisory");

                context.Logger.LogLine($"Input event invoked: {JsonConvert.SerializeObject(request)}");
                DynamoDBHelper dynamoDBHelper = new DynamoDBHelper(context, this.isLocalDebug);

                context.Logger.LogLine($"Custom cloudformation event request type: {request.RequestType}");
                //Since this is not basically creation of any custom resource , rather initial data load , we donot need
                //to care about anything other than the CREATE request type
                if (string.Equals(request.RequestType, Constants.CloudFormationCreateRequestType))
                {
                    dynamoDBHelper.putItemTable1(item1, request.ResourceProperties.TableName);

                    //Success - data inserted properly in the dynamoDB
                    CloudFormationResponse objResponse =
                        new CloudFormationResponse(
                            Constants.CloudformationSuccessCode,
                            "Custom Resource Creation Successful",
                            $"{request.StackId}-{request.LogicalResourceId}-DataLoad",
                            request.StackId,
                            request.RequestId,
                            request.LogicalResourceId,
                            item1
                            );

                    return(objResponse.CompleteCloudFormationResponse(request, context).GetAwaiter().GetResult());
                }
                else
                {
                    CloudFormationResponse objResponse =
                        new CloudFormationResponse(
                            Constants.CloudformationSuccessCode,
                            "Do nothing.Data will be pushed in only when stack event is Create",
                            context.LogStreamName,
                            request.StackId,
                            request.RequestId,
                            request.LogicalResourceId,
                            null
                            );
                    return(objResponse.CompleteCloudFormationResponse(request, context).GetAwaiter().GetResult());
                }
            }
            catch (Exception ex)
            {
                context.Logger.LogLine($"StartupProgram::LoadMasterData => {ex.Message}");
                context.Logger.LogLine($"StartupProgram::LoadMasterData => {ex.StackTrace}");

                //Error - log it into the cloudformation console
                CloudFormationResponse objResponse =
                    new CloudFormationResponse(
                        Constants.CloudformationErrorCode,
                        ex.Message,
                        context.LogStreamName,
                        request.StackId,
                        request.RequestId,
                        request.LogicalResourceId,
                        null
                        );

                return(objResponse.CompleteCloudFormationResponse(request, context).GetAwaiter().GetResult());
            }
        }