Ejemplo n.º 1
0
 public void AddAutoScalingGroupToCluster(string asgId, Amazon.CDK.AWS.AutoScaling.AutoScalingGroup autoScalingGroup, Cluster cluster)
 {
     cluster.AddAsgCapacityProvider(new AsgCapacityProvider(Scope, $"{asgId}-capacityprovider", new AsgCapacityProviderProps
     {
         AutoScalingGroup = autoScalingGroup
     }));
 }
        public Amazon.CDK.AWS.AutoScaling.AutoScalingGroup Create(Amazon.CDK.AWS.EC2.Vpc vpc, SecurityGroup sg)
        {
            // todo define roles in config
            var role      = new Security.Roles.IamRole().Create(this, _config, "asg-ec2-role");
            var selection = new SubnetSelection
            {
                SubnetType = SubnetType.PUBLIC
            };

            var healchCheck = HealthCheck.Elb(new ElbHealthCheckOptions
            {
                Grace = Duration.Minutes(5)
            });

            var asg = new Amazon.CDK.AWS.AutoScaling.AutoScalingGroup(this, _config.Asg.Name, new AutoScalingGroupProps
            {
                AutoScalingGroupName = _config.Asg.Name,
                Vpc = vpc,
                // todo parse enums and pull from config
                InstanceType = InstanceType.Of(InstanceClass.BURSTABLE3, InstanceSize.MICRO),
                // get the linux two type otherwise it defaults to the older image
                // todo parse enums and pull from config
                MachineImage = new AmazonLinuxImage(new AmazonLinuxImageProps {
                    Generation = AmazonLinuxGeneration.AMAZON_LINUX_2
                }),
                AllowAllOutbound = _config.Asg.AllowAllOutbound,
                DesiredCapacity  = _config.Asg.DesiredCapacity,
                MinCapacity      = _config.Asg.MinCapacity,
                MaxCapacity      = _config.Asg.MaxCapacity,

                KeyName = _config.Asg.KeyName,
                AssociatePublicIpAddress = _config.Asg.AssociatePublicIpAddress,
                VpcSubnets    = selection,
                Role          = role,
                UserData      = GetUserData(_config.Asg.UserDataPath),
                HealthCheck   = healchCheck,
                SecurityGroup = sg
            });

            Utilities.Tagging.Tag(asg, _config, _config.Asg.Tags);
            Utilities.Tagging.Tag(asg, _config, _config.Tags);

            //asg.ScaleOnCpuUtilization()


            return(asg);
        }
        public ApplicationLoadBalancer Create(Construct construct, Amazon.CDK.AWS.EC2.Vpc vpc, Amazon.CDK.AWS.AutoScaling.AutoScalingGroup asg, SecurityGroup sg)
        {
            var lb = new ApplicationLoadBalancer(construct, _config.Alb.Name, new ApplicationLoadBalancerProps
            {
                Vpc              = vpc,
                InternetFacing   = true,
                LoadBalancerName = _config.Alb.Name,
                SecurityGroup    = sg
            });

            Amazon.CDK.Tags.Of(lb).Add("Name", $"{_config.Alb.Name}");

            // add a listener
            var listener = AddListener(lb, 80, null);
            var appPort  = 80;
            var group    = listener.AddTargets($"AppFleet", new AddApplicationTargetsProps
            {
                Port    = appPort,
                Targets = new[] { asg }
            });

            // add specific tags
            Amazon.CDK.Tags.Of(listener).Add("Name", $"{_config.Alb.Name}-listner");
            Amazon.CDK.Tags.Of(group).Add("Name", $"{_config.Alb.Name}-fleet");


            // exmple of a fixed ok message returned by the LB
            listener.AddAction($"FixedOkMessage", new AddApplicationActionProps
            {
                Priority   = 10,
                Conditions = new[] { ListenerCondition.PathPatterns(new[] { "/ok" }) },
                Action     = ListenerAction.FixedResponse(200, new FixedResponseOptions
                {
                    ContentType = "text/html",
                    MessageBody = "OK"
                })
            });

            // example of a fixed health status message returned by LB
            listener.AddAction($"LBHealthInfo", new AddApplicationActionProps
            {
                Priority   = 15,
                Conditions = new[] { ListenerCondition.PathPatterns(new[] { "/lb-status" }) },
                Action     = ListenerAction.FixedResponse(200, new FixedResponseOptions
                {
                    ContentType = "application/json",
                    MessageBody = "{ \"lb\": { \"type\": \"application-load-balancer\", \"launchDateUtc\": \"{" + DateTime.UtcNow + "}\", \"status\": \"ok\" } }"
                })
            });

            //"arn:aws:acm:us-east-1:xxxxxxxxx:certificate/eb2b584c-421d-4134-b679-1746642b5e3f"
            if (_config.Alb.CertArn != null)
            {
                listener = AddListener(lb, 443, _config.Alb.CertArn);

                // forward any ssl requests to the target group
                listener.AddAction("SSLForward", new AddApplicationActionProps
                {
                    Action = ListenerAction.Forward(new[] { group }),
                });
            }


            return(lb);
        }