0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:CloudFrontOriginAccessIdentityId が存在しない?CloudFormationエラーの原因と対処法

Posted at

はじめに

本記事では、AWS CloudFormation を用いて CloudFront ディストリビューションを作成する際に発生したエラー:

リソース CloudFrontDistribution は CREATE_FAILED 状態です
Requested attribute CloudFrontOriginAccessIdentityId does not exist in schema for AWS::CloudFront::CloudFrontOriginAccessIdentity

について、その原因と修正方法を個人の備忘録として記録しています。

書こうと思ったきっかけ

CloudFront + S3 の構成を CloudFormation でコード化していた際に、CloudFront の OAI(Origin Access Identity)を指定する部分でエラーが発生しました。

エラー内容から、リソースの属性指定に誤りがあることが判明したため、備忘録として整理しました。

エラーの原因

CloudFront の OAI に対して、以下のように属性を指定していたことが原因です:

!Sub "origin-access-identity/cloudfront/${CloudFrontOAI.CloudFrontOriginAccessIdentityId}"

しかし、AWS::CloudFront::CloudFrontOriginAccessIdentity リソースには CloudFrontOriginAccessIdentityId という属性は存在しません。

正しい指定方法

CloudFormation では、CloudFront の OriginAccessIdentity に対して 論理ID を指定するだけで CloudFormation が内部的に適切な形式に変換してくれます。以下のように修正します:

S3OriginConfig:
  OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOAI}"

修正後のテンプレート例(該当箇所)

Origins:
  - Id: S3Origin
    DomainName: !GetAtt S3Bucket.RegionalDomainName
    S3OriginConfig:
      OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOAI}"

補足

AWS::CloudFront::CloudFrontOriginAccessIdentity には以下の2つの属性があります:

  • S3CanonicalUserId(バケットポリシーで使用)
  • 実際の OAI ID(例: E***********) → CloudFormation からは直接取得不可

そのため、CloudFront 側では論理IDベースでの指定がベストプラクティスです。

まとめ

CloudFormation で CloudFront OAI を指定する際は、リソースの論理IDを ${} で埋め込む形で !Sub を使ってみてください!

NG: ${CloudFrontOAI.CloudFrontOriginAccessIdentityId}

OK: ${CloudFrontOAI}

エラーが出た際は、公式ドキュメントやスキーマに存在する属性かどうかも確認すると、解決への近道になります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?