in source/patterns/@aws-solutions-constructs/aws-alb-fargate/lib/index.ts [167:246]
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)
this.vpc = defaults.buildVpc(scope, {
existingVpc: props.existingVpc,
defaultVpcProps: props.publicApi ? defaults.DefaultPublicPrivateVpcProps() : defaults.DefaultIsolatedVpcProps(),
userVpcProps: props.vpcProps,
constructVpcProps: { 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
);
const newListener: boolean = this.loadBalancer.listeners.length === 0;
// 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
);
if (newListener && this.listener) {
const levelOneListener = this.listener.node.defaultChild as CfnListener;
const cfnTargetGroup = newTargetGroup.node.defaultChild as CfnTargetGroup;
levelOneListener.addDependsOn(cfnTargetGroup);
}
}