Пример #1
0
        static async Task <(string functionArn, string functionName, Func <Task> functionCleanup)> CreateFunction(
            string roleArn,
            IAmazonLambda lambda,
            LambdaCreationOptions options)
        {
            var functionName = $"Beetles_{options.Name}_{options.Timestamp}";

            var retryWait = Stopwatch.StartNew();
            CreateFunctionResponse function = null;

            do
            {
                try
                {
                    function = await lambda.CreateFunctionAsync(new CreateFunctionRequest
                    {
                        Code = new FunctionCode {
                            ZipFile = new MemoryStream(File.ReadAllBytes(options.PackagePath))
                        },
                        Description  = $"Beetles Load Test {options.Name}",
                        FunctionName = functionName,
                        Handler      = options.LambdaHandlerName,
                        MemorySize   = options.MemorySize,
                        Publish      = true,
                        Role         = roleArn,
                        Runtime      = "dotnetcore2.1",
                        Timeout      = 120,
                        Environment  = new Environment
                        {
                            Variables =
                            {
                                [Constants.ConfigurationTypeNameKey] = options.SettingsTypeName
                            }
                        },
                        VpcConfig = new VpcConfig()
                    }, options.CancellationToken);

//          await lambda.PutFunctionConcurrencyAsync(new PutFunctionConcurrencyRequest()
//          {
//            FunctionName = functionName,
//            ReservedConcurrentExecutions = options.ProvisionedConcurrency
//          }, options.CancellationToken);
                }
                catch (InvalidParameterValueException) when(retryWait.Elapsed < TimeSpan.FromMinutes(1))
                {
                }
            } while (function == null);

            var functionArn = function.FunctionArn;

            return(functionArn, functionName, async() => await lambda.DeleteFunctionAsync(functionName));
        }
Пример #2
0
        static async Task <(string topicArn, Func <Task> cleanup)> CreateLambda(LambdaCreationOptions lambdaCreationOptions)
        {
            var    cleanup = new List <Func <Task> >();
            var    log     = lambdaCreationOptions.Verbose;
            string publishTopic;


            var lambdaClient = new AmazonLambdaClient();
            var snsClient    = new AmazonSimpleNotificationServiceClient();
            var iamClient    = new AmazonIdentityManagementServiceClient();

            cleanup.Add(() =>
            {
                lambdaClient.Dispose();
                snsClient.Dispose();
                iamClient.Dispose();
                return(Task.CompletedTask);
            });


            try
            {
                log("Creating IAM Role");
                var(roleArn, _, roleCleanup) =
                    await CreateRole(iamClient, lambdaCreationOptions.Timestamp, lambdaCreationOptions.Name,
                                     lambdaCreationOptions.CancellationToken);

                cleanup.Add(roleCleanup);

                log("Creating Function");
                var(functionArn, _, functionCleanup) =
                    await CreateFunction(roleArn,
                                         lambdaClient,
                                         lambdaCreationOptions);

                cleanup.Add(functionCleanup);

                log("Creating SNS Topic");
                var(topicArn, topicCleanup) =
                    await CreateTopic(snsClient, lambdaCreationOptions.Timestamp, lambdaCreationOptions.Name,
                                      lambdaCreationOptions.CancellationToken);

                cleanup.Add(topicCleanup);
                publishTopic = topicArn;

                log("Creating SNS Subscription");
                var subCleaner = await Subscribe(snsClient, functionArn, topicArn, lambdaCreationOptions.CancellationToken);

                cleanup.Add(subCleaner);

                log("Permissioning SNS to execute the function");
                var permCleanup = await AddExecutionFromSnsPermission(lambdaClient, functionArn, topicArn,
                                                                      lambdaCreationOptions.CancellationToken);

                cleanup.Add(permCleanup);
            }
            catch (Exception)
            {
                log("Cleaning up resources");
                await Cleanup(cleanup);

                throw;
            }

            return(publishTopic, () => Cleanup(cleanup));
        }