Example #1
0
        public async Task <ImageBuilderPackagerResponse> SearchAmiHandler(ImageBuilderPackagerRequest request, ILambdaContext context)
        {
            var serviceProvider = new Startup().Initialise();

            var app   = serviceProvider.GetService <ISearchAmiService>();
            var image = await app.Execute(request);

            return(new ImageBuilderPackagerResponse {
                success = (image != null),
                imageId = image?.ImageId,
                name = image?.Name,
                description = image?.Description,
            });
        }
        /// Get the AMI Image Details from EC2 AWS Marketplace
        public async Task <Amazon.EC2.Model.Image> Execute(ImageBuilderPackagerRequest request)
        {
            Amazon.EC2.Model.Image response = null;

            // Request Details
            this.logger.LogInformation(string.Format("Benchmark Name: {0}", request?.benchmarkName));
            this.logger.LogInformation(string.Format("Profile Level: {0}", request?.profileLevel));

            // Construct the AMI name pattern
            var amiNamePattern = request.benchmarkName + "*" + request.profileLevel + "*";

            // Create a Describe Images Request
            var describeRequest = new Amazon.EC2.Model.DescribeImagesRequest {
                Owners = new List <string> {
                    AWS_MARKETPLACE_ID
                },
                Filters = new List <Amazon.EC2.Model.Filter> {
                    new Amazon.EC2.Model.Filter {
                        Name   = "name",
                        Values = new List <string> {
                            amiNamePattern
                        }
                    }
                }
            };

            // Execute the request.
            this.logger.LogInformation("EC2 Describe-Images: Request - Begin");
            var describeResponse = await this.ec2Client.DescribeImagesAsync(describeRequest);

            this.logger.LogInformation("EC2 Describe-Images: Request - End");

            // Process the response.
            this.logger.LogInformation("EC2 Describe-Images: Response - Begin");
            if (describeResponse != null)
            {
                // Log all responses.
                describeResponse.Images.ForEach(item => this.logger.LogDebug(this.GetAmiImageDetailsAsString(item)));

                // Find the most recent AMI and set a result.
                response = describeResponse.Images.OrderByDescending(item => item.CreationDate).FirstOrDefault();
                this.logger.LogInformation(string.Format("SELECTED: {0}", this.GetAmiImageDetailsAsString(response)));
            }
            this.logger.LogInformation("EC2 Describe-Images: Response - End");

            // Return the selected AMI Image
            return(response);
        }