iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💣
[AWS CDK] Pitfalls when deploying API Gateway with WAFv2 Web ACL
This is a note regarding how I resolved the error AWS WAF couldn't perform the operation because your resource doesn't exist.
Environment
- aws-cdk-lib 2.30.0
- constructs 10.1.43
CDK Code
Here is the code.
import { Construct } from "constructs"
import * as cdk from "aws-cdk-lib"
export class WafStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
// APIGW+Lambda
const restApi = new cdk.aws_apigateway.RestApi(this, "restApi", {
restApiName: "restApi",
deployOptions: {
stageName: "dev",
},
})
const pingFn = new cdk.aws_lambda.Function(this, "pingFn", {
code: cdk.aws_lambda.Code.fromInline(`
exports.handler = (event, context, callback) => {
callback(null, { statusCode: 200, body: JSON.stringify({ message: "pong" }) });
};
`),
handler: "index.handler",
runtime: cdk.aws_lambda.Runtime.NODEJS_16_X,
})
restApi.root.addMethod(
"GET",
new cdk.aws_apigateway.LambdaIntegration(pingFn)
)
// WAF Web ACL
const webAcl = new cdk.aws_wafv2.CfnWebACL(this, "wafV2WebAcl", {
defaultAction: { allow: {} },
scope: "REGIONAL",
visibilityConfig: {
cloudWatchMetricsEnabled: true,
sampledRequestsEnabled: true,
metricName: "wafV2WebAcl",
},
rules: [
{
name: "AWSManagedRulesCommonRuleSet",
priority: 1,
statement: {
managedRuleGroupStatement: {
vendorName: "AWS",
name: "AWSManagedRulesCommonRuleSet",
},
},
overrideAction: { none: {} },
visibilityConfig: {
cloudWatchMetricsEnabled: true,
sampledRequestsEnabled: true,
metricName: "AWSManagedRulesCommonRuleSet",
},
},
],
})
// Associate APIGW with WebACL
const webAclAssociation = new cdk.aws_wafv2.CfnWebACLAssociation(
this,
"webAclAssociation",
{
resourceArn: `arn:aws:apigateway:${this.region}::/restapis/${restApi.restApiId}/stages/dev`,
webAclArn: webAcl.attrArn,
}
)
webAclAssociation.addDependsOn(webAcl)
webAclAssociation.addDependsOn(restApi.node.defaultChild as cdk.CfnResource)
}
}
Issue
The following error occurred.
12:28:38 AM | CREATE_FAILED | AWS::WAFv2::WebACLAssociation | webAclAssociation
Resource handler returned message: "AWS WAF couldn?t perform the operation because your resource doesn?t exist. (Service: Wafv2, Status Cod
e: 400, Request ID: xxxxxxxxxxxxxxxxxxxxxxxxx, Extended Request ID: null)" (xxxxxxxxxxxxxxxxxxxxxxx,
HandlerErrorCode: NotFound)
What I struggled with
webAclAssociation.addDependsOn(webAcl)
webAclAssociation.addDependsOn(restApi.node.defaultChild as cdk.CfnResource)
I was scratching my head because even though I had written the above, I was still getting an error saying the resource did not exist.
Solution
According to this, it seems necessary to explicitly wait for the stage within API Gateway, so I tried that approach.
- webAclAssociation.addDependsOn(restApi.node.defaultChild as cdk.CfnResource)
+ webAclAssociation.addDependsOn(
+ restApi.deploymentStage.node.defaultChild as cdk.CfnResource
+ )
Specifying restApi.deploymentStage.node.defaultChild in addDependsOn resolved the issue.
References
- amazon web services - AWS SAM - AWS::WAFv2::WebACLAssociation - AWS WAF couldn?t perform the operation because your resource doesn?t exist - Stack Overflow
- Constructed WAFv2 with AWS CDK and tried IP address restriction | DevelopersIO
- [Update] AWS WAF logs can now be output directly to CloudWatch Logs and S3 | DevelopersIO
- Built AWS WAF with CloudFormation (January 2022 version) | DevelopersIO
- Create a Lambda state machine for Step Functions using AWS CDK - AWS Step Functions
Discussion