-
-
Save baronfel/cfbadf9de44e6501ebfd9467b7131d42 to your computer and use it in GitHub Desktop.
sample AWS ALB networking for a multi-service app with SSL termination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This ALB will be responsible for routing traffic to our services | |
| const alb = new awsx.lb.ApplicationLoadBalancer("app-server-lb", { | |
| vpc: vpc, | |
| // marking this as 'external' ties in to the listeners below to ensure that they are available to the entire internet | |
| external: true, | |
| securityGroups: cluster.securityGroups, | |
| tags: merge(defaultTags, name("app-lb")), | |
| }); | |
| // This target group is used to configure the protocol/port/healthcheck for the containers we'll be connecting to | |
| const appServerTargetGroup = alb.createTargetGroup("app-server-tg", { | |
| port: 80, | |
| protocol: "HTTP", | |
| healthCheck: { | |
| path: "/healthcheck", | |
| port: "80", | |
| protocol: "HTTP", | |
| matcher: "200", | |
| } | |
| }); | |
| const adminPathBase = "/admin"; | |
| // This TG is used as a routing structure. ALBs can point to it as a target, and Fargate Services point to it to | |
| // place containers in it | |
| const appAdminTargetGroup = alb.createTargetGroup("app-admin-tg", { | |
| port: 80, | |
| protocol: "HTTP", | |
| healthCheck: { | |
| path: `${adminPathBase}/healthcheck`, | |
| port: "80", | |
| protocol: "HTTP", | |
| matcher: "200", | |
| } | |
| }); | |
| // creating this listener creates ingress/egress rules for this port, | |
| // because the listener is 'external' as well as the ALB the listener belongs to. | |
| // redirect to https when an http action is detected | |
| const appServerHttpRedirector = alb.createListener("app-server-http", { | |
| port: 80, | |
| protocol: "HTTP", | |
| external: true, | |
| defaultAction: { | |
| redirect: { | |
| statusCode: "HTTP_302", | |
| protocol: "HTTPS", | |
| port: "443" | |
| }, | |
| type: "redirect" | |
| } | |
| }); | |
| // listen for https traffic. by default returns 404, will have rules added to forward to the requisite groups | |
| const appHttpsListener = alb.createListener("app-server-https", { | |
| port: 443, | |
| protocol: "HTTPS", | |
| external: true, | |
| defaultAction: { | |
| type: "fixed-response", | |
| fixedResponse: { | |
| contentType: "text/plain", | |
| statusCode: "404" | |
| } | |
| }, | |
| certificateArn: cert.arn | |
| }); | |
| /// this listener forwards all traffic to the app server | |
| appHttpsListener.addListenerRule("plain-traffic-to-app-server", { | |
| priority: 49999, // priority goes from 1 to 50000 with lower numbers going first, so this makes the rule always eval last | |
| conditions: [{ httpRequestMethod: { values: ["GET", "PUT", "POST", "HEAD", "OPTIONS"] } } as aws.types.output.lb.ListenerRuleCondition], | |
| actions: [ | |
| { | |
| type: "forward", | |
| targetGroupArn: appServerTargetGroup.targetGroup.arn | |
| } | |
| ] | |
| }); | |
| /// this one forwards to the admin UI, based on /admin path | |
| appHttpsListener.addListenerRule("admin-traffic-to-app-admin", { | |
| priority: 10, | |
| conditions: [{ pathPattern: { values: ["/admin*"] } }], | |
| actions: [{ type: "forward", targetGroupArn: appAdminTargetGroup.targetGroup.arn }] | |
| }); | |
| // from this point, create your services as usual, just use `appServerTargetGroup` or `appAdminTargetGroup` for your `portMappings` property. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment