IVS Real-Time StreamingでBucketPolicyのcfnでalready exists食らった
めちゃめちゃマニアックなAWSのサービスの話。
Amazon IVS Real-Time streamingという、動画のライブ配信サービスの使用を検討している最中。
このサービスでは、動画の配信の際に、配信者ごとの録画を自動的に記録し、S3に保存する仕組みを利用することができます。
関連するリソース
- AWS::IVS::StorageConfiguration : IVSの録画設定。S3バケットを指定する
- AWS::S3::Bucket : 録画の保存先のS3バケット (BucketName:
test-ivs-bucket) - AWS::S3::BucketPolicy : CDNからS3バケットを参照するためのバケットポリシー
- AWS::CloudFront::Distribution : S3バケットのCDN
これらを、CloudFormationのyamlに記述して、適用してみたところ、
S3::BucketPolicyの適用時にCREATE_FAILEDしてしまい、以下のエラーが発生しました。
The bucket policy already exists on bucket test-ivs-bucket.
yamlを見渡しても、 test-ivs-bucket S3バケットに対する S3::BucketPolicyの定義は1つしかありません。
解決のヒントになったのは、こちらの記事でした。
エラー内容もIVSも直接の関係はないのですが、ヒントになったのは「BucketPolicyを同時に作る」「CloudTrailで見る」という点でした。CloudFormationの実行記録をCloudTrailで見るという視点が自分には足りていませんでした。
CloudTrailのイベント履歴ページで、該当時間帯の リソースタイプ AWS::S3::Bucket のイベントを見てみると、確かに PutBucketPolicy が記録されています。しかし、中身は自分の予想とは違いました。
"eventName": "PutBucketPolicy",
"awsRegion": "ap-northeast-1",
"sourceIPAddress": "ivschat.amazonaws.com",
"userAgent": "ivschat.amazonaws.com",
"requestParameters": {
"bucketPolicy": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Composite-XyzXyZX123XY",
"Resource": "arn:aws:s3:::test-ivs-bucket/*",
"Effect": "Allow",
"Principal": {
"Service": "ivs-composite.ap-northeast-1.amazonaws.com"
},
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Condition": {
"Bool": {
"aws:SecureTransport": "true"
},
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
}
]
},
"bucketName": "test-ivs-bucket",
"Host": "test-ivs-bucket.s3.ap-northeast-1.amazonaws.com",
"policy": ""
},
どうやらIVSがBucketPolicyを作っていたようです。同じタイミングで独自のBucketPolicyを割り当てようとして、競合?と言ってよいのかわかりませんが、エラーになっていたようです。
リソースの依存関係(DependsOn)を以下のように見直しました。
before:
S3::Bucket <- S3::BucketPolicy
<- CloudFront::Distribution
<- IVS::StorageConfiguration
after:
S3::Bucket <- S3::BucketPolicy <- IVS::StorageConfiguration
<- CloudFront::Distribution
これで無事にリソースが作成されました。
ちなみに、IVS::StorageConfigurationがBucketPolicyを作る話は、以下のURLで言及が有りました。
When a StorageConfiguration object is created, IVS will get access to write content to the specified S3 bucket. This access is granted by making modifications to the S3 bucket's policy. If the policy for the bucket is altered in a way that removes IVS's access, ongoing and new recordings will fail.
Composite Recodingの機能は使うつもりがなかったので、ノーマークでした。
Discussion