in lib/app-mesh-service.ts [37:81]
constructor(scope: cdk.Construct, id: string, props: AppMeshServiceProps) {
super(scope, id);
// Create EnvoyFargateServices.
this.envoyFargateServices = props.routes.map(r => {
return new EnvoyFargateService(this, `EnvoyFargateService_${r.name}`, {
meta: { name: r.name, port: props.port },
environment: props.environment,
});
});
// Create a VirtualRouter.
const virtualRouter = new appmesh.VirtualRouter(this, `VirtualRouter_${props.name}`, {
mesh: props.environment.mesh,
listeners: [appmesh.VirtualRouterListener.http(props.port)],
});
// Create weighted targets.
const weightedTargets = this.envoyFargateServices.map((s, idx) => {
return {
virtualNode: s.virtualNode,
weight: props.routes[idx].weight,
};
});
// Create a VirtualRouter
virtualRouter.addRoute('VirtualRoute', {
routeName: `Route_${props.name}`,
routeSpec: appmesh.RouteSpec.http({ weightedTargets }),
});
// Create a VirtualService.
this.virtualService = new appmesh.VirtualService(this, `VirtualService_${props.name}`, {
virtualServiceName: `${props.name}.${props.environment.hostedZone.zoneName}`.toLowerCase(),
virtualServiceProvider: appmesh.VirtualServiceProvider.virtualRouter(virtualRouter),
});
// Create an A record to the hosted zone to avoid the IP address lookup error.
// https://docs.aws.amazon.com/app-mesh/latest/userguide/troubleshoot-connectivity.html
new route53.ARecord(this, `ARecord_${props.name}`, {
zone: props.environment.hostedZone,
recordName: `${props.name}.${props.environment.hostedZone.zoneName}`.toLowerCase(),
target: route53.RecordTarget.fromIpAddresses('10.10.10.10'),
});
}