How do I create an AWS App Mesh using AWS CDK

599 Views Asked by At

I am attempting to create a stack for (currently) 9 .NET Core microservices to run in ECS Fargate and communicate with each other via App Mesh. I plan on creating an Infrastructure stack which creates the App Mesh resource and the ECS Cluster and a Microservice stack that creates the resources for each service and adds them to the App Mesh and ECS cluster.

I currently have this code:

Vpc = Amazon.CDK.AWS.EC2.Vpc.FromLookup(this, "vpc", new VpcLookupOptions
{
    VpcId = "xxxxxxxxxxxx"
});

DefaultCloudMapNamespace = new CloudMapNamespaceOptions
{
    Vpc = Vpc,
    Name = dnsNamespace,
    Type = NamespaceType.DNS_PRIVATE,
};

EcsCluster = new Cluster(this, $"{Env}-linux-cluster", new ClusterProps
{
    Vpc = Vpc,
    ClusterName = $"{Env}-linux-cluster",
    DefaultCloudMapNamespace = DefaultCloudMapNamespace
});

This seems to be okay - it creates a hosted zone in Route53.

When I am creating the Service for Cloud Map, I'm using this code:

var cloudMapService = new Service(this, serviceName, new ServiceProps
{
    Namespace = new PrivateDnsNamespace(this, $"{serviceNameHyphen}-cm-namespace", new PrivateDnsNamespaceProps
    {
        Vpc = infrastructureStack.Vpc,
        Name = $"{serviceName}.dev",
    }),
    DnsRecordType = DnsRecordType.SRV,
    DnsTtl = Duration.Seconds(60),
    RoutingPolicy = RoutingPolicy.MULTIVALUE,
    Name = serviceName
});

This is the first time I'm working with App Mesh & Cloud Map, but I would expect to use the same private hosted zone for both the Cloud Map namespace and the Cloud Map Service namespace.

Is this the correct approach?

1

There are 1 best solutions below

0
On

My approach:

I create Namespace first

    cloud_map = sds.PrivateDnsNamespace(
        self,
        "PrivateNameSpace",
        vpc=vpcObject,
        description=' '.join(["Private DNS for", self.node.try_get_context('EnvironmentName')]),
        name=service_domain
    )

Then when create Virtual Service I use same domain for it

        vservice = mesh.VirtualService(
            self,
            "VirtualService",
            virtual_service_name='.'.join([node_name, service_domain]),
            virtual_service_provider=mesh.VirtualServiceProvider.virtual_node(vnode)
        )

Then call it when create ECS service

    ecs_service = ecs.Ec2Service(
        self,
        "ECSService",
        task_definition=ecs_task,
        placement_strategies=[
            ecs.PlacementStrategy.spread_across_instances()
        ],
        desired_count=desiredCount,
        cluster=clusterObject,
        security_groups=[sgObject],
        vpc_subnets=ec2.SubnetSelection(
            subnet_type=ec2.SubnetType.PRIVATE
        ),
        enable_ecs_managed_tags=True,
        health_check_grace_period=cdk.Duration.seconds(120),
        max_healthy_percent=200,
        min_healthy_percent=50,
        cloud_map_options=ecs.CloudMapOptions(
            cloud_map_namespace=cloud_map,
            dns_record_type=cm.DnsRecordType.A,
            dns_ttl=cdk.Duration.seconds(300),
            failure_threshold=1,
            name=node_name
        ),
    )