-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
6,179 additions
and
109 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
source/patterns/@aws-solutions-constructs/aws-alb-fargate/.eslintignore
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lib/*.js | ||
test/*.js | ||
*.d.ts | ||
coverage |
15 changes: 15 additions & 0 deletions
15
source/patterns/@aws-solutions-constructs/aws-alb-fargate/.gitignore
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
lib/*.js | ||
test/*.js | ||
*.js.map | ||
*.d.ts | ||
node_modules | ||
*.generated.ts | ||
dist | ||
.jsii | ||
|
||
.LAST_BUILD | ||
.nyc_output | ||
coverage | ||
.nycrc | ||
.LAST_PACKAGE | ||
*.snk |
21 changes: 21 additions & 0 deletions
21
source/patterns/@aws-solutions-constructs/aws-alb-fargate/.npmignore
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Exclude typescript source and config | ||
*.ts | ||
tsconfig.json | ||
coverage | ||
.nyc_output | ||
*.tgz | ||
*.snk | ||
*.tsbuildinfo | ||
|
||
# Include javascript files and typescript declarations | ||
!*.js | ||
!*.d.ts | ||
|
||
# Exclude jsii outdir | ||
dist | ||
|
||
# Include .jsii | ||
!.jsii | ||
|
||
# Include .jsii | ||
!.jsii |
68 changes: 68 additions & 0 deletions
68
source/patterns/@aws-solutions-constructs/aws-alb-fargate/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# aws-alb-fargate module | ||
<!--BEGIN STABILITY BANNER--> | ||
|
||
--- | ||
|
||
![Stability: Experimental](https://img.shields.io/badge/stability-Experimental-important.svg?style=for-the-badge) | ||
|
||
> All classes are under active development and subject to non-backward compatible changes or removal in any | ||
> future version. These are not subject to the [Semantic Versioning](https://semver.org/) model. | ||
> This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package. | ||
--- | ||
<!--END STABILITY BANNER--> | ||
|
||
| **Reference Documentation**:| <span style="font-weight: normal">https://docs.aws.amazon.com/solutions/latest/constructs/</span>| | ||
|:-------------|:-------------| | ||
<div style="height:8px"></div> | ||
|
||
| **Language** | **Package** | | ||
|:-------------|-----------------| | ||
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`aws_solutions_constructs.aws_alb_fargate`| | ||
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@aws-solutions-constructs/aws-alb-fargate`| | ||
|![Java Logo](https://docs.aws.amazon.com/cdk/api/latest/img/java32.png) Java|`software.amazon.awsconstructs.services.albfargate`| | ||
|
||
This AWS Solutions Construct implements an an Application Load Balancer to an AWS Fargate service | ||
|
||
Here is a minimal deployable pattern definition in Typescript: | ||
|
||
``` typescript | ||
|
||
``` | ||
|
||
## Initializer | ||
|
||
``` text | ||
new AlbToFargate(scope: Construct, id: string, props: AlbToFargateProps); | ||
``` | ||
|
||
_Parameters_ | ||
|
||
* scope [`Construct`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html) | ||
* id `string` | ||
* props [`AlbToFargateProps`](#pattern-construct-props) | ||
|
||
## Pattern Construct Props | ||
|
||
| **Name** | **Type** | **Description** | | ||
|:-------------|:----------------|-----------------| | ||
|
||
## Pattern Properties | ||
|
||
| **Name** | **Type** | **Description** | | ||
|:-------------|:----------------|-----------------| | ||
|
||
## Default settings | ||
|
||
Out of the box implementation of the Construct without any override will set the following defaults: | ||
|
||
### Application Load Balancer | ||
* Creates or configures an Application Load Balancer with: | ||
|
||
### AWS Fargate Service | ||
|
||
## Architecture | ||
![Architecture Diagram](architecture.png) | ||
|
||
*** | ||
© Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
Binary file added
BIN
+122 KB
source/patterns/@aws-solutions-constructs/aws-alb-fargate/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
263 changes: 263 additions & 0 deletions
263
source/patterns/@aws-solutions-constructs/aws-alb-fargate/lib/index.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
/** | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
import * as elb from "@aws-cdk/aws-elasticloadbalancingv2"; | ||
import * as ec2 from "@aws-cdk/aws-ec2"; | ||
import * as s3 from "@aws-cdk/aws-s3"; | ||
import { Construct } from "@aws-cdk/core"; | ||
import * as defaults from "@aws-solutions-constructs/core"; | ||
import * as ecs from "@aws-cdk/aws-ecs"; | ||
import { GetActiveListener } from "@aws-solutions-constructs/core"; | ||
import { CfnListener, CfnTargetGroup } from "@aws-cdk/aws-elasticloadbalancingv2"; | ||
|
||
export interface AlbToFargateProps { | ||
/** | ||
* Optional custom properties for a new loadBalancer. Providing both this and | ||
* existingLoadBalancer is an error. This cannot specify a VPC, it will use the VPC | ||
* in existingVpc or the VPC created by the construct. | ||
* | ||
* @default - none | ||
*/ | ||
readonly loadBalancerProps?: elb.ApplicationLoadBalancerProps | any; | ||
/** | ||
* Existing Application Load Balancer to incorporate into the | ||
* construct architecture. Providing both this and loadBalancerProps is an | ||
* error. The VPC containing this loadBalancer must match the VPC provided in existingVpc. | ||
* | ||
* @default - none | ||
*/ | ||
readonly existingLoadBalancerObj?: elb.ApplicationLoadBalancer; | ||
/** | ||
* Props to define the listener. Must be provided when adding the listener | ||
* to an ALB (eg - when creating the alb), may not be provided when adding | ||
* a second target to an already established listener. When provided, must include | ||
* either a certificate or protocol: HTTP | ||
* | ||
* @default - none | ||
*/ | ||
readonly listenerProps?: elb.ApplicationListenerProps | any; | ||
/** | ||
* Optional custom properties for a new target group. While this is a standard | ||
* attribute of props for ALB constructs, there are few pertinent properties for a Lambda target. | ||
* | ||
* @default - none | ||
* | ||
*/ | ||
readonly targetGroupProps?: elb.ApplicationTargetGroupProps; | ||
/** | ||
* Rules for directing traffic to the target being created. May not be specified | ||
* for the first listener added to an ALB, and must be specified for the second | ||
* target added to a listener. Add a second target by instantiating this construct a | ||
* second time and providing the existingAlb from the first instantiation. | ||
* | ||
* @default - none | ||
*/ | ||
readonly ruleProps?: elb.AddRuleProps; | ||
/** | ||
* Optional custom properties for a VPC the construct will create. This VPC will | ||
* be used by the new ALB and any Private Hosted Zone the construct creates (that's | ||
* why loadBalancerProps and privateHostedZoneProps can't include a VPC). Providing | ||
* both this and existingVpc is an error. | ||
* | ||
* @default - none | ||
*/ | ||
readonly vpcProps?: ec2.VpcProps; | ||
/** | ||
* An existing VPC in which to deploy the construct. Providing both this and | ||
* vpcProps is an error. If the client provides an existing load balancer and/or | ||
* existing Private Hosted Zone, those constructs must exist in this VPC. | ||
* | ||
* @default - none | ||
*/ | ||
readonly existingVpc?: ec2.IVpc; | ||
/** | ||
* Whether to turn on Access Logs for the Application Load Balancer. Uses an S3 bucket | ||
* with associated storage costs. Enabling Access Logging is a best practice. | ||
* | ||
* @default - true | ||
*/ | ||
readonly logAlbAccessLogs?: boolean; | ||
/** | ||
* Optional properties to customize the bucket used to store the ALB Access | ||
* Logs. Supplying this and setting logAccessLogs to false is an error. | ||
* | ||
* @default - none | ||
*/ | ||
readonly albLoggingBucketProps?: s3.BucketProps; | ||
/** | ||
* Whether the construct is deploying a private or public API. This has implications for the VPC and ALB. | ||
* | ||
* @default - none | ||
*/ | ||
readonly publicApi: boolean; | ||
/** | ||
* Optional properties to create a new ECS cluster | ||
*/ | ||
readonly clusterProps?: ecs.ClusterProps; | ||
/** | ||
* The arn of an ECR Repository containing the image to use | ||
* to generate the containers | ||
* | ||
* format: | ||
* arn:aws:ecr:[region]:[account number]:repository/[Repository Name] | ||
*/ | ||
readonly ecrRepositoryArn?: string; | ||
/** | ||
* The version of the image to use from the repository | ||
* | ||
* @default - none | ||
*/ | ||
readonly ecrImageVersion?: string; | ||
/** | ||
* Optional existing image found in some repository | ||
* | ||
* @default - 'latest' | ||
*/ | ||
readonly existingImageObject?: ecs.ContainerImage; | ||
/** | ||
* Optional props to define the container created for the Fargate Service | ||
* | ||
* @default - see fargate-defaults.t | ||
*/ | ||
readonly containerDefinitionProps?: ecs.ContainerDefinitionProps | any; | ||
/** | ||
* Optional props to define the Fargate Task Definition for this construct | ||
* | ||
* @default - see fargate-defaults.ts | ||
*/ | ||
readonly fargateTaskDefinitionProps?: ecs.FargateTaskDefinitionProps | any; | ||
/** | ||
* Optional properties to override default values for the Fargate service. | ||
* Service will set up in the Public or Isolated subnets of the VPC by default, | ||
* override that (e.g. - choose Private subnets) by setting vpcSubnets on this | ||
* object. | ||
*/ | ||
readonly fargateServiceProps?: ecs.FargateServiceProps | any; | ||
/** | ||
* A Fargate Service already instantiated (probably by another Solutions Construct). If | ||
* this is specified, then no props defining a new service can be provided, including: | ||
* existingImageObject, ecrImageVersion, containerDefintionProps, fargateTaskDefinitionProps, | ||
* ecrRepositoryArn, fargateServiceProps, clusterProps, existingClusterInterface | ||
* | ||
* @default - none | ||
*/ | ||
readonly existingFargateServiceObject?: ecs.FargateService; | ||
|
||
readonly existingContainerDefinitionObject?: ecs.ContainerDefinition; | ||
} | ||
|
||
export class AlbToFargate extends Construct { | ||
public readonly loadBalancer: elb.ApplicationLoadBalancer; | ||
public readonly vpc: ec2.IVpc; | ||
public readonly listener: elb.ApplicationListener; | ||
public readonly service: ecs.FargateService; | ||
public readonly container: ecs.ContainerDefinition; | ||
|
||
constructor(scope: Construct, id: string, props: AlbToFargateProps) { | ||
super(scope, id); | ||
defaults.CheckProps(props); | ||
defaults.CheckAlbProps(props); | ||
defaults.CheckFargateProps(props); | ||
|
||
// Obtain VPC for construct (existing or created) | ||
// Determine all the resources to use (existing or launch new) | ||
if (props.existingVpc) { | ||
this.vpc = props.existingVpc; | ||
} else { | ||
this.vpc = defaults.buildVpc(scope, { | ||
defaultVpcProps: props.publicApi | ||
? defaults.DefaultPublicPrivateVpcProps() | ||
: defaults.DefaultIsolatedVpcProps(), | ||
userVpcProps: props.vpcProps, | ||
constructVpcProps: props.publicApi | ||
? undefined | ||
: { enableDnsHostnames: true, enableDnsSupport: true }, | ||
}); | ||
} | ||
|
||
// Set up the ALB | ||
this.loadBalancer = defaults.ObtainAlb( | ||
scope, | ||
`${id}-lb`, | ||
this.vpc, | ||
props.publicApi, | ||
props.existingLoadBalancerObj, | ||
props.loadBalancerProps, | ||
props.logAlbAccessLogs, | ||
props.albLoggingBucketProps | ||
); | ||
|
||
let newListener: boolean; | ||
if (this.loadBalancer.listeners.length === 0) { | ||
newListener = true; | ||
} else { | ||
newListener = false; | ||
} | ||
|
||
// If there's no listener, then we add one here | ||
if (newListener) { | ||
this.listener = defaults.AddListener( | ||
this, | ||
id, | ||
this.loadBalancer, | ||
props.listenerProps | ||
); | ||
} else { | ||
this.listener = GetActiveListener(this.loadBalancer.listeners); | ||
} | ||
|
||
if (props.existingFargateServiceObject) { | ||
this.service = props.existingFargateServiceObject; | ||
// CheckFargateProps confirms that the container is provided | ||
this.container = props.existingContainerDefinitionObject!; | ||
} else { | ||
[this.service, this.container] = defaults.CreateFargateService( | ||
scope, | ||
id, | ||
this.vpc, | ||
props.clusterProps, | ||
props.ecrRepositoryArn, | ||
props.ecrImageVersion, | ||
props.fargateTaskDefinitionProps, | ||
props.containerDefinitionProps, | ||
props.fargateServiceProps | ||
); | ||
} | ||
// Add the Fargate Service to the | ||
// to the ALB Listener we set up earlier | ||
const applicationTargetGroupProps = defaults.consolidateProps( | ||
defaults.DefaultApplicationTargetGroupProps(this.vpc), | ||
props.targetGroupProps | ||
); | ||
|
||
const newTargetGroup = defaults.AddFargateTarget( | ||
scope, | ||
`${id}-target`, | ||
this.listener, | ||
this.service, | ||
props.ruleProps, | ||
applicationTargetGroupProps | ||
); | ||
|
||
// this.listener needs to be set on the construct. | ||
// could be above: else { defaults.GetActiveListener } | ||
// do we then move that funcionality back into the construct (not the function). If so do | ||
// we leave it in AddNewTarget or just do it here and pass the listener? | ||
if (newListener && this.listener) { | ||
const levelOneListener = this.listener.node.defaultChild as CfnListener; | ||
const cfnTargetGroup = newTargetGroup.node.defaultChild as CfnTargetGroup; | ||
levelOneListener.addDependsOn(cfnTargetGroup); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.