|
| 1 | +/** |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +// Imports |
| 15 | +import * as defaults from "@aws-solutions-constructs/core"; |
| 16 | +import * as elb from "@aws-cdk/aws-elasticloadbalancingv2"; |
| 17 | +import * as s3 from "@aws-cdk/aws-s3"; |
| 18 | +import * as r53 from "@aws-cdk/aws-route53"; |
| 19 | +import * as r53t from '@aws-cdk/aws-route53-targets'; |
| 20 | +// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate |
| 21 | +import { Construct } from '@aws-cdk/core'; |
| 22 | +import * as ec2 from '@aws-cdk/aws-ec2'; |
| 23 | + |
| 24 | +export interface Route53ToAlbProps { |
| 25 | + /** |
| 26 | + * Custom properties for a new Private Hosted Zone. Cannot be specified for a |
| 27 | + * public API. Cannot specify a VPC |
| 28 | + * |
| 29 | + * @default - None |
| 30 | + */ |
| 31 | + readonly privateHostedZoneProps?: r53.PrivateHostedZoneProps | any, |
| 32 | + /** |
| 33 | + * Existing Public or Private Hosted Zone. If a Private Hosted Zone, must |
| 34 | + * exist in the same VPC specified in existingVpc |
| 35 | + * |
| 36 | + * @default - None |
| 37 | + */ |
| 38 | + readonly existingHostedZoneInterface?: r53.IHostedZone, |
| 39 | + /** |
| 40 | + * Custom properties for a new ALB. Providing both this and existingLoadBalancerObj |
| 41 | + * is an error. These properties cannot include a VPC. |
| 42 | + * |
| 43 | + * @default - None |
| 44 | + */ |
| 45 | + readonly loadBalancerProps?: elb.ApplicationLoadBalancerProps | any, |
| 46 | + /** |
| 47 | + * An existing Application Load Balancer. Providing both this and loadBalancerProps |
| 48 | + * is an error. This ALB must exist in the same VPC specified in existingVPC |
| 49 | + * |
| 50 | + * @default - None |
| 51 | + */ |
| 52 | + readonly existingLoadBalancerObj?: elb.ApplicationLoadBalancer, |
| 53 | + /** |
| 54 | + * Whether to turn on Access Logs for the Application Load Balancer. Uses an S3 bucket |
| 55 | + * with associated storage costs. Enabling Access Logging is a best practice. |
| 56 | + * |
| 57 | + * @default - true |
| 58 | + */ |
| 59 | + readonly logAccessLogs?: boolean, |
| 60 | + /** |
| 61 | + * Optional properties to customize the bucket used to store the ALB Access |
| 62 | + * Logs. Supplying this and setting logAccessLogs to false is an error. |
| 63 | + * |
| 64 | + * @default - none |
| 65 | + */ |
| 66 | + readonly loggingBucketProps?: s3.BucketProps, |
| 67 | + /** |
| 68 | + * Custom properties for a new VPC. Providing both this and existingVpc is |
| 69 | + * an error. If an existingAlb or existing Private Hosted Zone is provided, those |
| 70 | + * already exist in a VPC so this value cannot be provided. |
| 71 | + * |
| 72 | + * @default - None |
| 73 | + */ |
| 74 | + readonly vpcProps?: ec2.VpcProps, |
| 75 | + /** |
| 76 | + * An existing VPC. Providing both this and vpcProps is an error. If an existingAlb or existing |
| 77 | + * Private Hosted Zone is provided, this value must be the VPC associated with those resources. |
| 78 | + * |
| 79 | + * @default - None |
| 80 | + */ |
| 81 | + readonly existingVpc?: ec2.IVpc, |
| 82 | + /** |
| 83 | + * Whether to create a public or private API. This value has implications |
| 84 | + * for the VPC, the type of Hosted Zone and the Application Load Balancer |
| 85 | + * |
| 86 | + * @default - None |
| 87 | + */ |
| 88 | + readonly publicApi: boolean |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @summary Configures a Route53 Hosted Zone to route to an Application Load Balancer |
| 93 | + */ |
| 94 | +export class Route53ToAlb extends Construct { |
| 95 | + public readonly hostedZone: r53.IHostedZone; |
| 96 | + public readonly vpc: ec2.IVpc; |
| 97 | + public readonly loadBalancer: elb.ApplicationLoadBalancer; |
| 98 | + |
| 99 | + /** |
| 100 | + * @summary Constructs a new instance of the Route53ToAlb class. |
| 101 | + * @param {cdk.App} scope - represents the scope for all the resources. |
| 102 | + * @param {string} id - this is a a scope-unique id. |
| 103 | + * @param {Route53ToAlbProps} props - user provided props for the construct. |
| 104 | + * @access public |
| 105 | + */ |
| 106 | + constructor(scope: Construct, id: string, props: Route53ToAlbProps) { |
| 107 | + super(scope, id); |
| 108 | + defaults.CheckProps(props); |
| 109 | + |
| 110 | + if ((props?.logAccessLogs === false) && (props.loggingBucketProps)) { |
| 111 | + throw new Error('If logAccessLogs is false, supplying loggingBucketProps is invalid.'); |
| 112 | + } |
| 113 | + |
| 114 | + if (props?.loadBalancerProps?.vpc) { |
| 115 | + throw new Error('Specify any existing VPC at the construct level, not within loadBalancerProps.'); |
| 116 | + } |
| 117 | + |
| 118 | + if (props.existingLoadBalancerObj && !props.existingVpc) { |
| 119 | + throw new Error('An existing ALB already exists in a VPC, so that VPC must be passed to the construct in props.existingVpc'); |
| 120 | + } |
| 121 | + |
| 122 | + if (props.existingHostedZoneInterface && !props.publicApi && !props.existingVpc) { |
| 123 | + throw new Error('An existing Private Hosted Zone already exists in a VPC, so that VPC must be passed to the construct in props.existingVpc'); |
| 124 | + } |
| 125 | + |
| 126 | + if (props.existingVpc) { |
| 127 | + this.vpc = props.existingVpc; |
| 128 | + } else { |
| 129 | + this.vpc = defaults.buildVpc(scope, { |
| 130 | + defaultVpcProps: props.publicApi ? |
| 131 | + defaults.DefaultPublicPrivateVpcProps() : |
| 132 | + // If this is an internal app, we're going to turn on DNS |
| 133 | + // by default to allow gateway and interface service endpoints |
| 134 | + defaults.overrideProps(defaults.DefaultIsolatedVpcProps(), { enableDnsHostnames: true, enableDnsSupport: true, }), |
| 135 | + userVpcProps: props.vpcProps, |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + if (props.existingHostedZoneInterface) { |
| 140 | + this.hostedZone = props.existingHostedZoneInterface; |
| 141 | + } else { |
| 142 | + if (props.publicApi) { |
| 143 | + throw new Error('Public APIs require an existingHostedZone be passed in the Props object.'); |
| 144 | + } else { |
| 145 | + if (!props.privateHostedZoneProps) { |
| 146 | + throw new Error('Must supply privateHostedZoneProps to create a private API'); |
| 147 | + } |
| 148 | + if (props.privateHostedZoneProps.vpc) { |
| 149 | + throw new Error('All VPC specs must be provided at the Construct level in Route53ToAlbProps'); |
| 150 | + } |
| 151 | + const manufacturedProps: r53.PrivateHostedZoneProps = defaults.overrideProps(props.privateHostedZoneProps, { vpc: this.vpc }); |
| 152 | + this.hostedZone = new r53.PrivateHostedZone(this, `${id}-zone`, manufacturedProps); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + this.loadBalancer = defaults.ObtainAlb( |
| 157 | + this, |
| 158 | + id, |
| 159 | + this.vpc, |
| 160 | + props.publicApi, |
| 161 | + props.existingLoadBalancerObj, |
| 162 | + props.loadBalancerProps, |
| 163 | + props.logAccessLogs, |
| 164 | + props.loggingBucketProps |
| 165 | + ); |
| 166 | + |
| 167 | + // Add the ALB to the HostedZone as a target |
| 168 | + const hostedZoneTarget = new r53t.LoadBalancerTarget(this.loadBalancer); |
| 169 | + |
| 170 | + new r53.ARecord(this, `${id}-alias`, { |
| 171 | + target: { aliasTarget: hostedZoneTarget }, |
| 172 | + zone: this.hostedZone |
| 173 | + }); |
| 174 | + } |
| 175 | +} |
0 commit comments