Skip to content

Commit 98863f9

Browse files
Yandy Ramirezrix0rrr
authored andcommitted
feat(appmesh): add support for AWS AppMesh (#2299)
This PR allows one to work with AWS AppMesh at higher level L2 construct without needing to dive into the CFN layer for most use case. Fixes #2297.
1 parent 1e81053 commit 98863f9

File tree

22 files changed

+3942
-483
lines changed

22 files changed

+3942
-483
lines changed

packages/@aws-cdk/aws-appmesh/README.md

Lines changed: 261 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,266 @@
1515
---
1616
<!--END STABILITY BANNER-->
1717

18-
This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.
1918

20-
```ts
21-
import appmesh = require('@aws-cdk/aws-appmesh');
19+
AWS App Mesh is a service mesh based on the [Envoy](https://www.envoyproxy.io/) proxy that makes it easy to monitor and control microservices. App Mesh standardizes how your microservices communicate, giving you end-to-end visibility and helping to ensure high-availability for your applications.
20+
21+
App Mesh gives you consistent visibility and network traffic controls for every microservice in an application.
22+
23+
App Mesh supports microservice applications that use service discovery naming for their components. To use App Mesh, you must have an existing application running on AWS Fargate, Amazon ECS, Amazon EKS, Kubernetes on AWS, or Amazon EC2.
24+
25+
For futher information on **AWS AppMesh** visit the [AWS Docs for AppMesh](https://docs.aws.amazon.com/app-mesh/index.html).
26+
27+
## Create the App and Stack
28+
29+
```typescript
30+
const app = new cdk.App();
31+
const stack = new cdk.Stack(app, 'stack');
32+
```
33+
34+
## Creating the Mesh
35+
36+
A service mesh is a logical boundary for network traffic between the services that reside within it.
37+
38+
After you create your service mesh, you can create virtual services, virtual nodes, virtual routers, and routes to distribute traffic between the applications in your mesh.
39+
40+
The following example creates the `AppMesh` service mesh with the default filter of `DROP_ALL`, see [docs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appmesh-mesh-egressfilter.html) here for more info on egress filters.
41+
42+
```typescript
43+
const mesh = new Mesh(stack, 'AppMesh', {
44+
name: 'myAwsmMesh',
45+
});
46+
```
47+
48+
The mesh can also be created with the "ALLOW_ALL" egress filter by overwritting the property.
49+
50+
```typescript
51+
const mesh = new Mesh(stack, 'AppMesh', {
52+
name: 'myAwsmMesh',
53+
meshSpec: {
54+
egressFilter: appmesh.MeshFilterType.Allow_All,
55+
},
56+
});
57+
```
58+
59+
## Adding VirtualRouters
60+
61+
The `Mesh` needs `VirtualRouters` as logical units to route to `VirtualNodes`.
62+
63+
Virtual routers handle traffic for one or more virtual services within your mesh. After you create a virtual router, you can create and associate routes for your virtual router that direct incoming requests to different virtual nodes.
64+
65+
```typescript
66+
const router = mesh.addVirtualRouter('router', {
67+
portMappings: [
68+
{
69+
port: 8081,
70+
protocol: appmesh.Protocol.HTTP,
71+
},
72+
],
73+
});
74+
```
75+
76+
The router can also be created using the constructor and passing in the mesh instead of calling the addVirtualRouter() method for the mesh.
77+
78+
```typescript
79+
const mesh = new Mesh(stack, 'AppMesh', {
80+
name: 'myAwsmMesh',
81+
meshSpec: {
82+
egressFilter: appmesh.MeshFilterType.Allow_All,
83+
},
84+
});
85+
86+
const router = new appmesh.VirtualRouter(stack, 'router', {
87+
mesh, // notice that mesh is a required property when creating a router with a new statement
88+
portMappings: [
89+
{
90+
port: 8081,
91+
protocol: appmesh.Protocol.HTTP,
92+
},
93+
],
94+
});
95+
```
96+
97+
The listener protocol can be either `HTTP` or `TCP`.
98+
99+
The same pattern applies to all constructs within the appmesh library, for any mesh.addXZY method, a new constuctor can also be used. This is particularly useful for cross stack resources are required. Where creating the `mesh` as part of an infrastructure stack and creating the `resources` such as `nodes` is more useful to keep in the application stack.
100+
101+
## Adding VirtualService
102+
103+
A virtual service is an abstraction of a real service that is provided by a virtual node directly or indirectly by means of a virtual router. Dependent services call your virtual service by its virtualServiceName, and those requests are routed to the virtual node or virtual router that is specified as the provider for the virtual service.
104+
105+
We recommend that you use the service discovery name of the real service that you're targeting (such as `my-service.default.svc.cluster.local`).
106+
107+
When creating a virtual service:
108+
109+
- If you want the virtual service to spread traffic across multiple virtual nodes, specify a Virtual router.
110+
- If you want the virtual service to reach a virtual node directly, without a virtual router, specify a Virtual node.
111+
112+
Adding a virtual router as the provider:
113+
114+
```typescript
115+
mesh.addVirtualService('virtual-service', {
116+
virtualRouter: router,
117+
virtualServiceName: 'my-service.default.svc.cluster.local',
118+
});
119+
```
120+
121+
Adding a virtual node as the provider:
122+
123+
```typescript
124+
mesh.addVirtualService('virtual-service', {
125+
virtualNode: node,
126+
virtualServiceName: `my-service.default.svc.cluster.local`,
127+
});
128+
```
129+
130+
**Note** that only one must of `virtualNode` or `virtualRouter` must be chosen.
131+
132+
## Adding a VirtualNode
133+
134+
A `virtual node` acts as a logical pointer to a particular task group, such as an Amazon ECS service or a Kubernetes deployment.
135+
136+
![Virtual node logical diagram](https://docs.aws.amazon.com/app-mesh/latest/userguide/images/virtual_node.png)
137+
138+
When you create a `virtual node`, you must specify the DNS service discovery hostname for your task group. Any inbound traffic that your `virtual node` expects should be specified as a listener. Any outbound traffic that your `virtual node` expects to reach should be specified as a backend.
139+
140+
The response metadata for your new `virtual node` contains the Amazon Resource Name (ARN) that is associated with the `virtual node`. Set this value (either the full ARN or the truncated resource name) as the APPMESH_VIRTUAL_NODE_NAME environment variable for your task group's Envoy proxy container in your task definition or pod spec. For example, the value could be mesh/default/virtualNode/simpleapp. This is then mapped to the node.id and node.cluster Envoy parameters.
141+
142+
> Note
143+
> If you require your Envoy stats or tracing to use a different name, you can override the node.cluster value that is set by APPMESH_VIRTUAL_NODE_NAME with the APPMESH_VIRTUAL_NODE_CLUSTER environment variable.
144+
145+
```typescript
146+
const vpc = new ec2.Vpc(stack, 'vpc');
147+
const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', {
148+
vpc,
149+
name: 'domain.local',
150+
});
151+
152+
const node = mesh.addVirtualNode('virtual-node', {
153+
hostname: 'node-a',
154+
namespace,
155+
listeners: {
156+
portMappings: [
157+
{
158+
port: 8081,
159+
protocol: appmesh.Protocol.HTTP,
160+
},
161+
],
162+
healthChecks: [
163+
{
164+
healthyThreshold: 3,
165+
intervalMillis: 5000, // minimum
166+
path: `/health-check-path`,
167+
port: 8080,
168+
protocol: appmesh.Protocol.HTTP,
169+
timeoutMillis: 2000, // minimum
170+
unhealthyThreshold: 2,
171+
},
172+
],
173+
},
174+
});
175+
```
176+
177+
Create a `VirtualNode` with the the constructor and add tags.
178+
179+
```typescript
180+
const node = new appmesh.VirtualNode(stack, 'node', {
181+
mesh,
182+
hostname: 'node-1',
183+
namespace,
184+
listener: {
185+
portMappings: [
186+
{
187+
port: 8080,
188+
protocol: appmesh.Protocol.HTTP,
189+
},
190+
],
191+
healthChecks: [
192+
{
193+
healthyThreshold: 3,
194+
intervalMillis: 5000, // min
195+
path: '/ping',
196+
port: 8080,
197+
protocol: appmesh.Protocol.HTTP,
198+
timeoutMillis: 2000, // min
199+
unhealthyThreshold: 2,
200+
},
201+
],
202+
},
203+
});
204+
205+
node.node.apply(new cdk.Tag('Environment', 'Dev'));
206+
```
207+
208+
The listeners property can be left blank dded later with the `mesh.addListeners()` method. The `healthcheck` property is optional but if specifying a listener, the `portMappings` must contain at least one property.
209+
210+
## Adding a Route
211+
212+
A `route` is associated with a virtual router, and it's used to match requests for a virtual router and distribute traffic accordingly to its associated virtual nodes.
213+
214+
You can use the prefix parameter in your `route` specification for path-based routing of requests. For example, if your virtual service name is my-service.local and you want the `route` to match requests to my-service.local/metrics, your prefix should be /metrics.
215+
216+
If your `route` matches a request, you can distribute traffic to one or more target virtual nodes with relative weighting.
217+
218+
```typescript
219+
router.addRoute('route', {
220+
routeTargets: [
221+
{
222+
virtualNode,
223+
weight: 1,
224+
},
225+
],
226+
prefix: `/path-to-app`,
227+
isHttpRoute: true,
228+
});
229+
```
230+
231+
Add a single route with multiple targets and split traffic 50/50
232+
233+
```typescript
234+
router.addRoute('route', {
235+
routeTargets: [
236+
{
237+
virtualNode,
238+
weight: 50,
239+
},
240+
{
241+
virtualNode2,
242+
weight: 50,
243+
},
244+
],
245+
prefix: `/path-to-app`,
246+
isHttpRoute: true,
247+
});
22248
```
249+
250+
Multiple routes may also be added at once to different applications or targets.
251+
252+
```typescript
253+
ratingsRouter.addRoutes(
254+
['route1', 'route2'],
255+
[
256+
{
257+
routeTargets: [
258+
{
259+
virtualNode,
260+
weight: 1,
261+
},
262+
],
263+
prefix: `/path-to-app`,
264+
isHttpRoute: true,
265+
},
266+
{
267+
routeTargets: [
268+
{
269+
virtualNode: virtualNode2,
270+
weight: 1,
271+
},
272+
],
273+
prefix: `/path-to-app2`,
274+
isHttpRoute: true,
275+
},
276+
]
277+
);
278+
```
279+
280+
The number of `route ids` and `route targets` must match as each route needs to have a unique name per router.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
// AWS::AppMesh CloudFormation Resources:
22
export * from './appmesh.generated';
3+
export * from './mesh';
4+
export * from './route';
5+
export * from './shared-interfaces';
6+
export * from './virtual-node';
7+
export * from './virtual-router';
8+
export * from './virtual-service';

0 commit comments

Comments
 (0)