public async Task TestServerlessPackage()
        {
            var logger   = new TestToolLogger();
            var assembly = this.GetType().GetTypeInfo().Assembly;

            var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../../testapps/TestServerlessWebApp");
            var command  = new PackageCICommand(logger, fullPath, new string[0]);

            command.Region                       = "us-west-2";
            command.Configuration                = "Release";
            command.TargetFramework              = "netcoreapp2.1";
            command.CloudFormationTemplate       = "serverless.template";
            command.CloudFormationOutputTemplate = Path.Combine(Path.GetTempPath(), "output-serverless.template");
            command.S3Bucket                     = "serverless-package-test-" + DateTime.Now.Ticks;
            command.DisableInteractive           = true;

            if (File.Exists(command.CloudFormationOutputTemplate))
            {
                File.Delete(command.CloudFormationOutputTemplate);
            }


            await command.S3Client.PutBucketAsync(command.S3Bucket);

            try
            {
                Assert.True(await command.ExecuteAsync());
                Assert.True(File.Exists(command.CloudFormationOutputTemplate));
            }
            finally
            {
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(command.S3Client, command.S3Bucket);
            }
        }
Example #2
0
        public static async Task <bool> Destroy(ILogger log, Context context)
        {
            var baseStack = await Cloudformation.GetExistingStackAsync(context.Cloudformation, BaseStack.Name(context.Config)).ConfigureAwait(false);

            if (baseStack == null)
            {
                log.Error("Deploy the {stackName} stack first.", BaseStack.Name(context.Config));
                return(false);
            }

            var role   = baseStack.GetStackOutput("CloudFormationServiceRole");
            var bucket = baseStack.GetStackOutput("DeploymentsBucket");

            try
            {
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(context.S3, bucket);
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error deleting S3 bucket {bucket}. Message: {message}", bucket, ex.Message);
                return(false);
            }

            var result = await Cloudformation.DeleteStackAsync(log, context.Cloudformation, Name(context.Config), role);

            return(result);
        }
Example #3
0
        protected override async Task DoDeleteAllAsync(CancellationToken?cancellationToken = null)
        {
            if (await IsBucketExistsAsync(Options.Bucket))
            {
                if (string.IsNullOrEmpty(Options.Prefix))
                {
                    try
                    {
                        await AmazonS3Util.DeleteS3BucketWithObjectsAsync(_s3Client, Options.Bucket,
                                                                          cancellationToken ?? CancellationToken.None);
                    }
                    catch (AmazonS3Exception ex) when(ex.Message.Contains(
                                                          "A header you provided implies functionality that is not implemented"))
                    {
                        await DeleteAllObjectsInBucket(cancellationToken);

                        await _s3Client.DeleteBucketAsync(Options.Bucket);
                    }
                }
                else
                {
                    await DeleteAllObjectsInBucket(cancellationToken);
                }
            }
        }
Example #4
0
        //[Fact]
        public async Task TestS3EventLambdaFunction()
        {
            var            reg = RegionEndpoint.USEast1;
            IAmazonEC2     ec2 = new AmazonEC2Client(reg);
            IAmazonRoute53 r53 = new AmazonRoute53Client(reg);
            IAmazonS3      s3  = new AmazonS3Client(reg);

            var bucketName = "lambda-VMBot-".ToLower() + DateTime.Now.Ticks;
            var key        = "text.txt";

            // Create a bucket an object to setup a test data.
            await s3.PutBucketAsync(bucketName);

            try
            {
                await s3.PutObjectAsync(new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = key,
                    ContentBody = "sample data"
                });

                // Setup the S3 event object that S3 notifications would create with the fields used by the Lambda function.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = key
                                }
                            }
                        }
                    }
                };

                var ec2StateChange = new EC2StateChangeEvent
                {
                };

                // Invoke the lambda function and confirm the content type was returned.
                var function = new Function(ec2, r53, s3);
                // var contentType = await function.FunctionHandler(s3Event, null);
                var contentType = await function.FunctionHandler(ec2StateChange, null);

                Assert.Equal("text/plain", contentType);
            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3, bucketName);
            }
        }
Example #5
0
        public async Task DeployStepFunctionWithTemplateSubstitution()
        {
            var cfClient = new AmazonCloudFormationClient(RegionEndpoint.USEast2);
            var s3Client = new AmazonS3Client(RegionEndpoint.USEast2);

            var bucketName = "deploy-step-functions-" + DateTime.Now.Ticks;
            await s3Client.PutBucketAsync(bucketName);

            try
            {
                var assembly = this.GetType().GetTypeInfo().Assembly;

                var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../TemplateSubstitutionTestProjects/StateMachineDefinitionStringTest");
                var command  = new DeployServerlessCommand(new TestToolLogger(), fullPath, new string[0]);
                command.Configuration          = "Release";
                command.TargetFramework        = "netcoreapp1.0";
                command.StackName              = "DeployStepFunctionWithTemplateSubstitution-" + DateTime.Now.Ticks;
                command.S3Bucket               = bucketName;
                command.WaitForStackToComplete = true;

                command.TemplateParameters = new Dictionary <string, string> {
                    { "NonExisting", "Parameter" }
                };

                var created = await command.ExecuteAsync();

                try
                {
                    Assert.True(created);

                    var describeResponse = await cfClient.DescribeStacksAsync(new DescribeStacksRequest
                    {
                        StackName = command.StackName
                    });

                    Assert.Equal(StackStatus.CREATE_COMPLETE, describeResponse.Stacks[0].StackStatus);
                }
                finally
                {
                    if (created)
                    {
                        try
                        {
                            var deleteCommand = new DeleteServerlessCommand(new ConsoleToolLogger(), fullPath, new string[0]);
                            deleteCommand.StackName = command.StackName;
                            await deleteCommand.ExecuteAsync();
                        }
                        catch
                        {
                            // Bury exception because we don't want to lose any exceptions during the deploy stage.
                        }
                    }
                }
            }
            finally
            {
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }
        public async Task IntegrationTest()
        {
            const string       fileName          = "sample-pic.jpg";
            IAmazonS3          s3Client          = new AmazonS3Client(RegionEndpoint.USWest2);
            IAmazonRekognition rekognitionClient = new AmazonRekognitionClient(RegionEndpoint.USWest2);

            var bucketName = "lambda-ImageViewer.Labeling-".ToLower() + DateTime.Now.Ticks;
            await s3Client.PutBucketAsync(bucketName);

            try
            {
                await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName = bucketName,
                    FilePath   = fileName
                });

                // Setup the S3 event object that S3 notifications would create and send to the Lambda function if
                // the bucket was configured as an event source.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = fileName
                                }
                            }
                        }
                    }
                };

                // Use test constructor for the function with the service clients created for the test
                var function = new Function(s3Client, rekognitionClient, Function.DEFAULT_MIN_CONFIDENCE);

                var context = new TestLambdaContext();
                await function.FunctionHandler(s3Event, context);

                var getTagsResponse = await s3Client.GetObjectTaggingAsync(new GetObjectTaggingRequest
                {
                    BucketName = bucketName,
                    Key        = fileName
                });

                Assert.True(getTagsResponse.Tagging.Count > 0);
            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }
        public async Task TestS3EventLambdaFunction()
        {
            // Use your Amazon awsAccessKeyId, awsAccessSecretId, RegionEndPoint
            string    awsAccessKeyId    = "";
            string    awsAccessSecretId = "";
            IAmazonS3 s3Client          = new AmazonS3Client(awsAccessKeyId, awsAccessSecretId, RegionEndpoint.USEast1);

            var bucketName = "lambda-AWS.S3Notification.Lambda-".ToLower() + DateTime.Now.Ticks;
            var key        = "text.json";

            // Create a bucket an object to setup a test data.
            await s3Client.PutBucketAsync(bucketName);

            try
            {
                await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = key,
                    ContentBody = "{event:'s3event', company:'amazon', 'description':'Capturing S3 event notification using Lambda'}"
                });

                // Setup the S3 event object that S3 notifications would create with the fields used by the Lambda function.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = key
                                }
                            }
                        }
                    }
                };

                // Invoke the lambda function and confirm the content type was returned.
                //Create webhook url from slack website https://api.slack.com/incoming-webhooks and replace below url string with one you have acsess
                string         url         = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX";
                var            webhookUrl  = new Uri(url);
                var            function    = new AWSS3EventNotification(s3Client, new SlackClient(), webhookUrl, BuildSlackMessage());
                ILambdaContext ctx         = new TestLambdaContext();
                var            contentType = await function.FunctionHandler(s3Event, ctx);

                Assert.Equal("Handler: Got Data from Key text.json for s3event with description Capturing S3 event notification using Lambda", contentType);
            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }
Example #8
0
        public object Execute(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;

            CmdletOutput output;

            if (cmdletContext.DeleteObjects)
            {
                output = null;
#if DESKTOP
                AmazonS3Util.DeleteS3BucketWithObjects(Client,
                                                       cmdletContext.BucketName,
                                                       new S3DeleteBucketWithObjectsOptions
                {
                    ContinueOnError = false
                });
#elif CORECLR
                AmazonS3Util.DeleteS3BucketWithObjectsAsync(Client,
                                                            cmdletContext.BucketName,
                                                            new S3DeleteBucketWithObjectsOptions
                {
                    ContinueOnError = false
                }).Wait();
#else
#error "Unknown build edition"
#endif
            }
            else
            {
                var request = new DeleteBucketRequest {
                    BucketName = cmdletContext.BucketName
                };

                using (var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint))
                {
                    try
                    {
                        var    response       = CallAWSServiceOperation(client, request);
                        object pipelineOutput = null;
                        pipelineOutput = cmdletContext.Select(response, this);
                        output         = new CmdletOutput
                        {
                            PipelineOutput  = pipelineOutput,
                            ServiceResponse = response,
                        };
                    }
                    catch (Exception e)
                    {
                        output = new CmdletOutput {
                            ErrorResponse = e
                        };
                    }
                }
            }

            return(output);
        }
Example #9
0
 public async Task PurgeBucketAsync(string bucketName)
 {
     await AmazonS3Util.DeleteS3BucketWithObjectsAsync(
         _amazonS3Client,
         bucketName.ToLower(),
         new S3DeleteBucketWithObjectsOptions()
     {
         ContinueOnError = true,
         QuietMode       = true
     });
 }
Example #10
0
        public async Task TestS3EventLambdaFunction()
        {
            IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.USWest2);
            var       context  = new TestLambdaContext();

            var bucketName = "lambda-S3JsonLogger-".ToLower() + DateTime.Now.Ticks;
            var key        = "text.txt";

            // Create a bucket an object to setup a test data.
            await s3Client.PutBucketAsync(bucketName);

            try
            {
                await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = key,
                    ContentBody = "sample data"
                });

                // Setup the S3 event object that S3 notifications would create with the fields used by the Lambda function.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = key
                                }
                            }
                        }
                    }
                };

                // Invoke the lambda function and confirm the content type was returned.
                var function    = new Function(s3Client);
                var contentType = await function.FunctionHandler(s3Event, context);

                Assert.Equal("text/plain", contentType);
            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }
Example #11
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    AmazonS3Util.DeleteS3BucketWithObjectsAsync(this.S3Client, BucketName).Wait();
                    this.S3Client.Dispose();
                }

                disposedValue = true;
            }
        }
Example #12
0
        public async Task TestS3EventLambdaFunction()
        {
            // IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.USWest2);

            var bucketName = "lambda-KiranAWSLambda-".ToLower() + DateTime.Now.Ticks;
            var key        = "text.txt";

            // Create a bucket an object to setup a test data.
            //await s3Client.PutBucketAsync(bucketName);
            try
            {
                /*await s3Client.PutObjectAsync(new PutObjectRequest
                 * {
                 *  BucketName = bucketName,
                 *  Key = key,
                 *  ContentBody = "sample test"
                 *  //"player,ball_1,ball_2,ball_3 Bryan,8,2,0 Bryan,3,0,0 Bryan,10,0,0 Bryan,10,0,0 Bryan,0,0,0 Bryan,9,1,0 Bryan,5,5,0 Bryan,10,0,0 Bryan,10,0,0 Bryan,10,10,10"
                 * });*/

                // Setup the S3 event object that S3 notifications would create with the fields used by the Lambda function.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = key
                                }
                            }
                        }
                    }
                };

                // Invoke the lambda function and confirm.
                var function = new Function(s3Client);
                var output   = await function.FunctionHandler(s3Event, null);

                Assert.Equal("No Events", output);
            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    AmazonS3Util.DeleteS3BucketWithObjectsAsync(this.S3Client, this.Bucket).GetAwaiter().GetResult();

                    this.S3Client.Dispose();
                }

                disposedValue = true;
            }
        }
        public async Task TestServerlessPackage()
        {
            var logger   = new TestToolLogger();
            var assembly = this.GetType().GetTypeInfo().Assembly;

            var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../../testapps/TestServerlessWebApp");
            var command  = new PackageCICommand(logger, fullPath, new string[0]);

            command.Region                       = "us-west-2";
            command.Configuration                = "Release";
            command.CloudFormationTemplate       = "serverless-arm.template";
            command.CloudFormationOutputTemplate = Path.Combine(Path.GetTempPath(), "output-serverless-arm.template");
            command.S3Bucket                     = "serverless-package-test-" + DateTime.Now.Ticks;
            command.DisableInteractive           = true;

            if (File.Exists(command.CloudFormationOutputTemplate))
            {
                File.Delete(command.CloudFormationOutputTemplate);
            }


            await command.S3Client.PutBucketAsync(command.S3Bucket);

            try
            {
                Assert.True(await command.ExecuteAsync());
                Assert.True(File.Exists(command.CloudFormationOutputTemplate));

                var templateJson = File.ReadAllText(command.CloudFormationOutputTemplate);
                var templateRoot = JsonConvert.DeserializeObject(templateJson) as JObject;
                var codeUri      = templateRoot["Resources"]["DefaultFunction"]["Properties"]["CodeUri"].ToString();
                Assert.False(string.IsNullOrEmpty(codeUri));

                var s3Key = codeUri.Split('/').Last();

                var transfer        = new TransferUtility(command.S3Client);
                var functionZipPath = Path.GetTempFileName();
                await transfer.DownloadAsync(functionZipPath, command.S3Bucket, s3Key);

                Assert.Equal("linux-arm64", GetRuntimeFromBundle(functionZipPath));

                File.Delete(functionZipPath);
            }
            finally
            {
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(command.S3Client, command.S3Bucket);
            }
        }
Example #15
0
        public async Task TestS3EventLambdaFunction()
        {
            var accessKey = Configuration["AWS:AccessKeyId"];
            var secretKey = Configuration["AWS:SecretKey"];

            IAmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), RegionEndpoint.APSoutheast2);

            var bucketName = "lambda-LambdaS3test-".ToLower() + DateTime.Now.Ticks;
            var key        = "abcd1234";

            var contextMock = new Mock <ILambdaContext>();

            contextMock.Setup(x => x.Logger).Returns(new TestLambdaLogger());

            // Create a bucket an object to setup a test data.
            await s3Client.PutBucketAsync(bucketName);

            try
            {
                var imageData = new Models.ObjectModel();
                imageData.ImageData = await ReadImageBase64("TestData\\borders.jpg");

                await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = key,
                    ContentBody = JsonConvert.SerializeObject(imageData)
                });

                // Setup the S3 event object that S3 notifications would create with the fields used by the Lambda function.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = key
                                }
                            },
                            EventName = EventType.ObjectCreatedPut
                        }
                    }
                };

                // Invoke the lambda function and confirm the content type was returned.
                var function    = new Function(s3Client);
                var contentType = await function.FunctionHandler(s3Event, contextMock.Object);

                Assert.Equal(null, contentType);
                Assert.True(true, "Succeeded");
            }
            catch (Exception e)
            {
                Assert.True(false, "Not succeeded");
            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }
Example #16
0
        public static async Task DeleteBucketAsync(AmazonS3Client client, string bucket)
        {
            await AmazonS3Util.DeleteS3BucketWithObjectsAsync(client, bucket);

            Console.WriteLine($"Deleted S3 bucket: {bucket}");
        }
Example #17
0
 public async void Dispose()
 {
     await AmazonS3Util.DeleteS3BucketWithObjectsAsync(S3Client, BucketName);
 }