-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecs-service-extensions): Publish Extension (#16326)
---- This PR adds a new service extension, `PublisherExtension`. This extension can be added to a service to allow it to publish events to SNS Topics. (This PR when paired with #16049 can be used to set up the pub/ sub architecture pattern) It sets up publish permissions for the service to be able to publish events to the topics provided. The user can also provide a list of accounts that will be given permissions to subscribe to the given topics. *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
6 changed files
with
1,411 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/injecter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import * as ecs from '@aws-cdk/aws-ecs'; | ||
import * as sns from '@aws-cdk/aws-sns'; | ||
import { Service } from '../service'; | ||
import { Container } from './container'; | ||
import { ContainerMutatingHook, ServiceExtension } from './extension-interfaces'; | ||
|
||
// Keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from '@aws-cdk/core'; | ||
|
||
/** | ||
* An interface that will be implemented by all the resources that can be published events or written data to. | ||
*/ | ||
export interface Injectable { | ||
environmentVariables(): { [key: string]: string }; | ||
} | ||
|
||
/** | ||
* An interface that will be implemented by all the injectable resources that need to grant permissions to the task role. | ||
*/ | ||
export interface GrantInjectable extends Injectable { | ||
grant(taskDefinition: ecs.TaskDefinition): void; | ||
} | ||
|
||
/** | ||
* The settings for the `InjectableTopic` class. | ||
*/ | ||
export interface InjectableTopicProps { | ||
/** | ||
* The SNS Topic to publish events to. | ||
*/ | ||
readonly topic: sns.ITopic; | ||
} | ||
|
||
/** | ||
* The `InjectableTopic` class represents SNS Topic resource that can be published events to by the parent service. | ||
*/ | ||
|
||
export class InjectableTopic implements GrantInjectable { | ||
public readonly topic: sns.ITopic; | ||
|
||
constructor(props: InjectableTopicProps) { | ||
this.topic = props.topic; | ||
} | ||
|
||
public grant(taskDefinition: ecs.TaskDefinition) { | ||
this.topic.grantPublish(taskDefinition.taskRole); | ||
} | ||
|
||
public environmentVariables(): { [key: string]: string } { | ||
let environment: { [key: string]: string } = {}; | ||
environment[`${this.topic.node.id.toUpperCase()}_TOPIC_ARN`] = this.topic.topicArn; | ||
return environment; | ||
} | ||
} | ||
|
||
/** | ||
* The settings for the Injecter extension. | ||
*/ | ||
export interface InjecterExtensionProps { | ||
/** | ||
* The list of injectable resources for this service. | ||
*/ | ||
readonly injectables: Injectable[]; | ||
} | ||
|
||
/** | ||
* Settings for the hook which mutates the application container | ||
* to add the injectable resource environment variables. | ||
*/ | ||
interface ContainerMutatingProps { | ||
/** | ||
* The resource environment variables to be added to the container environment. | ||
*/ | ||
readonly environment: { [key: string]: string }; | ||
} | ||
|
||
/** | ||
* This hook modifies the application container's environment to | ||
* add the injectable resource environment variables. | ||
*/ | ||
class InjecterExtensionMutatingHook extends ContainerMutatingHook { | ||
private environment: { [key: string]: string }; | ||
|
||
constructor(props: ContainerMutatingProps) { | ||
super(); | ||
this.environment = props.environment; | ||
} | ||
|
||
public mutateContainerDefinition(props: ecs.ContainerDefinitionOptions): ecs.ContainerDefinitionOptions { | ||
return { | ||
...props, | ||
|
||
environment: { ...(props.environment || {}), ...this.environment }, | ||
} as ecs.ContainerDefinitionOptions; | ||
} | ||
} | ||
|
||
/** | ||
* This extension accepts a list of `Injectable` resources that the parent service can publish events or write data to. | ||
* It sets up the corresponding permissions for the task role of the parent service. | ||
*/ | ||
export class InjecterExtension extends ServiceExtension { | ||
private props: InjecterExtensionProps; | ||
|
||
private environment: { [key: string]: string } = {}; | ||
|
||
constructor(props: InjecterExtensionProps) { | ||
super('injecter'); | ||
|
||
this.props = props; | ||
} | ||
|
||
// @ts-ignore - Ignore unused params that are required for abstract class extend | ||
public prehook(service: Service, scope: Construct) { | ||
this.parentService = service; | ||
|
||
for (const injectable of this.props.injectables) { | ||
for (const [key, val] of Object.entries(injectable.environmentVariables())) { | ||
this.environment[key] = val; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add hooks to the main application extension so that it is modified to | ||
* add the injectable resource environment variables to the container environment. | ||
*/ | ||
public addHooks() { | ||
const container = this.parentService.serviceDescription.get('service-container') as Container; | ||
|
||
if (!container) { | ||
throw new Error('Injecter Extension requires an application extension'); | ||
} | ||
|
||
container.addContainerMutatingHook(new InjecterExtensionMutatingHook({ | ||
environment: this.environment, | ||
})); | ||
} | ||
|
||
/** | ||
* After the task definition has been created, this hook grants the required permissions to the task role for the | ||
* parent service. | ||
* | ||
* @param taskDefinition The created task definition | ||
*/ | ||
public useTaskDefinition(taskDefinition: ecs.TaskDefinition) { | ||
for (const injectable of this.props.injectables) { | ||
if ((injectable as GrantInjectable).grant !== undefined) { | ||
(injectable as GrantInjectable).grant(taskDefinition); | ||
} | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
packages/@aws-cdk-containers/ecs-service-extensions/test/injecter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { countResources, expect, haveResource } from '@aws-cdk/assert-internal'; | ||
import * as ecs from '@aws-cdk/aws-ecs'; | ||
import * as sns from '@aws-cdk/aws-sns'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Container, Environment, InjecterExtension, InjectableTopic, Service, ServiceDescription } from '../lib'; | ||
|
||
describe('injecter', () => { | ||
test('correctly sets publish permissions for given topics', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
const environment = new Environment(stack, 'production'); | ||
const serviceDescription = new ServiceDescription(); | ||
|
||
serviceDescription.add(new Container({ | ||
cpu: 256, | ||
memoryMiB: 512, | ||
trafficPort: 80, | ||
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), | ||
environment: { | ||
PORT: '80', | ||
}, | ||
})); | ||
|
||
// WHEN | ||
const topic1 = new InjectableTopic({ | ||
topic: new sns.Topic(stack, 'topic1'), | ||
}); | ||
|
||
const topic2 = new InjectableTopic({ | ||
topic: new sns.Topic(stack, 'topic2'), | ||
}); | ||
|
||
serviceDescription.add(new InjecterExtension({ | ||
injectables: [topic1, topic2], | ||
})); | ||
|
||
new Service(stack, 'my-service', { | ||
environment, | ||
serviceDescription, | ||
}); | ||
|
||
// THEN | ||
// Ensure creation of provided topics | ||
expect(stack).to(countResources('AWS::SNS::Topic', 2)); | ||
|
||
// Ensure the task role is given permissions to publish events to topics | ||
expect(stack).to(haveResource('AWS::IAM::Policy', { | ||
PolicyDocument: { | ||
Statement: [ | ||
{ | ||
Action: 'sns:Publish', | ||
Effect: 'Allow', | ||
Resource: { | ||
Ref: 'topic152D84A37', | ||
}, | ||
}, | ||
{ | ||
Action: 'sns:Publish', | ||
Effect: 'Allow', | ||
Resource: { | ||
Ref: 'topic2A4FB547F', | ||
}, | ||
}, | ||
], | ||
Version: '2012-10-17', | ||
}, | ||
})); | ||
|
||
// Ensure that the topic ARNs have been correctly appended to the environment variables | ||
expect(stack).to(haveResource('AWS::ECS::TaskDefinition', { | ||
ContainerDefinitions: [ | ||
{ | ||
Cpu: 256, | ||
Environment: [ | ||
{ | ||
Name: 'PORT', | ||
Value: '80', | ||
}, | ||
{ | ||
Name: 'TOPIC1_TOPIC_ARN', | ||
Value: { | ||
Ref: 'topic152D84A37', | ||
}, | ||
}, | ||
{ | ||
Name: 'TOPIC2_TOPIC_ARN', | ||
Value: { | ||
Ref: 'topic2A4FB547F', | ||
}, | ||
}, | ||
], | ||
Image: 'nathanpeck/name', | ||
Essential: true, | ||
Memory: 512, | ||
Name: 'app', | ||
PortMappings: [ | ||
{ | ||
ContainerPort: 80, | ||
Protocol: 'tcp', | ||
}, | ||
], | ||
Ulimits: [ | ||
{ | ||
HardLimit: 1024000, | ||
Name: 'nofile', | ||
SoftLimit: 1024000, | ||
}, | ||
], | ||
}, | ||
], | ||
})); | ||
}); | ||
}); |
Oops, something went wrong.