forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-apigatewayv2-integrations): Introduce L2 construct for Event…
…Bridge-PutEvents AWS service integration This is in reference to GitHub Issue aws#11947 *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
12 changed files
with
12,618 additions
and
9,475 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/@aws-cdk/aws-apigatewayv2-integrations/lib/http/aws/integration.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,23 @@ | ||
import { | ||
AwsServiceIntegrationSubtype, | ||
HttpConnectionType, | ||
HttpIntegrationType, | ||
HttpRouteIntegrationBindOptions, | ||
HttpRouteIntegrationConfig, | ||
IHttpRouteIntegration, | ||
PayloadFormatVersion, | ||
} from '@aws-cdk/aws-apigatewayv2'; | ||
|
||
/** | ||
* The HTTP Private integration resource for HTTP API | ||
* | ||
* @internal | ||
*/ | ||
export abstract class AwsServiceIntegration implements IHttpRouteIntegration { | ||
protected connectionType = HttpConnectionType.INTERNET | ||
protected integrationSubtype?: AwsServiceIntegrationSubtype; | ||
protected integrationType = HttpIntegrationType.AWS_PROXY; | ||
protected payloadFormatVersion = PayloadFormatVersion.VERSION_1_0; // 1.0 is required and is the only supported format | ||
|
||
public abstract bind(options: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig; | ||
} |
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
76 changes: 76 additions & 0 deletions
76
packages/@aws-cdk/aws-apigatewayv2-integrations/lib/http/eventbridge.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,76 @@ | ||
import { | ||
HttpRouteIntegrationBindOptions, | ||
HttpRouteIntegrationConfig, | ||
EventBridgeIntegrationRequestParameters, | ||
AwsServiceIntegrationSubtype, | ||
} from '@aws-cdk/aws-apigatewayv2'; | ||
import { PolicyStatement, Role, ServicePrincipal } from '@aws-cdk/aws-iam'; | ||
import { IEventBus } from '../../../aws-events'; | ||
import { AwsServiceIntegration } from './aws/integration'; | ||
import { AwsServiceIntegrationOptions } from './base-types'; | ||
|
||
/** | ||
* Properties to initialize `EventBridgeIntegration`. | ||
*/ | ||
export interface EventBridgeIntegrationProps extends AwsServiceIntegrationOptions { | ||
/** | ||
* The EventBridge API call to proxy to. | ||
* @default AwsServiceIntegrationSubtype.EVENT_BRIDGE_PUT_EVENTS | ||
*/ | ||
readonly integrationSubtype?: AwsServiceIntegrationSubtype; | ||
|
||
/** | ||
* The event bus to bind proxy to. | ||
*/ | ||
readonly eventBus: IEventBus; | ||
|
||
/** | ||
* The event source. | ||
*/ | ||
readonly eventSource?: string; | ||
|
||
/** | ||
* The EventBridge PutEvents request parameters. | ||
* https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEvents.html | ||
*/ | ||
readonly requestParameters: EventBridgeIntegrationRequestParameters; | ||
} | ||
|
||
/** | ||
* The EventBridge integration resource for HTTP API | ||
*/ | ||
export class EventBridgeIntegration extends AwsServiceIntegration { | ||
constructor(private readonly props: EventBridgeIntegrationProps) { | ||
super(); | ||
} | ||
|
||
public bind(options: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig { | ||
this.integrationSubtype = this.props.integrationSubtype ?? AwsServiceIntegrationSubtype.EVENT_BRIDGE_PUT_EVENTS; | ||
|
||
const { scope } = options; | ||
|
||
const role = new Role(scope, 'EventBridgeIntegrationRole', { | ||
description: 'Role for API Gateway to publish Events', | ||
assumedBy: new ServicePrincipal('apigateway.amazonaws.com'), | ||
}); | ||
|
||
role.addToPolicy(new PolicyStatement({ | ||
actions: ['events:PutEvents'], | ||
resources: [this.props.eventBus.eventBusArn], | ||
conditions: (() => !('eventSource' in this.props) ? undefined : { | ||
StringEquals: { | ||
'events:source': this.props.eventSource, | ||
}, | ||
})(), | ||
})); | ||
|
||
return { | ||
payloadFormatVersion: this.payloadFormatVersion, | ||
type: this.integrationType, | ||
connectionType: this.connectionType, | ||
credentialsArn: role.roleArn, | ||
integrationSubtype: this.integrationSubtype, | ||
requestParameters: this.props.requestParameters, | ||
}; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -38,4 +38,4 @@ export class HttpProxyIntegration implements IHttpRouteIntegration { | |
uri: this.props.url, | ||
}; | ||
} | ||
} | ||
} |
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
143 changes: 143 additions & 0 deletions
143
packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.eventbridge.expected.json
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,143 @@ | ||
{ | ||
"Resources": { | ||
"EventBus7B8748AA": { | ||
"Type": "AWS::Events::EventBus", | ||
"Properties": { | ||
"Name": "integeventbridgeproxyEventBus554075B3" | ||
} | ||
}, | ||
"EventBridgeProxyApi73E23498": { | ||
"Type": "AWS::ApiGatewayV2::Api", | ||
"Properties": { | ||
"Name": "EventBridgeProxyApi", | ||
"ProtocolType": "HTTP" | ||
} | ||
}, | ||
"EventBridgeProxyApiDefaultRouteEventBridgeIntegrationRole3A0AD809": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "apigateway.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"Description": "Role for API Gateway to publish Events" | ||
} | ||
}, | ||
"EventBridgeProxyApiDefaultRouteEventBridgeIntegrationRoleDefaultPolicy3589E340": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "events:PutEvents", | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"EventBus7B8748AA", | ||
"Arn" | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "EventBridgeProxyApiDefaultRouteEventBridgeIntegrationRoleDefaultPolicy3589E340", | ||
"Roles": [ | ||
{ | ||
"Ref": "EventBridgeProxyApiDefaultRouteEventBridgeIntegrationRole3A0AD809" | ||
} | ||
] | ||
} | ||
}, | ||
"EventBridgeProxyApiDefaultRoute7A237CAD": { | ||
"Type": "AWS::ApiGatewayV2::Route", | ||
"Properties": { | ||
"ApiId": { | ||
"Ref": "EventBridgeProxyApi73E23498" | ||
}, | ||
"RouteKey": "$default", | ||
"AuthorizationScopes": [], | ||
"Target": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"integrations/", | ||
{ | ||
"Ref": "EventBridgeProxyApiHttpIntegrationbdce00b62e57d880c36340c0f0df5a1d0AD4F597" | ||
} | ||
] | ||
] | ||
} | ||
} | ||
}, | ||
"EventBridgeProxyApiHttpIntegrationbdce00b62e57d880c36340c0f0df5a1d0AD4F597": { | ||
"Type": "AWS::ApiGatewayV2::Integration", | ||
"Properties": { | ||
"ApiId": { | ||
"Ref": "EventBridgeProxyApi73E23498" | ||
}, | ||
"IntegrationType": "AWS_PROXY", | ||
"ConnectionType": "INTERNET", | ||
"CredentialsArn": { | ||
"Fn::GetAtt": [ | ||
"EventBridgeProxyApiDefaultRouteEventBridgeIntegrationRole3A0AD809", | ||
"Arn" | ||
] | ||
}, | ||
"IntegrationSubtype": "EventBridge-PutEvents", | ||
"PayloadFormatVersion": "1.0", | ||
"RequestParameters": { | ||
"Detail": "$request.body.result", | ||
"DetailType": "$request.body.description", | ||
"EventBusName": { | ||
"Ref": "EventBus7B8748AA" | ||
}, | ||
"Source": "test", | ||
"Time": "$context.requestTimeEpoch" | ||
} | ||
} | ||
}, | ||
"EventBridgeProxyApiDefaultStage642846E1": { | ||
"Type": "AWS::ApiGatewayV2::Stage", | ||
"Properties": { | ||
"ApiId": { | ||
"Ref": "EventBridgeProxyApi73E23498" | ||
}, | ||
"StageName": "$default", | ||
"AutoDeploy": true | ||
} | ||
} | ||
}, | ||
"Outputs": { | ||
"Endpoint": { | ||
"Value": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"https://", | ||
{ | ||
"Ref": "EventBridgeProxyApi73E23498" | ||
}, | ||
".execute-api.", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
".", | ||
{ | ||
"Ref": "AWS::URLSuffix" | ||
}, | ||
"/" | ||
] | ||
] | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.eventbridge.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,27 @@ | ||
import { HttpApi } from '@aws-cdk/aws-apigatewayv2'; | ||
import { App, CfnOutput, Stack } from '@aws-cdk/core'; | ||
import { EventBus } from '../../../aws-events'; | ||
import { EventBridgeIntegration } from '../../lib'; | ||
|
||
const app = new App(); | ||
|
||
const stack = new Stack(app, 'integ-eventbridge-proxy'); | ||
|
||
const eventBus = new EventBus(stack, 'EventBus'); | ||
|
||
const endpoint = new HttpApi(stack, 'EventBridgeProxyApi', { | ||
defaultIntegration: new EventBridgeIntegration({ | ||
eventBus, | ||
requestParameters: { | ||
eventBusName: eventBus.eventBusName, | ||
source: 'test', | ||
detail: '$request.body.result', | ||
detailType: '$request.body.description', | ||
time: '$context.requestTimeEpoch', | ||
}, | ||
}), | ||
}); | ||
|
||
new CfnOutput(stack, 'Endpoint', { | ||
value: endpoint.url!, | ||
}); |
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
Oops, something went wrong.