public async Task FunctionHandlerAsync(UnpackZipIntoS3BucketRequest request, ILambdaContext context)
        {
            try
            {
                context.Logger.Log("UnpackZipIntoS3BucketFunction invoked: " + JsonConvert.SerializeObject(request));

                if (request.RequestType != "Delete")
                {
                    using (var zipStream = await S3Client.GetObjectStreamAsync(request.ResourceProperties.ZipS3Bucket, request.ResourceProperties.ZipS3Key, null))
                    {
                        using (var zipArchive = new ZipArchive(zipStream))
                        {
                            foreach (var entry in zipArchive.Entries)
                            {
                                using (var entryStream = entry.Open())
                                {
                                    using (var ms = new MemoryStream())
                                    {
                                        entryStream.CopyTo(ms);

                                        await S3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
                                        {
                                            BucketName  = request.ResourceProperties.OutputS3Bucket,
                                            Key         = Path.Combine(request.ResourceProperties.OutputPrefix ?? "", entry.FullName),
                                            InputStream = ms
                                        });
                                    }
                                }
                            }
                        }
                    }
                }

                await CloudFormationResponse.CompleteCloudFormation(null, request, context);
            }
            catch (Exception ex)
            {
                await CloudFormationResponse.FailCloudFormation(ex, request, context);

                throw;
            }
        }
Example #2
0
        public async Task FunctionHandlerAsync(InvalidateCloudFrontRequest request, ILambdaContext context)
        {
            try
            {
                context.Logger.Log("InvalidateCloudFrontFunction invoked: " + JsonConvert.SerializeObject(request));

                if (request.RequestType != "Delete")
                {
                    if (string.IsNullOrWhiteSpace(request.ResourceProperties.DistributionId))
                    {
                        throw new ArgumentException($"Missing or empty ResourceProperties.DistributionId");
                    }

                    if (request.ResourceProperties.Paths == null || !request.ResourceProperties.Paths.Any())
                    {
                        throw new ArgumentException($"Missing or empty ResourceProperties.Paths");
                    }

                    await CloudFrontClient.CreateInvalidationAsync(new CreateInvalidationRequest
                    {
                        DistributionId    = request.ResourceProperties.DistributionId,
                        InvalidationBatch = new InvalidationBatch
                        {
                            CallerReference = request.ResourceProperties.CallerReference,
                            Paths           = new Paths
                            {
                                Quantity = request.ResourceProperties.Paths.Count,
                                Items    = request.ResourceProperties.Paths
                            }
                        }
                    });
                }

                await CloudFormationResponse.CompleteCloudFormation(null, request, context);
            }
            catch (Exception ex)
            {
                await CloudFormationResponse.FailCloudFormation(ex, request, context);

                throw;
            }
        }
        private static async Task ProcessCloudFormationResponse(CloudFormationResponse response, CloudFormationRequest request, ILambdaContext context)
        {
            try
            {
                var jsonPayload = JsonConvert.SerializeObject(response);

                context.Logger.Log($"ProcessCloudFormationResponse: {jsonPayload}");

                var client      = new HttpClient();
                var jsonContent = new StringContent(jsonPayload);

                jsonContent.Headers.Remove("Content-Type");

                var postResponse = await client.PutAsync(request.ResponseURL, jsonContent);

                postResponse.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                context.Logger.Log("Exception in ProcessCloudFormationResponse: " + ex.ToString());
            }
        }
Example #4
0
        public async Task FunctionHandlerAsync(DeployLambdaEdgeRequest request, ILambdaContext context)
        {
            try
            {
                context.Logger.Log("LambdaEdgeFunction invoked: " + JsonConvert.SerializeObject(request));

                if (string.IsNullOrWhiteSpace(request.RequestType))
                {
                    throw new ArgumentException($"Missing or empty RequestType");
                }

                string lambdaVersionedArn = null;

                if (request.RequestType == "Create" || request.RequestType == "Update")
                {
                    if (string.IsNullOrWhiteSpace(request.ResourceProperties.RoleArn))
                    {
                        throw new ArgumentException($"Missing or empty ResourceProperties.RoleArn");
                    }

                    if (string.IsNullOrWhiteSpace(request.ResourceProperties.FunctionName))
                    {
                        throw new ArgumentException($"Missing or empty ResourceProperties.FunctionName");
                    }

                    if (string.IsNullOrWhiteSpace(request.ResourceProperties.EmbeddedFileName))
                    {
                        throw new ArgumentException($"Missing or empty ResourceProperties.EmbeddedFileName");
                    }

                    Stream       lambdaEdgeJsFile = GetLambdaEdgeMemoryStreamFromAssembly(request);
                    MemoryStream zipFile          = AddFileStreamToZipAndReturnZipStream(lambdaEdgeJsFile, request.ResourceProperties.EmbeddedFileName);

                    if (request.RequestType == "Create")
                    {
                        var createResponse = await LambdaClient.CreateFunctionAsync(new CreateFunctionRequest
                        {
                            FunctionName = request.ResourceProperties.FunctionName,
                            Handler      = $"{Path.GetFileNameWithoutExtension(request.ResourceProperties.EmbeddedFileName)}.handler",
                            Code         = new FunctionCode()
                            {
                                ZipFile = zipFile
                            },
                            Runtime = Runtime.Nodejs610,
                            Publish = true,
                            Role    = request.ResourceProperties.RoleArn
                        });

                        request.PhysicalResourceId = createResponse.FunctionArn;
                        lambdaVersionedArn         = $"{createResponse.FunctionArn}:{createResponse.Version}";
                    }
                    else if (request.RequestType == "Update")
                    {
                        if (request.PhysicalResourceId.StartsWith("arn:aws:lambda:"))
                        {
                            var updateResponse = await LambdaClient.UpdateFunctionCodeAsync(new UpdateFunctionCodeRequest
                            {
                                FunctionName = $"{request.PhysicalResourceId}",
                                ZipFile      = zipFile,
                                Publish      = true,
                            });

                            // The FunctionArn sometimes returns the version in it
                            if (updateResponse.FunctionArn.EndsWith(updateResponse.Version))
                            {
                                lambdaVersionedArn = $"{updateResponse.FunctionArn}";
                            }
                            else
                            {
                                lambdaVersionedArn = $"{updateResponse.FunctionArn}:{updateResponse.Version}";
                            }
                        }
                        else
                        {
                            context.Logger.LogLine("PhysicalResourceId was not a lambda resource on UPDATE, skipping update command");
                        }
                    }
                }
                else if (request.RequestType == "Delete")
                {
                    if (request.PhysicalResourceId.StartsWith("arn:aws:lambda:"))
                    {
                        var updateResponse = await LambdaClient.DeleteFunctionAsync(new DeleteFunctionRequest
                        {
                            FunctionName = $"{request.PhysicalResourceId}"
                        });
                    }
                    else
                    {
                        context.Logger.LogLine("PhysicalResourceId was not a lambda resource on DELETE, skipping remove command");
                    }
                }

                await CloudFormationResponse.CompleteCloudFormation(new { LambdaVersionedArn = lambdaVersionedArn }, request, context);
            }
            catch (Exception ex)
            {
                await CloudFormationResponse.FailCloudFormation(ex, request, context);

                throw;
            }
        }