-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ecs-service-extensions): Publish Extension #16326
Merged
mergify
merged 17 commits into
aws:master
from
upparekh:upparekh/publish-service-extension
Sep 17, 2021
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0bc9a8e
Added publish extension
upparekh c221a10
IPublisher methods for accessing env vars
upparekh 3a7f64f
README update
upparekh 05ed02f
Updated env var key
upparekh ca7378e
Added integ test
upparekh f7fc4f5
Updated README
upparekh dc37e37
README updates after review
upparekh f5f9994
README update after review
upparekh 03d3ea7
Updated env var injection
upparekh cb9db5e
Removed account IDs
upparekh e4d50d7
Refactor for adding Injectable interfaces
upparekh b0c5523
name update: InjectableTopic
upparekh dba8b7f
Updated README
upparekh fa7d351
Updated env var names
upparekh 8e92b07
Test refactor for failing build
upparekh a6eb709
Deleted test file
upparekh e76126c
Merge branch 'master' into upparekh/publish-service-extension
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome example! 🤩