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(events): API Gateway target #13823
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bdbe1b9
add api gateway target
hedrall 1f41d00
adding test in progress
hedrall 06a2343
add test
hedrall befba87
resolved a conflict
hedrall db50077
add to readme
hedrall 6664ad6
Merge branch 'master' into events-add-api-gateway
hedrall c4223a9
Merge branch 'master' of https://github.com/aws/aws-cdk into events-a…
hedrall 6da030c
fix review points
hedrall 041e398
Merge branch 'master' into events-add-api-gateway
hedrall ae1f56a
fix to use assert-internal
hedrall 36c8ea1
Merge branch 'master' into events-add-api-gateway
hedrall 4f01a64
Merge branch 'master' into events-add-api-gateway
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
123 changes: 123 additions & 0 deletions
123
packages/@aws-cdk/aws-events-targets/lib/api-gateway.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,123 @@ | ||
import * as api from '@aws-cdk/aws-apigateway'; | ||
import * as events from '@aws-cdk/aws-events'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { addToDeadLetterQueueResourcePolicy, bindBaseTargetConfig, singletonEventRole, TargetBaseProps } from './util'; | ||
|
||
/** | ||
* Customize the API Gateway Event Target | ||
*/ | ||
export interface ApiGatewayProps extends TargetBaseProps { | ||
|
||
/** | ||
* The method for api resource invoked by the rule. | ||
* | ||
* @default '*' that treated as ANY | ||
*/ | ||
readonly method?: string; | ||
|
||
/** | ||
* The api resource invoked by the rule. | ||
* We can use wildcards('*') to specify the path. In that case, | ||
* an equal number of real values must be specified for pathParameterValues. | ||
* | ||
* @default '/' | ||
*/ | ||
readonly path?: string; | ||
|
||
/** | ||
* The deploy stage of api gateway invoked by the rule. | ||
* | ||
* @default the value of deploymentStage.stageName of target api gateway. | ||
*/ | ||
readonly stage?: string; | ||
|
||
/** | ||
* The headers to be set when requesting API | ||
* | ||
* @default no header parameters | ||
*/ | ||
readonly headerParameters?: { [key: string]: (string) }; | ||
|
||
/** | ||
* The path parameter values to be used to | ||
* populate to wildcards("*") of requesting api path | ||
* | ||
* @default no path parameters | ||
*/ | ||
readonly pathParameterValues?: string[]; | ||
|
||
/** | ||
* The query parameters to be set when requesting API. | ||
* | ||
* @default no querystring parameters | ||
*/ | ||
readonly queryStringParameters?: { [key: string]: (string) }; | ||
|
||
/** | ||
* This will be the post request body send to the API. | ||
* | ||
* @default the entire EventBridge event | ||
*/ | ||
readonly postBody?: events.RuleTargetInput; | ||
|
||
/** | ||
* The role to assume before invoking the target | ||
* (i.e., the pipeline) when the given rule is triggered. | ||
* | ||
* @default - a new role will be created | ||
*/ | ||
readonly eventRole?: iam.IRole; | ||
} | ||
|
||
/** | ||
* Use an API Gateway REST APIs as a target for Amazon EventBridge rules. | ||
*/ | ||
export class ApiGateway implements events.IRuleTarget { | ||
|
||
constructor(public readonly restApi: api.RestApi, private readonly props?: ApiGatewayProps) { | ||
} | ||
|
||
/** | ||
* Returns a RuleTarget that can be used to trigger this API Gateway REST APIs | ||
* as a result from an EventBridge event. | ||
* | ||
* @see https://docs.aws.amazon.com/eventbridge/latest/userguide/resource-based-policies-eventbridge.html#sqs-permissions | ||
*/ | ||
public bind(rule: events.IRule, _id?: string): events.RuleTargetConfig { | ||
if (this.props?.deadLetterQueue) { | ||
addToDeadLetterQueueResourcePolicy(rule, this.props.deadLetterQueue); | ||
} | ||
|
||
const wildcardCountsInPath = this.props?.path?.match( /\*/g )?.length ?? 0; | ||
if (wildcardCountsInPath !== (this.props?.pathParameterValues || []).length) { | ||
throw new Error('The number of wildcards in the path does not match the number of path pathParameterValues.'); | ||
} | ||
|
||
const restApiArn = this.restApi.arnForExecuteApi( | ||
this.props?.method, | ||
this.props?.path || '/', | ||
this.props?.stage || this.restApi.deploymentStage.stageName, | ||
); | ||
return { | ||
...(this.props ? bindBaseTargetConfig(this.props) : {}), | ||
arn: restApiArn, | ||
role: this.props?.eventRole || singletonEventRole(this.restApi, [new iam.PolicyStatement({ | ||
resources: [restApiArn], | ||
actions: [ | ||
'execute-api:Invoke', | ||
'execute-api:ManageConnections', | ||
], | ||
})]), | ||
deadLetterConfig: this.props?.deadLetterQueue && { arn: this.props.deadLetterQueue?.queueArn }, | ||
input: this.props?.postBody, | ||
targetResource: this.restApi, | ||
httpParameters: { | ||
headerParameters: this.props?.headerParameters, | ||
queryStringParameters: this.props?.queryStringParameters, | ||
pathParameterValues: this.props?.pathParameterValues, | ||
}, | ||
}; | ||
} | ||
|
||
} | ||
|
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.
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.
Should we not validate
pathParameterValues.length
agains the number of*
in thepath
?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.
I didn't know if it would be better to validate with CDK or wait for Cloudformation to handle the errors naturally at runtime.
(If it is documented somewhere, I would appreciate it if you could let me know.)
I have implemented validation as you mentioned.