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);
        }
Exemplo n.º 2
0
        internal InfraStack(Construct scope, string id, CustomStackProps props = null)
            : base(scope, id, props)
        {
            var vpc = props.Vpc;

            var cluster = new Cluster(this, "Cluster",
                                      new ClusterProps
            {
                Vpc         = vpc,
                ClusterName = Globals.GetDeployEnvironment(this).PutEnvNamePrefixWithDash("Cluster")
            });

            var albSecurityGroup = new SecurityGroup(this, "AlbSecurityGroup",
                                                     new SecurityGroupProps
            {
                Vpc = vpc,
                AllowAllOutbound = true
            });

            albSecurityGroup.AddIngressRule(Peer.AnyIpv4(), Port.Tcp(80));

            var alb = new ApplicationLoadBalancer(this, "ALB",
                                                  new ApplicationLoadBalancerProps
            {
                Vpc            = vpc,
                InternetFacing = true,
                Http2Enabled   = true,
                IdleTimeout    = Duration.Seconds(60),
                IpAddressType  = IpAddressType.IPV4,
                SecurityGroup  = albSecurityGroup
            });

            var webApiServiceSecurityGroup = new SecurityGroup(this, "WebApiServiceSecurityGroup",
                                                               new SecurityGroupProps
            {
                Vpc = vpc,
                AllowAllOutbound = true
            });

            webApiServiceSecurityGroup.AddIngressRule(albSecurityGroup, Port.Tcp(80));

            var appListener = alb.AddListener("AppListener",
                                              new BaseApplicationListenerProps
            {
                Port          = 80,
                Protocol      = ApplicationProtocol.HTTP,
                DefaultAction = ListenerAction.FixedResponse(404,
                                                             new FixedResponseOptions
                {
                    ContentType = "text/plain",
                    MessageBody = "This is not here..."
                })
            });

            new CfnOutput(this, "ClusterName",
                          new CfnOutputProps
            {
                ExportName = Globals.GetDeployEnvironment(this).PutEnvNamePrefixWithDash("ClusterName"),
                Value      = cluster.ClusterName
            });

            new CfnOutput(this, "WebApiServiceSecurityGroupId",
                          new CfnOutputProps
            {
                ExportName = Globals.GetDeployEnvironment(this).PutEnvNamePrefixWithDash("WebApiServiceSecurityGroupId"),
                Value      = albSecurityGroup.SecurityGroupId
            });

            new CfnOutput(this, "AppListenerArn",
                          new CfnOutputProps
            {
                ExportName = Globals.GetDeployEnvironment(this).PutEnvNamePrefixWithDash("AppListenerArn"),
                Value      = appListener.ListenerArn
            });
        }