private void ConfigureBeanstalkEnvironment()

in src/AWS.Deploy.Recipes/CdkTemplates/AspNetAppElasticBeanstalkWindows/Generated/Recipe.cs [207:500]


        private void ConfigureBeanstalkEnvironment(bool newDeployment, Configuration settings, string beanstalkApplicationName)
        {
            if (Ec2InstanceProfile == null)
                throw new InvalidOperationException($"{nameof(Ec2InstanceProfile)} has not been set. The {nameof(ConfigureIAM)} method should be called before {nameof(ConfigureBeanstalkEnvironment)}");
            if (ApplicationVersion == null)
                throw new InvalidOperationException($"{nameof(ApplicationVersion)} has not been set. The {nameof(ConfigureApplication)} method should be called before {nameof(ConfigureBeanstalkEnvironment)}");

            var optionSettingProperties = new List<CfnEnvironment.OptionSettingProperty> {
                   new CfnEnvironment.OptionSettingProperty {
                        Namespace = "aws:autoscaling:launchconfiguration",
                        OptionName =  "IamInstanceProfile",
                        Value = Ec2InstanceProfile.AttrArn
                   },
                   new CfnEnvironment.OptionSettingProperty {
                        Namespace = "aws:elasticbeanstalk:environment",
                        OptionName =  "EnvironmentType",
                        Value = settings.EnvironmentType
                   },
                   new CfnEnvironment.OptionSettingProperty
                   {
                        Namespace = "aws:elasticbeanstalk:managedactions",
                        OptionName = "ManagedActionsEnabled",
                        Value = settings.ElasticBeanstalkManagedPlatformUpdates.ManagedActionsEnabled.ToString().ToLower()
                   },
                   new CfnEnvironment.OptionSettingProperty
                   {
                        Namespace = "aws:elasticbeanstalk:xray",
                        OptionName = "XRayEnabled",
                        Value = settings.XRayTracingSupportEnabled.ToString().ToLower()
                   },
                   new CfnEnvironment.OptionSettingProperty
                   {
                        Namespace = "aws:elasticbeanstalk:healthreporting:system",
                        OptionName = "SystemType",
                        Value = settings.EnhancedHealthReporting
                   }
                };

            if (newDeployment ||
                (!newDeployment && !string.Equals(settings.IMDSv1Access, IMDS_V1_DEFAULT, StringComparison.InvariantCultureIgnoreCase)))
            {
                var computedDisableIMDSv1 = string.Equals(settings.IMDSv1Access, IMDS_V1_ENABLED, StringComparison.InvariantCultureIgnoreCase) ? "false" : "true";
                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:autoscaling:launchconfiguration",
                    OptionName = "DisableIMDSv1",
                    Value = computedDisableIMDSv1
                });
            }

            if (!string.IsNullOrEmpty(settings.InstanceType))
            {
                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:autoscaling:launchconfiguration",
                    OptionName = "InstanceType",
                    Value = settings.InstanceType
                });
            }

            if (settings.EnvironmentType.Equals(ENVIRONMENTTYPE_LOADBALANCED))
            {
                optionSettingProperties.Add(
                    new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:elasticbeanstalk:environment",
                        OptionName = "LoadBalancerType",
                        Value = settings.LoadBalancerType
                    }
                );

                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:ec2:vpc",
                    OptionName = "ELBScheme",
                    Value = settings.LoadBalancerScheme
                });

                if (!string.IsNullOrEmpty(settings.HealthCheckURL))
                {
                    optionSettingProperties.Add(
                        new CfnEnvironment.OptionSettingProperty
                        {
                            Namespace = "aws:elasticbeanstalk:application",
                            OptionName = "Application Healthcheck URL",
                            Value = settings.HealthCheckURL
                        }
                    );

                    optionSettingProperties.Add(
                        new CfnEnvironment.OptionSettingProperty
                        {
                            Namespace = "aws:elasticbeanstalk:environment:process:default",
                            OptionName = "HealthCheckPath",
                            Value = settings.HealthCheckURL
                        }
                    );
                }
            }

            if (!string.IsNullOrEmpty(settings.EC2KeyPair))
            {
                optionSettingProperties.Add(
                    new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:autoscaling:launchconfiguration",
                        OptionName = "EC2KeyName",
                        Value = settings.EC2KeyPair
                    }
                );
            }

            if (settings.ElasticBeanstalkManagedPlatformUpdates.ManagedActionsEnabled)
            {
                if (BeanstalkServiceRole == null)
                    throw new InvalidOrMissingConfigurationException("The Elastic Beanstalk service role cannot be null");

                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:elasticbeanstalk:environment",
                    OptionName = "ServiceRole",
                    Value = BeanstalkServiceRole.RoleArn
                });

                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:elasticbeanstalk:managedactions",
                    OptionName = "PreferredStartTime",
                    Value = settings.ElasticBeanstalkManagedPlatformUpdates.PreferredStartTime
                });

                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:elasticbeanstalk:managedactions:platformupdate",
                    OptionName = "UpdateLevel",
                    Value = settings.ElasticBeanstalkManagedPlatformUpdates.UpdateLevel
                });
            }
            
            if (settings.ElasticBeanstalkRollingUpdates.RollingUpdatesEnabled)
            {
                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:autoscaling:updatepolicy:rollingupdate",
                    OptionName = "RollingUpdateEnabled",
                    Value = settings.ElasticBeanstalkRollingUpdates.RollingUpdatesEnabled.ToString().ToLower()
                });

                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:autoscaling:updatepolicy:rollingupdate",
                    OptionName = "RollingUpdateType",
                    Value = settings.ElasticBeanstalkRollingUpdates.RollingUpdateType
                });

                optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                {
                    Namespace = "aws:autoscaling:updatepolicy:rollingupdate",
                    OptionName = "Timeout",
                    Value = settings.ElasticBeanstalkRollingUpdates.Timeout
                });

                if (settings.ElasticBeanstalkRollingUpdates.MaxBatchSize != null)
                {
                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:autoscaling:updatepolicy:rollingupdate",
                        OptionName = "MaxBatchSize",
                        Value = settings.ElasticBeanstalkRollingUpdates.MaxBatchSize.ToString()
                    });
                }

                if (settings.ElasticBeanstalkRollingUpdates.MinInstancesInService != null)
                {
                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:autoscaling:updatepolicy:rollingupdate",
                        OptionName = "MinInstancesInService",
                        Value = settings.ElasticBeanstalkRollingUpdates.MinInstancesInService.ToString()
                    });
                }

                if (settings.ElasticBeanstalkRollingUpdates.PauseTime != null)
                {
                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:autoscaling:updatepolicy:rollingupdate",
                        OptionName = "PauseTime",
                        Value = settings.ElasticBeanstalkRollingUpdates.PauseTime
                    });
                }
            }

            if (settings.ElasticBeanstalkEnvironmentVariables != null)
            {
                foreach (var (key, value) in settings.ElasticBeanstalkEnvironmentVariables)
                {
                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:elasticbeanstalk:application:environment",
                        OptionName = key,
                        Value = value
                    });
                }
            }

            if (settings.VPC.UseVPC)
            {
                if (settings.VPC.CreateNew)
                {
                    if (AppVpc == null)
                        throw new InvalidOperationException($"{nameof(AppVpc)} has not been set. The {nameof(ConfigureVpc)} method should be called before {nameof(ConfigureBeanstalkEnvironment)}");

                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:ec2:vpc",
                        OptionName = "VPCId",
                        Value = AppVpc.VpcId
                    });

                    if (settings.EnvironmentType.Equals(ENVIRONMENTTYPE_SINGLEINSTANCE))
                    {
                        optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                        {
                            Namespace = "aws:ec2:vpc",
                            OptionName = "Subnets",
                            Value = string.Join(",", AppVpc.PublicSubnets.Select(x => x.SubnetId))
                        });
                    }
                    else if (settings.EnvironmentType.Equals(ENVIRONMENTTYPE_LOADBALANCED))
                    {
                        optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                        {
                            Namespace = "aws:ec2:vpc",
                            OptionName = "Subnets",
                            Value = string.Join(",", AppVpc.PrivateSubnets.Select(x => x.SubnetId))
                        });
                        optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                        {
                            Namespace = "aws:ec2:vpc",
                            OptionName = "ELBSubnets",
                            Value = string.Join(",", AppVpc.PublicSubnets.Select(x => x.SubnetId))
                        });
                    }

                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:autoscaling:launchconfiguration",
                        OptionName = "SecurityGroups",
                        Value = AppVpc.VpcDefaultSecurityGroup
                    });
                }
                else
                {
                    optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                    {
                        Namespace = "aws:ec2:vpc",
                        OptionName = "VPCId",
                        Value = settings.VPC.VpcId
                    });

                    if (settings.VPC.Subnets.Any())
                    {
                        optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                        {
                            Namespace = "aws:ec2:vpc",
                            OptionName = "Subnets",
                            Value = string.Join(",", settings.VPC.Subnets)
                        });

                        if (settings.VPC.SecurityGroups.Any())
                        {
                            optionSettingProperties.Add(new CfnEnvironment.OptionSettingProperty
                            {
                                Namespace = "aws:autoscaling:launchconfiguration",
                                OptionName = "SecurityGroups",
                                Value = string.Join(",", settings.VPC.SecurityGroups)
                            });
                        }
                    }
                }
            }

            BeanstalkEnvironment = new CfnEnvironment(this, nameof(BeanstalkEnvironment), InvokeCustomizeCDKPropsEvent(nameof(BeanstalkEnvironment), this, new CfnEnvironmentProps
            {
                EnvironmentName = settings.EnvironmentName,
                ApplicationName = beanstalkApplicationName,
                PlatformArn = settings.ElasticBeanstalkPlatformArn,
                OptionSettings = optionSettingProperties.ToArray(),
                CnamePrefix = !string.IsNullOrEmpty(settings.CNamePrefix) ? settings.CNamePrefix : null,
                // This line is critical - reference the label created in this same stack
                VersionLabel = ApplicationVersion.Ref,
            }));
        }