By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Create Fargate service with multiple DNSRecordTypes A and SRV

0

I create my ECS Fargate Service with following code.

		const exposedContainer = this.exposedContainer;

		this.service = this.service = new FargateService(this, `${baseId}/service`, {
			cluster: ecsCluster,
			taskDefinition: this.taskDefinition,
			desiredCount: replicas,
			assignPublicIp: publicip,
			serviceName: serviceName ?? exposedContainer.props.image.imageName,
			securityGroups: [securityGroup],
			cloudMapOptions: {
				container: exposedContainer.definition,
				cloudMapNamespace,
				name: exposedContainer.props.cloudmapServiceName,
				//dnsRecordType: DnsRecordType.A,
				dnsRecordType: DnsRecordType.SRV,
				containerPort: exposedContainer.props.exposedPort.containerPort,				
			},
			volumeConfigurations: [],
		});
		const CfnService = this.service.cloudMapService?.node.defaultChild as CfnService;
		CfnService.dnsConfig = {
			...CfnService.dnsConfig,
			dnsRecords: [
				{
					ttl: 60,
					type: 'A',
				},
				{
					ttl: 60,
					type: 'SRV',
				},
			],
		};

I need A and SRV but I can only set dnsRecordType: DnsRecordType.SRV or dnsRecordType: DnsRecordType.A not both in cloudMapOptions. What I do wrong?

asked a month ago56 views
2 Answers
2

You're on the right track, but there's a slight modification needed in your approach. When creating a Fargate service with AWS Cloud Map integration, you can indeed have both A and SRV records, but you need to specify this differently.

Instead of trying to set multiple DNS record types in the cloudMapOptions, you should use the cloudMapOptions to set up the initial configuration, and then modify the underlying CloudFormation resource to add the additional DNS record type.

Here's how you can modify your code to achieve this:

  1. In the cloudMapOptions, specify either A or SRV as the primary DNS record type.
  2. After creating the Fargate service, access the underlying CloudFormation resource for the Cloud Map service.
  3. Override the dnsConfig property of this resource to include both A and SRV record types.

Your approach of modifying the CfnService is correct. However, make sure you're not overwriting the existing configuration. Instead, you should extend it. Here's a slight modification to your code:

const CfnService = this.service.cloudMapService?.node.defaultChild as CfnService;
CfnService.dnsConfig = {
    ...CfnService.dnsConfig,
    dnsRecords: [
        { ttl: 60, type: 'A' },
        { ttl: 60, type: 'SRV' }
    ],
};

This way, you're preserving any other configurations that might be present in the dnsConfig while adding both A and SRV record types.

Remember that when you specify settings for an SRV record and provide values for AWS_INSTANCE_IPV4 or AWS_INSTANCE_IPV6 in the RegisterInstance request, AWS Cloud Map automatically creates A and/or AAAA records with the same name as the service-hostname in the SRV record.

By following this approach, you should be able to create your Fargate service with both A and SRV DNS record types in Cloud Map.
Sources
AWS::ServiceDiscovery::Service DnsRecord - AWS CloudFormation
aws-alb-fargate - AWS Solutions Constructs
AWS::EKS::FargateProfile - AWS CloudFormation

profile picture
answered a month ago
profile picture
EXPERT
reviewed a month ago
0

When creating multiple DNS records and TYPES in cloudmap options have you tried allowing the cloud map options to replicate your original then just modify the record source.

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions