Skip to content

Conversation

@takaokouji
Copy link
Contributor

Summary

This PR adds AWS WAFv2 configuration to the AppSync GraphQL API to restrict access based on the Origin header. This restriction is only applied to the prod (production) stage.

Implementation Details

  • Created a CfnWebACL with a rule AllowSpecificOrigins.
  • The rule uses a ByteMatchStatement on the Origin header with a LOWERCASE transformation.
  • Allowed origins:
    • https://smalruby.app
    • https://smalruby.jp
  • Requests from other origins (including localhost) or without an Origin header are blocked by the default action.
  • Associated the WebACL with the AppSync API using CfnWebACLAssociation.

Test Coverage

  • Added unit tests in test/mesh-v2.test.ts to:
    • Verify that WAF resources are created when STAGE=prod.
    • Verify that WAF resources are NOT created for other stages (e.g., stg).
    • Verify the specific rules and allowed origins in the WebACL.

Part of smalruby/smalruby3-develop#10

- Create a WAFv2 WebACL for the production stage.
- Restrict the 'Origin' header to 'https://smalruby.app' and 'https://smalruby.jp'.
- Associate the WebACL with the AppSync GraphQL API.
- Add unit tests to verify WAF creation and origin restriction logic.

Part of smalruby/smalruby3-develop#10
- Set isolatedModules: true in tsconfig.json to resolve ts-jest warning.
- Replace deprecated DnsValidatedCertificate with Certificate in mesh-v2-stack.ts.
- Note: Removed region: 'us-east-1' from certificate as AppSync custom domains are regional and Certificate construct uses the stack's region.
@takaokouji
Copy link
Contributor Author

調査結果: 証明書作成の修正について

問題

コミット 6b4652bDnsValidatedCertificate から Certificate に変更したことにより、以下の問題が発生:

Resource handler returned message: "Invalid request provided: Certificate must be in us-east-1.

根本原因

  1. AppSync の仕様: AppSync カスタムドメインは CloudFront を使用するため、証明書は 必ず us-east-1 にある必要がある(AWS ドキュメント
  2. CDK の制限: Certificate construct には region パラメータがなく、スタックのリージョン(ap-northeast-1)で証明書を作成してしまう

npm test 警告の真の原因

元々の警告は ts-jest の警告であり、DnsValidatedCertificate とは無関係でした:

  • 原因: tsconfig.jsonisolatedModules が設定されていなかった
  • 解決: isolatedModules: true を追加(これは正しい修正)

推奨する修正方針

DnsValidatedCertificate に戻す必要があります:

理由:

  • AppSync カスタムドメインには us-east-1 の証明書が必須
  • Certificate construct にはクロスリージョン対応がない
  • DnsValidatedCertificate は非推奨だが、代替手段が提供されていない
  • CDK v3 のリリース予定はまだなく、当分使用可能

代替案の検討:

  • ❌ 別スタック作成 + crossRegionReferences: オーバースペック
  • Certificate の使用: us-east-1 に証明書を作成できない
  • DnsValidatedCertificate の継続使用: 最もシンプルで実用的

非推奨警告について:

修正内容

コミット 6b4652b の以下の変更を元に戻します:

const certificate = new acm.DnsValidatedCertificate(this, 'ApiCertificate', {
  domainName: customDomain,
  hostedZone: zone,
  region: 'us-east-1',
});

tsconfig.jsonisolatedModules: true は維持します(ts-jest の警告対応として正しい)。

この修正により、既存の証明書を削除せずに継続使用できます。

参考資料

AppSync custom domains require certificates to be located in us-east-1.
The standard Certificate construct does not support cross-region creation,
so reverting to DnsValidatedCertificate with region: 'us-east-1' to
ensure successful deployment in regional stacks (e.g., ap-northeast-1).

This resolves the "Certificate must be in us-east-1" error during deployment.

Co-Authored-By: Gemini <noreply@google.com>
@takaokouji takaokouji merged commit 068ace0 into main Jan 10, 2026
3 checks passed
@takaokouji takaokouji deleted the feature/waf-origin-restriction branch January 10, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants