Skip to content

Commit 68e1e85

Browse files
iamhopaul123mergify[bot]
authored andcommitted
feat(ecs): add support for ProxyConfiguration in ECS TaskDefinition (#4007)
* Add proxyConfiguration support * Fix test case * Improve the user experience when setting proxy configuration * Add base proxy configuration class and an app mesh proxy configuration class * Add generic proxy configuration class * Remove generic proxy configuration and minor changes * Add integ test, reword error message * Fix integ test and unit test * Update with support for empty string for egressIgnoredIPs/Ports * Refactoring bind method * Minor refactoring to renderProperties method
1 parent bd36c6c commit 68e1e85

9 files changed

Lines changed: 1313 additions & 2 deletions

packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Construct, IResource, Lazy, Resource } from '@aws-cdk/core';
44
import { ContainerDefinition, ContainerDefinitionOptions, PortMapping, Protocol } from '../container-definition';
55
import { CfnTaskDefinition } from '../ecs.generated';
66
import { PlacementConstraint } from '../placement';
7+
import { ProxyConfiguration } from '../proxy-configuration/proxy-configuration';
78

89
/**
910
* The interface for all task definitions.
@@ -64,6 +65,13 @@ export interface CommonTaskDefinitionProps {
6465
*/
6566
readonly taskRole?: iam.IRole;
6667

68+
/**
69+
* The configuration details for the App Mesh proxy.
70+
*
71+
* @default - No proxy configuration.
72+
*/
73+
readonly proxyConfiguration?: ProxyConfiguration;
74+
6775
/**
6876
* The list of volume definitions for the task. For more information, see
6977
* [Task Definition Parameter Volumes](https://docs.aws.amazon.com/AmazonECS/latest/developerguide//task_definition_parameters.html#volumes).
@@ -279,6 +287,7 @@ export class TaskDefinition extends TaskDefinitionBase {
279287
placementConstraints: Lazy.anyValue({ produce: () =>
280288
!isFargateCompatible(this.compatibility) ? this.placementConstraints : undefined
281289
}, { omitEmptyArray: true }),
290+
proxyConfiguration: props.proxyConfiguration ? props.proxyConfiguration.bind(this.stack, this) : undefined,
282291
cpu: props.cpu,
283292
memory: props.memoryMiB,
284293
});

packages/@aws-cdk/aws-ecs/lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export * from './log-drivers/syslog-log-driver';
3030
export * from './log-drivers/log-driver';
3131
export * from './log-drivers/log-drivers';
3232

33+
export * from './proxy-configuration/app-mesh-proxy-configuration';
34+
export * from './proxy-configuration/proxy-configuration';
35+
export * from './proxy-configuration/proxy-configurations';
36+
3337
// AWS::ECS CloudFormation Resources:
3438
//
3539
export * from './ecs.generated';
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { Construct } from '@aws-cdk/core';
2+
import { TaskDefinition } from '../base/task-definition';
3+
import { CfnTaskDefinition } from '../ecs.generated';
4+
import { ProxyConfiguration } from './proxy-configuration';
5+
6+
/**
7+
* Interface for setting the properties of proxy configuration.
8+
*/
9+
export interface AppMeshProxyConfigurationProps {
10+
/**
11+
* The user ID (UID) of the proxy container as defined by the user parameter in a container definition.
12+
* This is used to ensure the proxy ignores its own traffic. If IgnoredGID is specified, this field can be empty.
13+
*/
14+
readonly ignoredUID?: number;
15+
16+
/**
17+
* The group ID (GID) of the proxy container as defined by the user parameter in a container definition.
18+
* This is used to ensure the proxy ignores its own traffic. If IgnoredUID is specified, this field can be empty.
19+
*/
20+
readonly ignoredGID?: number;
21+
22+
/**
23+
* The list of ports that the application uses.
24+
* Network traffic to these ports is forwarded to the ProxyIngressPort and ProxyEgressPort.
25+
*/
26+
readonly appPorts: number[];
27+
28+
/**
29+
* Specifies the port that incoming traffic to the AppPorts is directed to.
30+
*/
31+
readonly proxyIngressPort: number;
32+
33+
/**
34+
* Specifies the port that outgoing traffic from the AppPorts is directed to.
35+
*/
36+
readonly proxyEgressPort: number;
37+
38+
/**
39+
* The egress traffic going to these specified ports is ignored and not redirected to the ProxyEgressPort. It can be an empty list.
40+
*/
41+
readonly egressIgnoredPorts?: number[];
42+
43+
/**
44+
* The egress traffic going to these specified IP addresses is ignored and not redirected to the ProxyEgressPort. It can be an empty list.
45+
*/
46+
readonly egressIgnoredIPs?: string[];
47+
}
48+
49+
/**
50+
* The configuration to use when setting an App Mesh proxy configuration.
51+
*/
52+
export interface AppMeshProxyConfigurationConfigProps {
53+
/**
54+
* The name of the container that will serve as the App Mesh proxy.
55+
*/
56+
readonly containerName: string;
57+
58+
/**
59+
* The set of network configuration parameters to provide the Container Network Interface (CNI) plugin.
60+
*/
61+
readonly properties: AppMeshProxyConfigurationProps;
62+
}
63+
64+
/**
65+
* The class for App Mesh proxy configurations.
66+
*
67+
* For tasks using the EC2 launch type, the container instances require at least version 1.26.0 of the container agent and at least version
68+
* 1.26.0-1 of the ecs-init package to enable a proxy configuration. If your container instances are launched from the Amazon ECS-optimized
69+
* AMI version 20190301 or later, then they contain the required versions of the container agent and ecs-init.
70+
* For more information, see [Amazon ECS-optimized AMIs](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html).
71+
*
72+
* For tasks using the Fargate launch type, the task or service requires platform version 1.3.0 or later.
73+
*/
74+
export class AppMeshProxyConfiguration extends ProxyConfiguration {
75+
/**
76+
* Constructs a new instance of the AppMeshProxyConfiguration class.
77+
*/
78+
constructor(private readonly props: AppMeshProxyConfigurationConfigProps) {
79+
super();
80+
if (props.properties) {
81+
if (!props.properties.ignoredUID && !props.properties.ignoredGID) {
82+
throw new Error("At least one of ignoredUID or ignoredGID should be specified.");
83+
}
84+
}
85+
}
86+
87+
/**
88+
* Called when the proxy configuration is configured on a task definition.
89+
*/
90+
public bind(_scope: Construct, _taskDefinition: TaskDefinition): CfnTaskDefinition.ProxyConfigurationProperty {
91+
const configProps = this.props.properties;
92+
const configType = "APPMESH";
93+
return {
94+
containerName: this.props.containerName,
95+
proxyConfigurationProperties: renderProperties(configProps),
96+
type: configType
97+
};
98+
}
99+
}
100+
101+
function renderProperties(props: AppMeshProxyConfigurationProps): CfnTaskDefinition.KeyValuePairProperty[] {
102+
const ret = [];
103+
for (const [k, v] of Object.entries(props)) {
104+
const key = String(k);
105+
const value = String(v);
106+
if (value !== "undefined" && value !== "") {
107+
const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);
108+
ret.push({ ["name"]: capitalizedKey, ["value"]: value });
109+
}
110+
}
111+
return ret;
112+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Construct } from '@aws-cdk/core';
2+
import { TaskDefinition } from '../base/task-definition';
3+
import { CfnTaskDefinition } from '../ecs.generated';
4+
5+
/**
6+
* The base class for proxy configurations.
7+
*/
8+
export abstract class ProxyConfiguration {
9+
/**
10+
* Called when the proxy configuration is configured on a task definition.
11+
*/
12+
public abstract bind(_scope: Construct, _taskDefinition: TaskDefinition): CfnTaskDefinition.ProxyConfigurationProperty;
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AppMeshProxyConfiguration, AppMeshProxyConfigurationConfigProps } from './app-mesh-proxy-configuration';
2+
import { ProxyConfiguration } from './proxy-configuration';
3+
4+
/**
5+
* The base class for proxy configurations.
6+
*/
7+
export class ProxyConfigurations {
8+
/**
9+
* Constructs a new instance of the ProxyConfiguration class.
10+
*/
11+
public static appMeshProxyConfiguration(props: AppMeshProxyConfigurationConfigProps): ProxyConfiguration {
12+
return new AppMeshProxyConfiguration(props);
13+
}
14+
}

0 commit comments

Comments
 (0)