An AWS CDK library that provides ECS service discovery functionality for an ECS task's public IP address
Project description
ECS Public Discovery
The poor man's alternative to the recommended approach of using an ELB for public ingress into your ECS service.
The provided CDK construct offers similar functionality to what AWS provides through ECS Service Discovery (also known as AWS Cloud Map), but instead of creating a DNS entry with the private IP address of the task (even with a public Cloud Map namespace), it will register the public IP address of the task.
This can support services with multiple tasks with Route53 multivalue answer routing.
Installation
TypeScript / JavaScript
npm install cdk-ecs-public-discovery
or
yarn add cdk-ecs-public-discovery
Python
pip install cdk-ecs-public-discovery
Java
<dependency>
<groupId>com.engineal.cdk</groupId>
<artifactId>ecs-public-discovery</artifactId>
</dependency>
C# / .Net
dotnet add package EngineAL.CDK.EcsPublicDiscovery
Usage
Create a new EcsPublicDiscovery
construct for your ECS cluster. Provide the Route53 Hosted Zone to create DNS entries
in.
const cluster = new ecs.Cluster(stack, 'TestCluster');
const hostedZone = route53.HostedZone.fromHostedZoneAttributes(stack, 'HostedZone', {
hostedZoneId: 'Z1R8UBAEXAMPLE',
zoneName: 'example.com'
});
const ecsPublicDiscovery = new EcsPublicDiscovery(stack, 'EcsPublicDiscovery', {
cluster,
hostedZone
});
Then for each service you create in that cluster that you want to enable public routing to, set assignPublicIp
to
true
and register it with the EcsPublicDiscovery
construct you created:
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TestTaskDefinition');
taskDefinition.addContainer('TestContainer', {
image: ecs.ContainerImage.fromRegistry('hello-world')
});
const service = new ecs.FargateService(stack, 'TestService', {
assignPublicIp: true,
cluster,
taskDefinition
});
ecsPublicDiscovery.addService({
// eslint-disable-next-line no-magic-numbers
dnsTtl: cdk.Duration.minutes(1),
name: 'test',
service
});
Details
This construct creates a Lambda function that is triggered by an EventBridge rule that listens for when an ECS task is running or has stopped.
Each ECS service registered with this construct is tagged with the public-discovery:name
and optionally the
public-discovery:ttl
tags based on the props you provide, which will be propagated to the service's tasks and the
network interface attached to the task. The ECS task definition must use the AwsVpc network mode, and the ECS service
must assign a public IP to its tasks' network interface.
When a task is running, the Lambda function will call the EC2 DescribeNetworkInterfaces
API action to describe the
task's network interface to look up the public IP address and tags assigned to the task. It will then call the
Route53 ChangeResourceRecordSets
API action to upsert an A record, using the public IP address as the value of the
resource record, the value of the public-discovery:name
tag as the name, the value of the public-discovery:ttl
tag
as the TTL if present or 60 seconds if absent, and the task's id as the set identifier to allow for multivalue routing.
When the task has stopped, the Lambda function will call the Route53 ListResourceRecordSets
API action to look up the
resource record set for the task's id and will then call the Route53 ChangeResourceRecordSets
API action to delete
that A record.
Useful commands
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run package
generates libraries for all languagesnpm run test
perform the jest unit tests
License
Copyright 2023 Aaron Lucia
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cdk-ecs-public-discovery-1.0.1.tar.gz
.
File metadata
- Download URL: cdk-ecs-public-discovery-1.0.1.tar.gz
- Upload date:
- Size: 11.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce43c2f90a5dd686e7ee5e797b290d814a2518733f53219bd1d3995110870b83 |
|
MD5 | 6e5f3f1c0a2eb6f5f265adbcac00fbc3 |
|
BLAKE2b-256 | 457bd6ea804760a9336635e2ba0f742a930f7f98986a27bd088986ddcd4c75d6 |
File details
Details for the file cdk_ecs_public_discovery-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: cdk_ecs_public_discovery-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.1 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf8414026ff1d7dd8557581c1df1bae88583ba041cd676a80cb8119ac897253c |
|
MD5 | 8a38326b087f7cd9b12b5c95cba451ae |
|
BLAKE2b-256 | 620b582439cdee0b9e1013f57653d8e3930ff43ffbe7fa0b1c07b2cde603b3f6 |