示例#1
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 DeployMultiProject()
        {
            var assembly = this.GetType().GetTypeInfo().Assembly;

            var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../../testapps/MPDeployServerless/CurrentDirectoryTest");
            var command  = new DeployServerlessCommand(new TestToolLogger(_testOutputHelper), fullPath, new string[] { });

            command.StackName = "DeployMultiProject-" + DateTime.Now.Ticks;
            command.S3Bucket  = this._testFixture.Bucket;
            command.WaitForStackToComplete = true;
            command.DisableInteractive     = true;
            command.ProjectLocation        = fullPath;

            var created = await command.ExecuteAsync();

            try
            {
                Assert.True(created);

                using (var cfClient = new AmazonCloudFormationClient(RegionEndpoint.USEast1))
                {
                    var stack  = (await cfClient.DescribeStacksAsync(new DescribeStacksRequest {
                        StackName = command.StackName
                    })).Stacks[0];
                    var apiUrl = stack.Outputs.FirstOrDefault(x => string.Equals(x.OutputKey, "ApiURL"))?.OutputValue;
                    Assert.NotNull(apiUrl);

                    Assert.Equal("CurrentProjectTest", await GetRestContent(apiUrl, "current"));
                    Assert.Equal("SecondCurrentProjectTest", await GetRestContent(apiUrl, "current2"));
                    Assert.Equal("SiblingProjectTest", await GetRestContent(apiUrl, "sibling"));
                    Assert.Equal("SingleFileNodeFunction", await GetRestContent(apiUrl, "singlenode"));
                    Assert.Equal("DirectoryNodeFunction", await GetRestContent(apiUrl, "directorynode"));
                }
            }
            finally
            {
                if (created)
                {
                    var deleteCommand = new DeleteServerlessCommand(new TestToolLogger(_testOutputHelper), fullPath, new string[0]);
                    deleteCommand.StackName = command.StackName;
                    await deleteCommand.ExecuteAsync();
                }
            }
        }
        public async Task RunYamlServerlessDeployCommand()
        {
            var assembly = this.GetType().GetTypeInfo().Assembly;

            var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../../testapps/ServerlessWithYamlFunction");
            var command  = new DeployServerlessCommand(new TestToolLogger(_testOutputHelper), fullPath, new string[] { "--template-parameters", "Environment=whatever" });

            command.StackName = "ServerlessYamlStackTest-" + DateTime.Now.Ticks;
            command.S3Bucket  = this._testFixture.Bucket;
            command.WaitForStackToComplete = true;
            command.DisableInteractive     = true;
            command.ProjectLocation        = fullPath;

            var created = await command.ExecuteAsync();

            try
            {
                Assert.True(created);

                // Test if a redeployment happens with different template parameters it works.
                var renameParameterCommand = new DeployServerlessCommand(new TestToolLogger(_testOutputHelper), fullPath, new string[] { "--template-parameters", "EnvironmentRename=whatever" });
                renameParameterCommand.StackName = command.StackName;
                renameParameterCommand.S3Bucket  = this._testFixture.Bucket;
                renameParameterCommand.WaitForStackToComplete = true;
                renameParameterCommand.DisableInteractive     = true;
                renameParameterCommand.ProjectLocation        = fullPath;
                renameParameterCommand.CloudFormationTemplate = "rename-params-template.yaml";

                var updated = await renameParameterCommand.ExecuteAsync();

                Assert.True(updated);
            }
            finally
            {
                if (created)
                {
                    var deleteCommand = new DeleteServerlessCommand(new TestToolLogger(_testOutputHelper), fullPath, new string[0]);
                    deleteCommand.StackName = command.StackName;
                    await deleteCommand.ExecuteAsync();
                }
            }
        }
        public async Task TestDeployLargeServerless()
        {
            var logger   = new TestToolLogger();
            var assembly = this.GetType().GetTypeInfo().Assembly;

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

            command.Region                 = "us-east-1";
            command.Configuration          = "Release";
            command.TargetFramework        = "netcoreapp2.1";
            command.CloudFormationTemplate = "large-serverless.template";
            command.StackName              = "TestDeployLargeServerless-" + DateTime.Now.Ticks;
            command.S3Bucket               = this._testFixture.Bucket;

            command.WaitForStackToComplete = true;
            command.ProjectLocation        = fullPath;
            command.DisableInteractive     = true;



            var created = await command.ExecuteAsync();

            try
            {
                Assert.True(created);
            }
            finally
            {
                if (created)
                {
                    var deleteCommand = new DeleteServerlessCommand(new TestToolLogger(_testOutputHelper), fullPath, new string[0]);
                    deleteCommand.StackName          = command.StackName;
                    deleteCommand.Region             = command.Region;
                    deleteCommand.DisableInteractive = true;
                    await deleteCommand.ExecuteAsync();
                }
            }
        }
示例#5
0
        public static void Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    PrintUsage();
                    Environment.Exit(-1);
                }

                ICommand command = null;
                switch (args[0])
                {
                case DeployFunctionCommand.COMMAND_DEPLOY_NAME:
                    command = new DeployFunctionCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case InvokeFunctionCommand.COMMAND_NAME:
                    command = new InvokeFunctionCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case ListFunctionCommand.COMMAND_NAME:
                    command = new ListFunctionCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case DeleteFunctionCommand.COMMAND_NAME:
                    command = new DeleteFunctionCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case GetFunctionConfigCommand.COMMAND_NAME:
                    command = new GetFunctionConfigCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case UpdateFunctionConfigCommand.COMMAND_NAME:
                    command = new UpdateFunctionConfigCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case DeployServerlessCommand.COMMAND_NAME:
                    command = new DeployServerlessCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case ListServerlessCommand.COMMAND_NAME:
                    command = new ListServerlessCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case DeleteServerlessCommand.COMMAND_NAME:
                    command = new DeleteServerlessCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case PackageCommand.COMMAND_NAME:
                    command = new PackageCommand(new ConsoleToolLogger(), Directory.GetCurrentDirectory(), args.Skip(1).ToArray());
                    break;

                case "--help":
                case "--h":
                case "help":
                    PrintUsageHeader();

                    if (args.Length > 1)
                    {
                        PrintUsage(args[1]);
                    }
                    else
                    {
                        PrintUsage();
                    }
                    break;

                default:
                    Console.Error.WriteLine($"Unknown command {args[0]}");
                    PrintUsage();
                    Environment.Exit(-1);
                    break;
                }

                if (command != null)
                {
                    command.EnableInteractive = true;
                    var success = command.ExecuteAsync().Result;
                    if (!success)
                    {
                        Environment.Exit(-1);
                    }
                }
            }
            catch (LambdaToolsException e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(-1);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Unknown error: {e.Message}");
                Console.Error.WriteLine(e.StackTrace);

                Environment.Exit(-1);
            }
        }
示例#6
0
        public async Task DeployAspNetCoreWithlayer()
        {
            var logger = new TestToolLogger(_testOutputHelper);

            var templateTilePath = Path.Combine(_aspnercoreLayerFunctionPath, "serverless.template");

            if (File.Exists(templateTilePath))
            {
                File.Delete(templateTilePath);
            }

            var publishLayerCommand = await PublishLayerAsync(_aspnercoreLayerFunctionPath, "");

            try
            {
                var getLayerResponse = await this._testFixture.LambdaClient.GetLayerVersionAsync(new GetLayerVersionRequest { LayerName = publishLayerCommand.NewLayerArn, VersionNumber = publishLayerCommand.NewLayerVersionNumber });

                Assert.NotNull(getLayerResponse.Description);

                // Make sure layer does not contain any core ASP.NET Core dependencies.
                var layerManifest = JsonMapper.ToObject <LayerDescriptionManifest>(getLayerResponse.Description);
                using (var getManifestResponse = await this._testFixture.S3Client.GetObjectAsync(layerManifest.Buc, layerManifest.Key))
                    using (var reader = new StreamReader(getManifestResponse.ResponseStream))
                    {
                        var xml = await reader.ReadToEndAsync();

                        Assert.False(xml.Contains("Microsoft.AspNetCore"));
                        Assert.False(xml.Contains("runtime"));
                    }

                var templateContent = File.ReadAllText(Path.Combine(_aspnercoreLayerFunctionPath, "fake.template"));
                templateContent =
                    templateContent.Replace("LAYER_ARN_PLACEHOLDER", publishLayerCommand.NewLayerVersionArn);

                File.WriteAllText(templateTilePath, templateContent);

                var command = new DeployServerlessCommand(new TestToolLogger(_testOutputHelper), _aspnercoreLayerFunctionPath, new string[] { });
                command.DisableInteractive     = true;
                command.StackName              = "DeployAspNetCoreWithlayer-" + DateTime.Now.Ticks;
                command.Region                 = publishLayerCommand.Region;
                command.S3Bucket               = this._testFixture.Bucket;
                command.WaitForStackToComplete = true;
                command.DisableInteractive     = true;
                command.ProjectLocation        = _aspnercoreLayerFunctionPath;
                var created = await command.ExecuteAsync();

                try
                {
                    Assert.True(created);

                    var lambdaFunctionName =
                        await TestHelper.GetPhysicalCloudFormationResourceId(_testFixture.CFClient, command.StackName, "AspNetCoreFunction");

                    var apiUrl = await TestHelper.GetOutputParameter(_testFixture.CFClient, command.StackName, "ApiURL");

                    using (var client = new HttpClient())
                    {
                        await client.GetStringAsync(new Uri(new Uri(apiUrl), "api/values"));
                    }

                    var getConfigResponse = await this._testFixture.LambdaClient.GetFunctionConfigurationAsync(new GetFunctionConfigurationRequest { FunctionName = lambdaFunctionName });

                    Assert.NotNull(getConfigResponse.Layers.FirstOrDefault(x => string.Equals(x.Arn, publishLayerCommand.NewLayerVersionArn)));

                    var getCodeResponse = await this._testFixture.LambdaClient.GetFunctionAsync(lambdaFunctionName);

                    using (var client = new HttpClient())
                    {
                        var data = await client.GetByteArrayAsync(getCodeResponse.Code.Location);

                        var zipArchive = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read);

                        Assert.NotNull(zipArchive.GetEntry("TestLayerAspNetCore.dll"));
                        Assert.Null(zipArchive.GetEntry("Amazon.Lambda.Core.dll"));
                        Assert.Null(zipArchive.GetEntry("AWSSDK.S3.dll"));
                        Assert.Null(zipArchive.GetEntry("AWSSDK.Extensions.NETCore.Setup.dll"));
                        Assert.Null(zipArchive.GetEntry("Amazon.Lambda.AspNetCoreServer.dll"));
                    }
                }
                finally
                {
                    if (created)
                    {
                        var deleteCommand = new DeleteServerlessCommand(new TestToolLogger(_testOutputHelper), _aspnercoreLayerFunctionPath, new string[0]);
                        deleteCommand.DisableInteractive = true;
                        deleteCommand.Region             = publishLayerCommand.Region;
                        deleteCommand.StackName          = command.StackName;
                        await deleteCommand.ExecuteAsync();
                    }
                }
            }
            finally
            {
                await this._testFixture.LambdaClient.DeleteLayerVersionAsync(new DeleteLayerVersionRequest { LayerName = publishLayerCommand.NewLayerArn, VersionNumber = publishLayerCommand.NewLayerVersionNumber });

                if (File.Exists(templateTilePath))
                {
                    File.Delete(templateTilePath);
                }
            }
        }
示例#7
0
        public async Task DeployServerlessWithlayer()
        {
            var logger = new TestToolLogger(_testOutputHelper);

            var templateTilePath = Path.Combine(_serverlessLayerFunctionPath, "serverless.template");

            if (File.Exists(templateTilePath))
            {
                File.Delete(templateTilePath);
            }

            var publishLayerCommand = await PublishLayerAsync(_serverlessLayerFunctionPath, "");

            try
            {
                var templateContent = File.ReadAllText(Path.Combine(_serverlessLayerFunctionPath, "fake.template"));
                templateContent =
                    templateContent.Replace("LAYER_ARN_PLACEHOLDER", publishLayerCommand.NewLayerVersionArn);

                File.WriteAllText(templateTilePath, templateContent);

                var command = new DeployServerlessCommand(new TestToolLogger(_testOutputHelper), _serverlessLayerFunctionPath, new string[] { });
                command.DisableInteractive     = true;
                command.StackName              = "DeployServerlessWithlayer-" + DateTime.Now.Ticks;
                command.Region                 = publishLayerCommand.Region;
                command.S3Bucket               = this._testFixture.Bucket;
                command.WaitForStackToComplete = true;
                command.DisableInteractive     = true;
                command.ProjectLocation        = _serverlessLayerFunctionPath;

                var created = await command.ExecuteAsync();

                try
                {
                    Assert.True(created);

                    Func <string, Task> testFunctionAsync = async(functionName) =>
                    {
                        var lambdaFunctionName =
                            await TestHelper.GetPhysicalCloudFormationResourceId(_testFixture.CFClient, command.StackName, functionName);

                        await ValidateInvokeAsync(lambdaFunctionName, "\"hello\"", "\"HELLO\"");

                        var getConfigResponse = await this._testFixture.LambdaClient.GetFunctionConfigurationAsync(new GetFunctionConfigurationRequest { FunctionName = lambdaFunctionName });

                        Assert.NotNull(getConfigResponse.Layers.FirstOrDefault(x => string.Equals(x.Arn, publishLayerCommand.NewLayerVersionArn)));

                        var getCodeResponse = await this._testFixture.LambdaClient.GetFunctionAsync(lambdaFunctionName);

                        using (var client = new HttpClient())
                        {
                            var data = await client.GetByteArrayAsync(getCodeResponse.Code.Location);

                            var zipArchive = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read);

                            Assert.NotNull(zipArchive.GetEntry("TestLayerServerless.dll"));
                            Assert.Null(zipArchive.GetEntry("Amazon.Lambda.Core.dll"));
                        }
                    };

                    await testFunctionAsync("TheFunction");
                    await testFunctionAsync("TheSecondFunctionSameSource");
                }
                finally
                {
                    if (created)
                    {
                        var deleteCommand = new DeleteServerlessCommand(new TestToolLogger(_testOutputHelper), _serverlessLayerFunctionPath, new string[0]);
                        deleteCommand.DisableInteractive = true;
                        deleteCommand.Region             = publishLayerCommand.Region;
                        deleteCommand.StackName          = command.StackName;
                        await deleteCommand.ExecuteAsync();
                    }
                }
            }
            finally
            {
                await this._testFixture.LambdaClient.DeleteLayerVersionAsync(new DeleteLayerVersionRequest { LayerName = publishLayerCommand.NewLayerArn, VersionNumber = publishLayerCommand.NewLayerVersionNumber });

                if (File.Exists(templateTilePath))
                {
                    File.Delete(templateTilePath);
                }
            }
        }