Exemplo n.º 1
0
        private async Task CreateFunctionAsync()
        {
            var tryCount = 3;

            while (true)
            {
                try
                {
                    await _lambdaClient.CreateFunctionAsync(new CreateFunctionRequest
                    {
                        FunctionName = _functionName,
                        Code         = new FunctionCode
                        {
                            ImageUri = _imageUri
                        },
                        MemorySize  = 512,
                        Role        = _executionRoleArn,
                        PackageType = PackageType.Image
                    });

                    break;
                }
                catch (InvalidParameterValueException)
                {
                    tryCount--;
                    if (tryCount == 0)
                    {
                        throw;
                    }

                    // Wait another 5 seconds to let execution role propagate
                    await Task.Delay(5000);
                }
            }

            var endTime  = DateTime.Now.AddSeconds(30);
            var isActive = false;

            while (DateTime.Now < endTime)
            {
                var response = await _lambdaClient.GetFunctionConfigurationAsync(new GetFunctionConfigurationRequest
                {
                    FunctionName = _functionName
                });

                if (response.State == State.Active)
                {
                    isActive = true;
                    break;
                }

                await Task.Delay(2000);
            }

            if (!isActive)
            {
                throw new Exception($"Timed out trying to create Lambda function {_functionName}");
            }
        }