aws / aws/aws-cdk

API Gateway: LambdaRestApi defaultCorsPreflightOptions not attaching to children

Open
#28,512 2 comments 3 reactions 0 assignees View on GitHub
@aws-cdk/aws-apigateway bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

I'm instantiating a LambdaRestApi with the `defaultCorsPreflightOptions` property. However, the OPTIONS method that handles the CORS configuration for that property is not automatically being attached to all child resources like the property claims.

### Expected Behavior

I expected that defining the `defaultCorsPreflightOptions` would apply an OPTIONS method to all child resources of the API instance.

### Current Behavior

Only the root resource has an OPTIONS method attached to it.

### Reproduction Steps

Dependencies:

```
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/node": "20.7.1",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"aws-cdk": "2.101.0",
"ts-node": "^10.9.1",
"typescript": "~5.2.2"
},
"dependencies": {
"aws-cdk-lib": "2.101.0",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
```
LambdaRestApi instance:

```ts
this.restApi = new apiGateway.LambdaRestApi(
this,
`PricetoolLambdaRestApi${stage}`,
{
restApiName: `pt-lambda-rest-api-${stage}`,
proxy: false,
defaultCorsPreflightOptions: {
allowOrigins: apiGateway.Cors.ALL_ORIGINS,
allowMethods: apiGateway.Cors.ALL_METHODS,
allowHeaders: ["*"],
},
deployOptions: {
accessLogDestination: new apiGateway.LogGroupLogDestination(
apiLogGroup
),
accessLogFormat: apiGateway.AccessLogFormat.jsonWithStandardFields(),
loggingLevel: apiGateway.MethodLoggingLevel.ERROR,
},
cloudWatchRole: true,
cloudWatchRoleRemovalPolicy: cdk.RemovalPolicy.DESTROY,
handler: new lambda.Function(this, `APIBaseLambda${stage}`, {
functionName: `ptApiBase${stage}`,
runtime: lambda.Runtime.NODEJS_16_X,
handler: "index.handler",
code: lambda.Code.fromInline(`
exports.handler = async () => {
return {
statusCode: 200,
body: "Hello, world!",
};
};
`),
}),
}
);
```

### Possible Solution

_No response_

### Additional Information/Context

I am configuring the API resources/methods/lambda integrations in a nested stack. I can supply the code for that if needed, I'm unsure if it's relevant.

### CDK CLI Version

2.101.0

### Framework Version

_No response_

### Node.js Version

v18

### OS

Linux

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the LambdaRestApi configuration with defaultCorsPreflightOptions and nested resources, then inspect how the option is applied to the root and child resources. Done means child resources receive the expected OPTIONS method and CORS configuration, with regression coverage for the reported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.