private async Task CreateEnvironment()

in src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs [326:438]


        private async Task<string> CreateEnvironment(string application, string environment, string versionLabel, bool isWindowsEnvironment)
        {
            var createRequest = new CreateEnvironmentRequest
            {
                ApplicationName = application,
                EnvironmentName = environment,
                VersionLabel = versionLabel,
                SolutionStackName = this.GetSolutionStackOrDefault(this.DeployEnvironmentOptions.SolutionStack, EBDefinedCommandOptions.ARGUMENT_SOLUTION_STACK, true),
                CNAMEPrefix = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.CNamePrefix, EBDefinedCommandOptions.ARGUMENT_CNAME_PREFIX, false)
            };

            string environmentType, loadBalancerType;
            DetermineEnvironment(out environmentType, out loadBalancerType);

            if (!string.IsNullOrEmpty(environmentType))
            {
                createRequest.OptionSettings.Add(new ConfigurationOptionSetting()
                {
                    Namespace = "aws:elasticbeanstalk:environment",
                    OptionName = "EnvironmentType",
                    Value = environmentType
                });
            }

            var ec2KeyPair = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.EC2KeyPair, EBDefinedCommandOptions.ARGUMENT_EC2_KEYPAIR, false);
            if (!string.IsNullOrEmpty(ec2KeyPair))
            {
                createRequest.OptionSettings.Add(new ConfigurationOptionSetting()
                {
                    Namespace = "aws:autoscaling:launchconfiguration",
                    OptionName = "EC2KeyName",
                    Value = ec2KeyPair
                });
            }

            var instanceType = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.InstanceType, EBDefinedCommandOptions.ARGUMENT_INSTANCE_TYPE, false);
            if (string.IsNullOrEmpty(instanceType))
            {
                instanceType = isWindowsEnvironment ? EBConstants.DEFAULT_WINDOWS_INSTANCE_TYPE : EBConstants.DEFAULT_LINUX_INSTANCE_TYPE;
            }
                

            createRequest.OptionSettings.Add(new ConfigurationOptionSetting()
            {
                Namespace = "aws:autoscaling:launchconfiguration",
                OptionName = "InstanceType",
                Value = instanceType
            });

            var instanceProfile = this.GetInstanceProfileOrDefault(this.DeployEnvironmentOptions.InstanceProfile, EBDefinedCommandOptions.ARGUMENT_INSTANCE_PROFILE, true, string.Format("eb_{0}_{1}", application, environment));
            if (!string.IsNullOrEmpty(instanceProfile))
            {
                int pos = instanceProfile.LastIndexOf('/');
                if (pos != -1)
                {
                    instanceProfile = instanceProfile.Substring(pos + 1);
                }

                createRequest.OptionSettings.Add(new ConfigurationOptionSetting()
                {
                    Namespace = "aws:autoscaling:launchconfiguration",
                    OptionName = "IamInstanceProfile",
                    Value = instanceProfile
                });
            }

            var serviceRole = this.GetServiceRoleOrCreateIt(this.DeployEnvironmentOptions.ServiceRole, EBDefinedCommandOptions.ARGUMENT_SERVICE_ROLE, 
                "aws-elasticbeanstalk-service-role", Constants.ELASTICBEANSTALK_ASSUME_ROLE_POLICY, null, "AWSElasticBeanstalkManagedUpdatesCustomerRolePolicy", "AWSElasticBeanstalkEnhancedHealth");
            if (!string.IsNullOrEmpty(serviceRole))
            {
                int pos = serviceRole.LastIndexOf('/');
                if(pos != -1)
                {
                    serviceRole = serviceRole.Substring(pos + 1);
                }

                createRequest.OptionSettings.Add(new ConfigurationOptionSetting()
                {
                    Namespace = "aws:elasticbeanstalk:environment",
                    OptionName = "ServiceRole",
                    Value = serviceRole 
                });
            }

            if (!string.IsNullOrWhiteSpace(loadBalancerType))
            {
                if (!EBConstants.ValidLoadBalancerType.Contains(loadBalancerType))
                    throw new ElasticBeanstalkExceptions($"The loadbalancer type {loadBalancerType} is invalid. Valid values are: {string.Join(", ", EBConstants.ValidLoadBalancerType)}", ElasticBeanstalkExceptions.EBCode.InvalidLoadBalancerType);

                createRequest.OptionSettings.Add(new ConfigurationOptionSetting()
                {
                    Namespace = "aws:elasticbeanstalk:environment",
                    OptionName = "LoadBalancerType",
                    Value = loadBalancerType
                });
            }

            AddAdditionalOptions(createRequest.OptionSettings, true, isWindowsEnvironment);

            var tags = ConvertToTagsCollection();
            if (tags != null && tags.Count > 0)
                createRequest.Tags = tags;

            try
            {
                var createResponse = await this.EBClient.CreateEnvironmentAsync(createRequest);
                return createResponse.EnvironmentArn;
            }
            catch (Exception e)
            {
                throw new ElasticBeanstalkExceptions("Error creating environment: " + e.Message, ElasticBeanstalkExceptions.EBCode.FailedToCreateEnvironment);
            }
        }