ApiGateway Class
Use an API Gateway REST APIs as a target for Amazon EventBridge rules.
Example
ts
import * as api from 'aws-cdk-lib/aws-apigateway';
import * as lambda from 'aws-cdk-lib/aws-lambda';
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.rate(Duration.minutes(1)),
});
const fn = new lambda.Function( this, 'MyFunc', {
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_LATEST,
code: lambda.Code.fromInline( 'exports.handler = e => {}' ),
} );
const restApi = new api.LambdaRestApi( this, 'MyRestAPI', { handler: fn } );
const dlq = new sqs.Queue(this, 'DeadLetterQueue');
rule.addTarget(
new targets.ApiGateway( restApi, {
path: '/*/test',
method: 'GET',
stage: 'prod',
pathParameterValues: ['path-value'],
headerParameters: {
Header1: 'header1',
},
queryStringParameters: {
QueryParam1: 'query-param-1',
},
deadLetterQueue: dlq
} ),
)
python
import aws_cdk.aws_apigateway as api
import aws_cdk.aws_lambda as lambda_
rule = events.Rule(self, "Rule",
schedule=events.Schedule.rate(Duration.minutes(1))
)
fn = lambda_.Function(self, "MyFunc",
handler="index.handler",
runtime=lambda_.Runtime.NODEJS_LATEST,
code=lambda_.Code.from_inline("exports.handler = e => {}")
)
rest_api = api.LambdaRestApi(self, "MyRestAPI", handler=fn)
dlq = sqs.Queue(self, "DeadLetterQueue")
rule.add_target(
targets.ApiGateway(rest_api,
path="/*/test",
method="GET",
stage="prod",
path_parameter_values=["path-value"],
header_parameters={
"Header1": "header1"
},
query_string_parameters={
"QueryParam1": "query-param-1"
},
dead_letter_queue=dlq
))
java
import software.amazon.awscdk.services.apigateway.*;
import software.amazon.awscdk.services.lambda.*;
Rule rule = Rule.Builder.create(this, "Rule")
.schedule(Schedule.rate(Duration.minutes(1)))
.build();
Function fn = Function.Builder.create(this, "MyFunc")
.handler("index.handler")
.runtime(Runtime.NODEJS_LATEST)
.code(Code.fromInline("exports.handler = e => {}"))
.build();
LambdaRestApi restApi = LambdaRestApi.Builder.create(this, "MyRestAPI").handler(fn).build();
Queue dlq = new Queue(this, "DeadLetterQueue");
rule.addTarget(
ApiGateway.Builder.create(restApi)
.path("/*/test")
.method("GET")
.stage("prod")
.pathParameterValues(List.of("path-value"))
.headerParameters(Map.of(
"Header1", "header1"))
.queryStringParameters(Map.of(
"QueryParam1", "query-param-1"))
.deadLetterQueue(dlq)
.build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.APIGateway;
using Amazon.CDK.AWS.Lambda;
var rule = new Rule(this, "Rule", new RuleProps {
Schedule = Schedule.Rate(Duration.Minutes(1))
});
var fn = new Function(this, "MyFunc", new FunctionProps {
Handler = "index.handler",
Runtime = Runtime.NODEJS_LATEST,
Code = Code.FromInline("exports.handler = e => {}")
});
var restApi = new LambdaRestApi(this, "MyRestAPI", new LambdaRestApiProps { Handler = fn });
var dlq = new Queue(this, "DeadLetterQueue");
rule.AddTarget(
new ApiGateway(restApi, new ApiGatewayProps {
Path = "/*/test",
Method = "GET",
Stage = "prod",
PathParameterValues = new [] { "path-value" },
HeaderParameters = new Dictionary<string, string> {
{ "Header1", "header1" }
},
QueryStringParameters = new Dictionary<string, string> {
{ "QueryParam1", "query-param-1" }
},
DeadLetterQueue = dlq
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import api "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
rule := events.NewRule(this, jsii.String("Rule"), &RuleProps{
Schedule: events.Schedule_Rate(awscdk.Duration_Minutes(jsii.Number(1))),
})
fn := lambda.NewFunction(this, jsii.String("MyFunc"), &FunctionProps{
Handler: jsii.String("index.handler"),
Runtime: lambda.Runtime_NODEJS_LATEST(),
Code: lambda.Code_FromInline(jsii.String("exports.handler = e => {}")),
})
restApi := api.NewLambdaRestApi(this, jsii.String("MyRestAPI"), &LambdaRestApiProps{
Handler: fn,
})
dlq := sqs.NewQueue(this, jsii.String("DeadLetterQueue"))
rule.AddTarget(
targets.NewApiGateway(restApi, &ApiGatewayProps{
Path: jsii.String("/*/test"),
Method: jsii.String("GET"),
Stage: jsii.String("prod"),
PathParameterValues: []*string{
jsii.String("path-value"),
},
HeaderParameters: map[string]*string{
"Header1": jsii.String("header1"),
},
QueryStringParameters: map[string]*string{
"QueryParam1": jsii.String("query-param-1"),
},
DeadLetterQueue: dlq,
}))
Initializer
ts
new ApiGateway(restApi: IRestApi, props?: ApiGatewayProps)
python
ApiGateway(rest_api: IRestApi, props: typing.Optional[ApiGatewayProps] = None)
java
new ApiGateway(IRestApi restApi, ApiGatewayProps props)
csharp
new ApiGateway(IRestApi restApi, ApiGatewayProps? props = null)
go
NewApiGateway(RestApi IRestApi, Props ApiGatewayProps)
Properties
Methods
bind(rule: IRuleRef, _id?: string): RuleTargetConfigdef bind(self, rule: IRuleRef, _id: typing.Optional[str] = None) -> RuleTargetConfigpublic RuleTargetConfig bind(IRuleRef rule, String _id)public RuleTargetConfig Bind(IRuleRef rule, string? _id = null)func Bind(Rule IRuleRef, _id *string) RuleTargetConfig#Returns a RuleTarget that can be used to trigger this API Gateway REST APIs as a result from an EventBridge event.