aws-cdk-aws-appmesh 1.204.0


pip install aws-cdk-aws-appmesh

  Latest version

Released: Jun 19, 2023

Project Links

Meta
Author: Amazon Web Services
Requires Python: ~=3.7

Classifiers

Intended Audience
  • Developers

Operating System
  • OS Independent

Programming Language
  • JavaScript
  • Python :: 3 :: Only
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11

Typing
  • Typed

Development Status
  • 7 - Inactive

License
  • OSI Approved

Framework
  • AWS CDK
  • AWS CDK :: 1

AWS App Mesh Construct Library

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


AWS App Mesh is a service mesh based on the Envoy 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.

App Mesh gives you consistent visibility and network traffic controls for every microservice in an application.

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.

For further information on AWS App Mesh, visit the AWS App Mesh Documentation.

Create the App and Stack

app = cdk.App()
stack = cdk.Stack(app, "stack")

Creating the Mesh

A service mesh is a logical boundary for network traffic between the services that reside within it.

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.

The following example creates the AppMesh service mesh with the default egress filter of DROP_ALL. See the AWS CloudFormation EgressFilter resource for more info on egress filters.

mesh = appmesh.Mesh(self, "AppMesh",
    mesh_name="myAwsMesh"
)

The mesh can instead be created with the ALLOW_ALL egress filter by providing the egressFilter property.

mesh = appmesh.Mesh(self, "AppMesh",
    mesh_name="myAwsMesh",
    egress_filter=appmesh.MeshFilterType.ALLOW_ALL
)

Adding VirtualRouters

A mesh uses virtual routers as logical units to route requests to virtual nodes.

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 to your virtual router that direct incoming requests to different virtual nodes.

# mesh: appmesh.Mesh

router = mesh.add_virtual_router("router",
    listeners=[appmesh.VirtualRouterListener.http(8080)]
)

Note that creating the router using the addVirtualRouter() method places it in the same stack as the mesh (which might be different from the current stack). The router can also be created using the VirtualRouter constructor (passing in the mesh) instead of calling the addVirtualRouter() method. This is particularly useful when splitting your resources between many stacks: for example, defining the mesh itself as part of an infrastructure stack, but defining the other resources, such as routers, in the application stack:

# infra_stack: cdk.Stack
# app_stack: cdk.Stack


mesh = appmesh.Mesh(infra_stack, "AppMesh",
    mesh_name="myAwsMesh",
    egress_filter=appmesh.MeshFilterType.ALLOW_ALL
)

# the VirtualRouter will belong to 'appStack',
# even though the Mesh belongs to 'infraStack'
router = appmesh.VirtualRouter(app_stack, "router",
    mesh=mesh,  # notice that mesh is a required property when creating a router with the 'new' statement
    listeners=[appmesh.VirtualRouterListener.http(8081)]
)

The same is true for other add*() methods in the App Mesh construct library.

The VirtualRouterListener class lets you define protocol-specific listeners. The http(), http2(), grpc() and tcp() methods create listeners for the named protocols. They accept a single parameter that defines the port to on which requests will be matched. The port parameter defaults to 8080 if omitted.

Adding a VirtualService

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 specified as the provider for the virtual service.

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).

When creating a virtual service:

  • If you want the virtual service to spread traffic across multiple virtual nodes, specify a virtual router.
  • If you want the virtual service to reach a virtual node directly, without a virtual router, specify a virtual node.

Adding a virtual router as the provider:

# router: appmesh.VirtualRouter


appmesh.VirtualService(self, "virtual-service",
    virtual_service_name="my-service.default.svc.cluster.local",  # optional
    virtual_service_provider=appmesh.VirtualServiceProvider.virtual_router(router)
)

Adding a virtual node as the provider:

# node: appmesh.VirtualNode


appmesh.VirtualService(self, "virtual-service",
    virtual_service_name="my-service.default.svc.cluster.local",  # optional
    virtual_service_provider=appmesh.VirtualServiceProvider.virtual_node(node)
)

Adding a VirtualNode

A virtual node acts as a logical pointer to a particular task group, such as an Amazon ECS service or a Kubernetes deployment.

When you create a virtual node, accept inbound traffic by specifying a listener. Outbound traffic that your virtual node expects to send should be specified as a back end.

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.

Note 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.

# mesh: appmesh.Mesh
vpc = ec2.Vpc(self, "vpc")
namespace = cloudmap.PrivateDnsNamespace(self, "test-namespace",
    vpc=vpc,
    name="domain.local"
)
service = namespace.create_service("Svc")
node = mesh.add_virtual_node("virtual-node",
    service_discovery=appmesh.ServiceDiscovery.cloud_map(service),
    listeners=[appmesh.VirtualNodeListener.http(
        port=8081,
        health_check=appmesh.HealthCheck.http(
            healthy_threshold=3,
            interval=cdk.Duration.seconds(5),  # minimum
            path="/health-check-path",
            timeout=cdk.Duration.seconds(2),  # minimum
            unhealthy_threshold=2
        )
    )],
    access_log=appmesh.AccessLog.from_file_path("/dev/stdout")
)

Create a VirtualNode with the constructor and add tags.

# mesh: appmesh.Mesh
# service: cloudmap.Service


node = appmesh.VirtualNode(self, "node",
    mesh=mesh,
    service_discovery=appmesh.ServiceDiscovery.cloud_map(service),
    listeners=[appmesh.VirtualNodeListener.http(
        port=8080,
        health_check=appmesh.HealthCheck.http(
            healthy_threshold=3,
            interval=cdk.Duration.seconds(5),
            path="/ping",
            timeout=cdk.Duration.seconds(2),
            unhealthy_threshold=2
        ),
        timeout=appmesh.HttpTimeout(
            idle=cdk.Duration.seconds(5)
        )
    )],
    backend_defaults=appmesh.BackendDefaults(
        tls_client_policy=appmesh.TlsClientPolicy(
            validation=appmesh.TlsValidation(
                trust=appmesh.TlsValidationTrust.file("/keys/local_cert_chain.pem")
            )
        )
    ),
    access_log=appmesh.AccessLog.from_file_path("/dev/stdout")
)

cdk.Tags.of(node).add("Environment", "Dev")

Create a VirtualNode with the constructor and add backend virtual service.

# mesh: appmesh.Mesh
# router: appmesh.VirtualRouter
# service: cloudmap.Service


node = appmesh.VirtualNode(self, "node",
    mesh=mesh,
    service_discovery=appmesh.ServiceDiscovery.cloud_map(service),
    listeners=[appmesh.VirtualNodeListener.http(
        port=8080,
        health_check=appmesh.HealthCheck.http(
            healthy_threshold=3,
            interval=cdk.Duration.seconds(5),
            path="/ping",
            timeout=cdk.Duration.seconds(2),
            unhealthy_threshold=2
        ),
        timeout=appmesh.HttpTimeout(
            idle=cdk.Duration.seconds(5)
        )
    )],
    access_log=appmesh.AccessLog.from_file_path("/dev/stdout")
)

virtual_service = appmesh.VirtualService(self, "service-1",
    virtual_service_provider=appmesh.VirtualServiceProvider.virtual_router(router),
    virtual_service_name="service1.domain.local"
)

node.add_backend(appmesh.Backend.virtual_service(virtual_service))

The listeners property can be left blank and added later with the node.addListener() method. The serviceDiscovery property must be specified when specifying a listener.

The backends property can be added with node.addBackend(). In the example, we define a virtual service and add it to the virtual node to allow egress traffic to other nodes.

The backendDefaults property is added to the node while creating the virtual node. These are the virtual node's default settings for all backends.

The VirtualNode.addBackend() method is especially useful if you want to create a circular traffic flow by having a Virtual Service as a backend whose provider is that same Virtual Node:

# mesh: appmesh.Mesh


node = appmesh.VirtualNode(self, "node",
    mesh=mesh,
    service_discovery=appmesh.ServiceDiscovery.dns("node")
)

virtual_service = appmesh.VirtualService(self, "service-1",
    virtual_service_provider=appmesh.VirtualServiceProvider.virtual_node(node),
    virtual_service_name="service1.domain.local"
)

node.add_backend(appmesh.Backend.virtual_service(virtual_service))

Adding TLS to a listener

The tls property specifies TLS configuration when creating a listener for a virtual node or a virtual gateway. Provide the TLS certificate to the proxy in one of the following ways:

  • A certificate from AWS Certificate Manager (ACM).
  • A customer-provided certificate (specify a certificateChain path file and a privateKey file path).
  • A certificate provided by a Secrets Discovery Service (SDS) endpoint over local Unix Domain Socket (specify its secretName).
# A Virtual Node with listener TLS from an ACM provided certificate
# cert: certificatemanager.Certificate
# mesh: appmesh.Mesh


node = appmesh.VirtualNode(self, "node",
    mesh=mesh,
    service_discovery=appmesh.ServiceDiscovery.dns("node"),
    listeners=[appmesh.VirtualNodeListener.grpc(
        port=80,
        tls=appmesh.ListenerTlsOptions(
            mode=appmesh.TlsMode.STRICT,
            certificate=appmesh.TlsCertificate.acm(cert)
        )
    )]
)

# A Virtual Gateway with listener TLS from a customer provided file certificate
gateway = appmesh.VirtualGateway(self, "gateway",
    mesh=mesh,
    listeners=[appmesh.VirtualGatewayListener.grpc(
        port=8080,
        tls=appmesh.ListenerTlsOptions(
            mode=appmesh.TlsMode.STRICT,
            certificate=appmesh.TlsCertificate.file("path/to/certChain", "path/to/privateKey")
        )
    )],
    virtual_gateway_name="gateway"
)

# A Virtual Gateway with listener TLS from a SDS provided certificate
gateway2 = appmesh.VirtualGateway(self, "gateway2",
    mesh=mesh,
    listeners=[appmesh.VirtualGatewayListener.http2(
        port=8080,
        tls=appmesh.ListenerTlsOptions(
            mode=appmesh.TlsMode.STRICT,
            certificate=appmesh.TlsCertificate.sds("secrete_certificate")
        )
    )],
    virtual_gateway_name="gateway2"
)

Adding mutual TLS authentication

Mutual TLS authentication is an optional component of TLS that offers two-way peer authentication. To enable mutual TLS authentication, add the mutualTlsCertificate property to TLS client policy and/or the mutualTlsValidation property to your TLS listener.

tls.mutualTlsValidation and tlsClientPolicy.mutualTlsCertificate can be sourced from either:

  • A customer-provided certificate (specify a certificateChain path file and a privateKey file path).
  • A certificate provided by a Secrets Discovery Service (SDS) endpoint over local Unix Domain Socket (specify its secretName).

Note Currently, a certificate from AWS Certificate Manager (ACM) cannot be used for mutual TLS authentication.

# mesh: appmesh.Mesh


node1 = appmesh.VirtualNode(self, "node1",
    mesh=mesh,
    service_discovery=appmesh.ServiceDiscovery.dns("node"),
    listeners=[appmesh.VirtualNodeListener.grpc(
        port=80,
        tls=appmesh.ListenerTlsOptions(
            mode=appmesh.TlsMode.STRICT,
            certificate=appmesh.TlsCertificate.file("path/to/certChain", "path/to/privateKey"),
            # Validate a file client certificates to enable mutual TLS authentication when a client provides a certificate.
            mutual_tls_validation=appmesh.MutualTlsValidation(
                trust=appmesh.TlsValidationTrust.file("path-to-certificate")
            )
        )
    )]
)

certificate_authority_arn = "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012"
node2 = appmesh.VirtualNode(self, "node2",
    mesh=mesh,
    service_discovery=appmesh.ServiceDiscovery.dns("node2"),
    backend_defaults=appmesh.BackendDefaults(
        tls_client_policy=appmesh.TlsClientPolicy(
            ports=[8080, 8081],
            validation=appmesh.TlsValidation(
                subject_alternative_names=appmesh.SubjectAlternativeNames.matching_exactly("mesh-endpoint.apps.local"),
                trust=appmesh.TlsValidationTrust.acm([
                    acmpca.CertificateAuthority.from_certificate_authority_arn(self, "certificate", certificate_authority_arn)
                ])
            ),
            # Provide a SDS client certificate when a server requests it and enable mutual TLS authentication.
            mutual_tls_certificate=appmesh.TlsCertificate.sds("secret_certificate")
        )
    )
)

Adding outlier detection to a Virtual Node listener

The outlierDetection property adds outlier detection to a Virtual Node listener. The properties baseEjectionDuration, interval, maxEjectionPercent, and maxServerErrors are required.

# mesh: appmesh.Mesh
# Cloud Map service discovery is currently required for host ejection by outlier detection
vpc = ec2.Vpc(self, "vpc")
namespace = cloudmap.PrivateDnsNamespace(self, "test-namespace",
    vpc=vpc,
    name="domain.local"
)
service = namespace.create_service("Svc")
node = mesh.add_virtual_node("virtual-node",
    service_discovery=appmesh.ServiceDiscovery.cloud_map(service),
    listeners=[appmesh.VirtualNodeListener.http(
        outlier_detection=appmesh.OutlierDetection(
            base_ejection_duration=cdk.Duration.seconds(10),
            interval=cdk.Duration.seconds(30),
            max_ejection_percent=50,
            max_server_errors=5
        )
    )]
)

Adding a connection pool to a listener

The connectionPool property can be added to a Virtual Node listener or Virtual Gateway listener to add a request connection pool. Each listener protocol type has its own connection pool properties.

# A Virtual Node with a gRPC listener with a connection pool set
# mesh: appmesh.Mesh

node = appmesh.VirtualNode(self, "node",
    mesh=mesh,
    # DNS service discovery can optionally specify the DNS response type as either LOAD_BALANCER or ENDPOINTS.
    # LOAD_BALANCER means that the DNS resolver returns a loadbalanced set of endpoints,
    # whereas ENDPOINTS means that the DNS resolver is returning all the endpoints.
    # By default, the response type is assumed to be LOAD_BALANCER
    service_discovery=appmesh.ServiceDiscovery.dns("node", appmesh.DnsResponseType.ENDPOINTS),
    listeners=[appmesh.VirtualNodeListener.http(
        port=80,
        connection_pool=appmesh.HttpConnectionPool(
            max_connections=100,
            max_pending_requests=10
        )
    )]
)

# A Virtual Gateway with a gRPC listener with a connection pool set
gateway = appmesh.VirtualGateway(self, "gateway",
    mesh=mesh,
    listeners=[appmesh.VirtualGatewayListener.grpc(
        port=8080,
        connection_pool=appmesh.GrpcConnectionPool(
            max_requests=10
        )
    )],
    virtual_gateway_name="gateway"
)

Adding a Route

A route matches requests with an associated virtual router and distributes traffic to its associated virtual nodes. The route distributes matching requests to one or more target virtual nodes with relative weighting.

The RouteSpec class lets you define protocol-specific route specifications. The tcp(), http(), http2(), and grpc() methods create a specification for the named protocols.

For HTTP-based routes, the match field can match on path (prefix, exact, or regex), HTTP method, scheme, HTTP headers, and query parameters. By default, HTTP-based routes match all requests.

For gRPC-based routes, the match field can match on service name, method name, and metadata. When specifying the method name, the service name must also be specified.

For example, here's how to add an HTTP route that matches based on a prefix of the URL path:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-http",
    route_spec=appmesh.RouteSpec.http(
        weighted_targets=[appmesh.WeightedTarget(
            virtual_node=node
        )
        ],
        match=appmesh.HttpRouteMatch(
            # Path that is passed to this method must start with '/'.
            path=appmesh.HttpRoutePathMatch.starts_with("/path-to-app")
        )
    )
)

Add an HTTP2 route that matches based on exact path, method, scheme, headers, and query parameters:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-http2",
    route_spec=appmesh.RouteSpec.http2(
        weighted_targets=[appmesh.WeightedTarget(
            virtual_node=node
        )
        ],
        match=appmesh.HttpRouteMatch(
            path=appmesh.HttpRoutePathMatch.exactly("/exact"),
            method=appmesh.HttpRouteMethod.POST,
            protocol=appmesh.HttpRouteProtocol.HTTPS,
            headers=[
                # All specified headers must match for the route to match.
                appmesh.HeaderMatch.value_is("Content-Type", "application/json"),
                appmesh.HeaderMatch.value_is_not("Content-Type", "application/json")
            ],
            query_parameters=[
                # All specified query parameters must match for the route to match.
                appmesh.QueryParameterMatch.value_is("query-field", "value")
            ]
        )
    )
)

Add a single route with two targets and split traffic 50/50:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-http",
    route_spec=appmesh.RouteSpec.http(
        weighted_targets=[appmesh.WeightedTarget(
            virtual_node=node,
            weight=50
        ), appmesh.WeightedTarget(
            virtual_node=node,
            weight=50
        )
        ],
        match=appmesh.HttpRouteMatch(
            path=appmesh.HttpRoutePathMatch.starts_with("/path-to-app")
        )
    )
)

Add an http2 route with retries:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-http2-retry",
    route_spec=appmesh.RouteSpec.http2(
        weighted_targets=[appmesh.WeightedTarget(virtual_node=node)],
        retry_policy=appmesh.HttpRetryPolicy(
            # Retry if the connection failed
            tcp_retry_events=[appmesh.TcpRetryEvent.CONNECTION_ERROR],
            # Retry if HTTP responds with a gateway error (502, 503, 504)
            http_retry_events=[appmesh.HttpRetryEvent.GATEWAY_ERROR],
            # Retry five times
            retry_attempts=5,
            # Use a 1 second timeout per retry
            retry_timeout=cdk.Duration.seconds(1)
        )
    )
)

Add a gRPC route with retries:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-grpc-retry",
    route_spec=appmesh.RouteSpec.grpc(
        weighted_targets=[appmesh.WeightedTarget(virtual_node=node)],
        match=appmesh.GrpcRouteMatch(service_name="servicename"),
        retry_policy=appmesh.GrpcRetryPolicy(
            tcp_retry_events=[appmesh.TcpRetryEvent.CONNECTION_ERROR],
            http_retry_events=[appmesh.HttpRetryEvent.GATEWAY_ERROR],
            # Retry if gRPC responds that the request was cancelled, a resource
            # was exhausted, or if the service is unavailable
            grpc_retry_events=[appmesh.GrpcRetryEvent.CANCELLED, appmesh.GrpcRetryEvent.RESOURCE_EXHAUSTED, appmesh.GrpcRetryEvent.UNAVAILABLE
            ],
            retry_attempts=5,
            retry_timeout=cdk.Duration.seconds(1)
        )
    )
)

Add an gRPC route that matches based on method name and metadata:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-grpc-retry",
    route_spec=appmesh.RouteSpec.grpc(
        weighted_targets=[appmesh.WeightedTarget(virtual_node=node)],
        match=appmesh.GrpcRouteMatch(
            # When method name is specified, service name must be also specified.
            method_name="methodname",
            service_name="servicename",
            metadata=[
                # All specified metadata must match for the route to match.
                appmesh.HeaderMatch.value_starts_with("Content-Type", "application/"),
                appmesh.HeaderMatch.value_does_not_start_with("Content-Type", "text/")
            ]
        )
    )
)

Add a gRPC route with timeout:

# router: appmesh.VirtualRouter
# node: appmesh.VirtualNode


router.add_route("route-http",
    route_spec=appmesh.RouteSpec.grpc(
        weighted_targets=[appmesh.WeightedTarget(
            virtual_node=node
        )
        ],
        match=appmesh.GrpcRouteMatch(
            service_name="my-service.default.svc.cluster.local"
        ),
        timeout=appmesh.GrpcTimeout(
            idle=cdk.Duration.seconds(2),
            per_request=cdk.Duration.seconds(1)
        )
    )
)

Adding a Virtual Gateway

A virtual gateway allows resources outside your mesh to communicate with resources inside your mesh. The virtual gateway represents an Envoy proxy running in an Amazon ECS task, in a Kubernetes service, or on an Amazon EC2 instance. Unlike a virtual node, which represents Envoy running with an application, a virtual gateway represents Envoy deployed by itself.

A virtual gateway is similar to a virtual node in that it has a listener that accepts traffic for a particular port and protocol (HTTP, HTTP2, gRPC). Traffic received by the virtual gateway is directed to other services in your mesh using rules defined in gateway routes which can be added to your virtual gateway.

Create a virtual gateway with the constructor:

# mesh: appmesh.Mesh

certificate_authority_arn = "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012"

gateway = appmesh.VirtualGateway(self, "gateway",
    mesh=mesh,
    listeners=[appmesh.VirtualGatewayListener.http(
        port=443,
        health_check=appmesh.HealthCheck.http(
            interval=cdk.Duration.seconds(10)
        )
    )],
    backend_defaults=appmesh.BackendDefaults(
        tls_client_policy=appmesh.TlsClientPolicy(
            ports=[8080, 8081],
            validation=appmesh.TlsValidation(
                trust=appmesh.TlsValidationTrust.acm([
                    acmpca.CertificateAuthority.from_certificate_authority_arn(self, "certificate", certificate_authority_arn)
                ])
            )
        )
    ),
    access_log=appmesh.AccessLog.from_file_path("/dev/stdout"),
    virtual_gateway_name="virtualGateway"
)

Add a virtual gateway directly to the mesh:

# mesh: appmesh.Mesh


gateway = mesh.add_virtual_gateway("gateway",
    access_log=appmesh.AccessLog.from_file_path("/dev/stdout"),
    virtual_gateway_name="virtualGateway",
    listeners=[appmesh.VirtualGatewayListener.http(
        port=443,
        health_check=appmesh.HealthCheck.http(
            interval=cdk.Duration.seconds(10)
        )
    )]
)

The listeners field defaults to an HTTP Listener on port 8080 if omitted. A gateway route can be added using the gateway.addGatewayRoute() method.

The backendDefaults property, provided when creating the virtual gateway, specifies the virtual gateway's default settings for all backends.

Adding a Gateway Route

A gateway route is attached to a virtual gateway and routes matching traffic to an existing virtual service.

For HTTP-based gateway routes, the match field can be used to match on path (prefix, exact, or regex), HTTP method, host name, HTTP headers, and query parameters. By default, HTTP-based gateway routes match all requests.

# gateway: appmesh.VirtualGateway
# virtual_service: appmesh.VirtualService


gateway.add_gateway_route("gateway-route-http",
    route_spec=appmesh.GatewayRouteSpec.http(
        route_target=virtual_service,
        match=appmesh.HttpGatewayRouteMatch(
            path=appmesh.HttpGatewayRoutePathMatch.regex("regex")
        )
    )
)

For gRPC-based gateway routes, the match field can be used to match on service name, host name, and metadata.

# gateway: appmesh.VirtualGateway
# virtual_service: appmesh.VirtualService


gateway.add_gateway_route("gateway-route-grpc",
    route_spec=appmesh.GatewayRouteSpec.grpc(
        route_target=virtual_service,
        match=appmesh.GrpcGatewayRouteMatch(
            hostname=appmesh.GatewayRouteHostnameMatch.ends_with(".example.com")
        )
    )
)

For HTTP based gateway routes, App Mesh automatically rewrites the matched prefix path in Gateway Route to โ€œ/โ€. This automatic rewrite configuration can be overwritten in following ways:

# gateway: appmesh.VirtualGateway
# virtual_service: appmesh.VirtualService


gateway.add_gateway_route("gateway-route-http",
    route_spec=appmesh.GatewayRouteSpec.http(
        route_target=virtual_service,
        match=appmesh.HttpGatewayRouteMatch(
            # This disables the default rewrite to '/', and retains original path.
            path=appmesh.HttpGatewayRoutePathMatch.starts_with("/path-to-app/", "")
        )
    )
)

gateway.add_gateway_route("gateway-route-http-1",
    route_spec=appmesh.GatewayRouteSpec.http(
        route_target=virtual_service,
        match=appmesh.HttpGatewayRouteMatch(
            # If the request full path is '/path-to-app/xxxxx', this rewrites the path to '/rewrittenUri/xxxxx'.
            # Please note both `prefixPathMatch` and `rewriteTo` must start and end with the `/` character.
            path=appmesh.HttpGatewayRoutePathMatch.starts_with("/path-to-app/", "/rewrittenUri/")
        )
    )
)

If matching other path (exact or regex), only specific rewrite path can be specified. Unlike startsWith() method above, no default rewrite is performed.

# gateway: appmesh.VirtualGateway
# virtual_service: appmesh.VirtualService


gateway.add_gateway_route("gateway-route-http-2",
    route_spec=appmesh.GatewayRouteSpec.http(
        route_target=virtual_service,
        match=appmesh.HttpGatewayRouteMatch(
            # This rewrites the path from '/test' to '/rewrittenPath'.
            path=appmesh.HttpGatewayRoutePathMatch.exactly("/test", "/rewrittenPath")
        )
    )
)

For HTTP/gRPC based routes, App Mesh automatically rewrites the original request received at the Virtual Gateway to the destination Virtual Service name. This default host name rewrite can be configured by specifying the rewrite rule as one of the match property:

# gateway: appmesh.VirtualGateway
# virtual_service: appmesh.VirtualService


gateway.add_gateway_route("gateway-route-grpc",
    route_spec=appmesh.GatewayRouteSpec.grpc(
        route_target=virtual_service,
        match=appmesh.GrpcGatewayRouteMatch(
            hostname=appmesh.GatewayRouteHostnameMatch.exactly("example.com"),
            # This disables the default rewrite to virtual service name and retain original request.
            rewrite_request_hostname=False
        )
    )
)

Importing Resources

Each App Mesh resource class comes with two static methods, from<Resource>Arn and from<Resource>Attributes (where <Resource> is replaced with the resource name, such as VirtualNode) for importing a reference to an existing App Mesh resource. These imported resources can be used with other resources in your mesh as if they were defined directly in your CDK application.

arn = "arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh/virtualNode/testNode"
appmesh.VirtualNode.from_virtual_node_arn(self, "importedVirtualNode", arn)
virtual_node_name = "my-virtual-node"
appmesh.VirtualNode.from_virtual_node_attributes(self, "imported-virtual-node",
    mesh=appmesh.Mesh.from_mesh_name(self, "Mesh", "testMesh"),
    virtual_node_name=virtual_node_name
)

To import a mesh, again there are two static methods, fromMeshArn and fromMeshName.

arn = "arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh"
appmesh.Mesh.from_mesh_arn(self, "imported-mesh", arn)
appmesh.Mesh.from_mesh_name(self, "imported-mesh", "abc")

IAM Grants

VirtualNode and VirtualGateway provide grantStreamAggregatedResources methods that grant identities that are running Envoy access to stream generated config from App Mesh.

# mesh: appmesh.Mesh

gateway = appmesh.VirtualGateway(self, "testGateway", mesh=mesh)
envoy_user = iam.User(self, "envoyUser")

#
# This will grant `grantStreamAggregatedResources` ONLY for this gateway.
#
gateway.grant_stream_aggregated_resources(envoy_user)

Adding Resources to shared meshes

A shared mesh allows resources created by different accounts to communicate with each other in the same mesh:

# This is the ARN for the mesh from different AWS IAM account ID.
# Ensure mesh is properly shared with your account. For more details, see: https://github.com/aws/aws-cdk/issues/15404
arn = "arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh"
shared_mesh = appmesh.Mesh.from_mesh_arn(self, "imported-mesh", arn)

# This VirtualNode resource can communicate with the resources in the mesh from different AWS IAM account ID.
appmesh.VirtualNode(self, "test-node",
    mesh=shared_mesh
)
1.204.0 Jun 19, 2023
1.203.0 May 31, 2023
1.202.0 May 22, 2023
1.201.0 May 10, 2023
1.200.0 Apr 26, 2023
1.199.0 Apr 20, 2023
1.198.1 Mar 31, 2023
1.198.0 Mar 22, 2023
1.197.0 Mar 14, 2023
1.196.0 Mar 08, 2023
1.195.0 Mar 02, 2023
1.194.0 Feb 21, 2023
1.193.0 Feb 15, 2023
1.192.0 Feb 09, 2023
1.191.0 Jan 31, 2023
1.190.0 Jan 25, 2023
1.189.0 Jan 19, 2023
1.188.0 Jan 11, 2023
1.187.0 Jan 03, 2023
1.186.1 Dec 30, 2022
1.186.0 Dec 29, 2022
1.185.0 Dec 28, 2022
1.184.1 Dec 23, 2022
1.184.0 Dec 22, 2022
1.183.0 Dec 14, 2022
1.182.0 Dec 07, 2022
1.181.1 Nov 29, 2022
1.181.0 Nov 18, 2022
1.180.0 Nov 01, 2022
1.179.0 Oct 27, 2022
1.178.0 Oct 20, 2022
1.177.0 Oct 13, 2022
1.176.0 Oct 06, 2022
1.175.0 Sep 29, 2022
1.174.0 Sep 22, 2022
1.173.0 Sep 16, 2022
1.172.0 Sep 08, 2022
1.171.0 Aug 31, 2022
1.170.1 Aug 31, 2022
1.170.0 Aug 25, 2022
1.169.0 Aug 18, 2022
1.168.0 Aug 09, 2022
1.167.0 Aug 02, 2022
1.166.1 Jul 29, 2022
1.165.0 Jul 19, 2022
1.164.0 Jul 16, 2022
1.163.2 Jul 14, 2022
1.163.1 Jul 09, 2022
1.163.0 Jul 06, 2022
1.162.0 Jul 01, 2022
1.161.0 Jun 23, 2022
1.160.0 Jun 14, 2022
1.159.0 Jun 03, 2022
1.158.0 May 27, 2022
1.157.0 May 21, 2022
1.156.1 May 13, 2022
1.156.0 May 12, 2022
1.155.0 May 04, 2022
1.154.0 Apr 28, 2022
1.153.1 Apr 23, 2022
1.153.0 Apr 22, 2022
1.152.0 Apr 07, 2022
1.151.0 Apr 01, 2022
1.150.0 Mar 26, 2022
1.149.0 Mar 17, 2022
1.148.0 Mar 10, 2022
1.147.0 Mar 01, 2022
1.146.0 Feb 25, 2022
1.145.0 Feb 19, 2022
1.144.0 Feb 08, 2022
1.143.0 Feb 02, 2022
1.142.0 Jan 29, 2022
1.141.0 Jan 27, 2022
1.140.0 Jan 20, 2022
1.139.0 Jan 11, 2022
1.138.2 Jan 10, 2022
1.138.1 Jan 07, 2022
1.138.0 Jan 04, 2022
1.137.0 Dec 21, 2021
1.136.0 Dec 15, 2021
1.135.0 Dec 10, 2021
1.134.0 Nov 23, 2021
1.133.0 Nov 19, 2021
1.132.0 Nov 09, 2021
1.131.0 Nov 07, 2021
1.130.0 Oct 29, 2021
1.129.0 Oct 21, 2021
1.128.0 Oct 14, 2021
1.127.0 Oct 08, 2021
1.126.0 Oct 05, 2021
1.125.0 Sep 29, 2021
1.124.0 Sep 21, 2021
1.123.0 Sep 17, 2021
1.122.0 Sep 08, 2021
1.121.0 Sep 01, 2021
1.120.0 Aug 26, 2021
1.119.0 Aug 17, 2021
1.118.0 Aug 11, 2021
1.117.0 Aug 05, 2021
1.116.0 Jul 28, 2021
1.115.0 Jul 21, 2021
1.114.0 Jul 15, 2021
1.113.0 Jul 12, 2021
1.112.0 Jul 09, 2021
1.111.0 Jul 02, 2021
1.110.1 Jun 28, 2021
1.110.0 Jun 24, 2021
1.109.0 Jun 17, 2021
1.108.1 Jun 11, 2021
1.108.0 Jun 09, 2021
1.107.0 Jun 02, 2021
1.106.1 May 26, 2021
1.106.0 May 25, 2021
1.105.0 May 19, 2021
1.104.0 May 15, 2021
1.103.0 May 10, 2021
1.102.0 May 04, 2021
1.101.0 Apr 28, 2021
1.100.0 Apr 20, 2021
1.99.0 Apr 19, 2021
1.98.0 Apr 12, 2021
1.97.0 Apr 06, 2021
1.96.0 Apr 01, 2021
1.95.2 Apr 01, 2021
1.95.1 Mar 26, 2021
1.95.0 Mar 25, 2021
1.94.1 Mar 17, 2021
1.94.0 Mar 16, 2021
1.93.0 Mar 11, 2021
1.92.0 Mar 06, 2021
1.91.0 Feb 23, 2021
1.90.1 Feb 19, 2021
1.90.0 Feb 17, 2021
1.89.0 Feb 09, 2021
1.88.0 Feb 04, 2021
1.87.1 Jan 28, 2021
1.87.0 Jan 27, 2021
1.86.0 Jan 21, 2021
1.85.0 Jan 14, 2021
1.84.0 Jan 12, 2021
1.83.0 Jan 06, 2021
1.82.0 Jan 03, 2021
1.81.0 Dec 31, 2020
1.80.0 Dec 22, 2020
1.79.0 Dec 17, 2020
1.78.0 Dec 12, 2020
1.77.0 Dec 07, 2020
1.76.0 Dec 01, 2020
1.75.0 Nov 24, 2020
1.74.0 Nov 17, 2020
1.73.0 Nov 11, 2020
1.72.0 Nov 06, 2020
1.71.0 Oct 29, 2020
1.70.0 Oct 24, 2020
1.69.0 Oct 19, 2020
1.68.0 Oct 15, 2020
1.67.0 Oct 07, 2020
1.66.0 Oct 02, 2020
1.65.0 Oct 01, 2020
1.64.1 Sep 25, 2020
1.64.0 Sep 24, 2020
1.63.0 Sep 14, 2020
1.62.0 Sep 04, 2020
1.61.1 Aug 28, 2020
1.61.0 Aug 27, 2020
1.60.0 Aug 20, 2020
1.59.0 Aug 15, 2020
1.58.0 Aug 12, 2020
1.57.0 Aug 07, 2020
1.56.0 Aug 01, 2020
1.55.0 Jul 28, 2020
1.54.0 Jul 22, 2020
1.53.0 Jul 20, 2020
1.52.0 Jul 18, 2020
1.51.0 Jul 09, 2020
1.50.0 Jul 07, 2020
1.49.1 Jul 02, 2020
1.49.0 Jul 02, 2020
1.48.0 Jul 01, 2020
1.47.1 Jun 30, 2020
1.47.0 Jun 24, 2020
1.46.0 Jun 20, 2020
1.45.0 Jun 09, 2020
1.44.0 Jun 04, 2020
1.43.0 Jun 04, 2020
1.42.1 Jun 01, 2020
1.42.0 May 27, 2020
1.41.0 May 21, 2020
1.40.0 May 20, 2020
1.39.0 May 16, 2020
1.38.0 May 08, 2020
1.37.0 May 05, 2020
1.36.1 Apr 29, 2020
1.36.0 Apr 28, 2020
1.35.0 Apr 24, 2020
1.34.1 Apr 22, 2020
1.34.0 Apr 21, 2020
1.33.1 Apr 19, 2020
1.33.0 Apr 17, 2020
1.32.2 Apr 10, 2020
1.32.1 Apr 09, 2020
1.32.0 Apr 07, 2020
1.31.0 Mar 24, 2020
1.30.0 Mar 18, 2020
1.29.0 Mar 18, 2020
1.28.0 Mar 16, 2020
1.27.0 Mar 03, 2020
1.26.0 Feb 26, 2020
1.25.0 Feb 19, 2020
1.24.0 Feb 14, 2020
1.23.0 Feb 07, 2020
1.22.0 Jan 23, 2020
1.21.1 Jan 16, 2020
1.21.0 Jan 16, 2020
1.20.0 Jan 07, 2020
1.19.0 Dec 17, 2019
1.18.0 Nov 25, 2019
1.17.1 Nov 19, 2019
1.17.0 Nov 19, 2019
1.16.3 Nov 13, 2019
1.16.2 Nov 12, 2019
1.16.1 Nov 12, 2019
1.16.0 Nov 11, 2019
1.15.0 Oct 28, 2019
1.14.0 Oct 22, 2019
1.13.1 Oct 15, 2019
1.13.0 Oct 15, 2019
1.12.0 Oct 07, 2019
1.11.0 Oct 02, 2019
1.10.1 Oct 01, 2019
1.10.0 Sep 30, 2019
1.9.0 Sep 20, 2019
1.8.0 Sep 10, 2019
1.7.0 Sep 06, 2019
1.6.1 Aug 29, 2019
1.6.0 Aug 27, 2019
1.5.0 Aug 21, 2019
1.4.0 Aug 14, 2019
1.3.0 Aug 02, 2019
1.2.0 Jul 25, 2019
1.1.0 Jul 19, 2019
1.0.0 Jul 11, 2019
0.39.0 Jul 09, 2019
0.38.0 Jul 08, 2019
0.37.0 Jul 04, 2019
0.36.2 Jul 03, 2019
0.36.1 Jul 01, 2019
0.36.0 Jun 25, 2019
0.35.0 Jun 19, 2019
0.34.0 Jun 10, 2019
0.33.0 May 30, 2019
0.32.0 May 24, 2019
0.31.0 May 07, 2019
0.30.0 May 02, 2019
0.29.0 Apr 24, 2019
0.28.0 Apr 04, 2019
0.27.0 Apr 04, 2019
Extras: None
Dependencies:
aws-cdk.aws-acmpca (==1.204.0)
aws-cdk.aws-certificatemanager (==1.204.0)
aws-cdk.aws-ec2 (==1.204.0)
aws-cdk.aws-iam (==1.204.0)
aws-cdk.aws-servicediscovery (==1.204.0)
aws-cdk.core (==1.204.0)
constructs (<4.0.0,>=3.3.69)
jsii (<2.0.0,>=1.84.0)
publication (>=0.0.3)
typeguard (~=2.13.3)