æ¬æ¥ã®ãé¡
AWS CDKï¼ä»¥ä¸ãCDKãã¨è¨è¼ï¼ã«ããã¦ãæ¢åã®S3ãã±ãããå¥ãã±ããã®ããµã¼ãã¼ã¢ã¯ã»ã¹ãã°ãã®éä¿¡å ã«è¨å®ããã¨ãCloudFormationï¼ä»¥ä¸ãCFnãã¨è¨è¼ï¼ã«ãããã¤ããéã«ã¨ã©ã¼ã«ãªã
詳細
ï¼Webãã¹ãã£ã³ã°ãè¡ã£ã¦ããS3ãã±ãããªã©ï¼Public Readã«è¨å®ãã¦ããS3ãã±ããã«ããã¦ããµã¼ãã¼ã¢ã¯ã»ã¹ãã°ãæå¹ã«ãã¦ã¢ã¯ã»ã¹ç£è¦ã»è§£æãããã¨ãããã¨ã¯ãéç¨ä¸ããããã¨æãã¾ããï¼å®ç¾©ã«ã¤ãã¦ã¯ãåèæ å ±ï¼AWS CDKã§ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããæ¹æ³ããåç §ï¼
ã¨ãããããµã¼ãã¼ã¢ã¯ã»ã¹ãã°ã®éä¿¡å ã«æ¢åã®ãã±ãããæå®ããå ´åãããããããã¤ããã¨ãCloudFormationã§ãªãã以ä¸ã®ã¨ã©ã¼ãçºçããå ´åãããã¾ãã
Resource handler returned message: "The bucket does not allow ACLs (Service: S3, Status Code: 400, Request ID:..(ä¸ç¥)..., HandlerErrorCode: InvalidRequest)
æ¬æ¥ã¯ãã®åé¡ã«ã¤ãã¦ã®ã話ã§ãã
ãããªãçµè«
å ã«çµè«ããè¨ã£ã¦ãã¾ãã¨ãããµã¼ãã¼ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããå ´åãCDKããACLãæå¹ã«ããCFnãã³ãã¬ã¼ãããåºåããããã¨ãã¨ã©ã¼ã®åå ã§ãã
ãµã¼ãã¼ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããå ´åãCDKã¯éä¿¡å ãã±ããã®ããããã£ã¨ãã¦ãä¸è¨CFnãã³ãã¬ã¼ããåºåãã¾ãã
ãã ããã®ãã³ãã¬ã¼ãã¯ãAccessControl
ã ObjectOwnership
(=ãªãã¸ã§ã¯ãææè
) ã ObjectWriter
ã ã£ãããããã¨ãããåããéããACLï¼Access Control Listï¼ãæå¹ãã®æã®ãã³ãã¬ã¼ãã«ãªã£ã¦ãã¾ãã
{ "AccessControl": "LogDeliveryWrite", "BucketName": "fortune-tmm-auth0-logo-bucket-dev-accesslog", "OwnershipControls": { "Rules": [ { "ObjectOwnership": "ObjectWriter" } ] }, }
ãããS3ã®ããã©ã«ãã¯ãACLç¡å¹ããªã®ã§*1ãæ¢åãã±ãã(=ACLãç¡å¹)ã«å¯¾ãã¦ãã®è¨å®ãé©ç¨ãããã¨ããã¨ããACLç¡å¹ã®ãã±ããã«ACLæå¹æã®ããããã£ãè¨å®ãããã¨ãã¦ãããã¨ãªããã¨ã©ã¼ã«ãªã£ã¦ãã¾ãã¾ãã
ãªããããã¯ããã¾ã§ãæ¢åãã±ãããã®å ´åã§ãããæ°è¦ä½æãããã±ãããªãã¨ã©ã¼ã¯çºçããªãããããã¾ããããç¾å¨ã¯S3ãã±ããã®ã¢ã¯ã»ã¹å¶å¾¡ã¯ãã±ããããªã·ã¼ã使ç¨ãããã¨ãæ¨å¥¨ããã¦ããããããACLæå¹ãã®ããããã£ãåºåããã®ã¯ãã¾ããããããªãã§ãã
ããããã£ãï¼å¼·å¼ã«ï¼æ¸ãæãã
ä¸è¨ã®ç¾è±¡ã¯ãACLæå¹ã®è¨å®ãå¼·å¶çã«æ¸ãæããããã¨ã§å¯¾å¿ã§ãã¾ãã
å ·ä½çã«ã¯ä¸è¨ã®å¯¾å¿ããã¾ãã
AccessControl
ãåé¤ããObjectOwnership
ãBucketOwnerEnforced
(=ãã±ããææè ã®å¼·å¶)ã«ãããã¾ãã¯OwnershipControls
ãåé¤ãã*2
ãã ãL2 Constructã§ã¯ä¸è¨ãå®è¡ã§ããªãã®ã§ãnode.defaultChild
ã使ç¨ãã¦L1 Constructã«å¤æããå¾ã§å®è¡ãã¾ãã
å ·ä½çã«ã¯ãå è¿°ã®ã½ã¼ã¹ã®æ«å°¾ã«ä¸è¨ã½ã¼ã¹ã追å ãã¾ãã
const cfnLogBucket = logBucket.node.defaultChild as s3.CfnBucket; // ObjectOwnershipã®ä¸æ¸ãã // ãã¡ããaddPropertyDeletionOverride('OwnershipControls') ã§ãOK cfnLogBucket.addPropertyOverride('OwnershipControls.Rules.0.ObjectOwnership', 'BucketOwnerEnforced'); // AccessControlã®åé¤ cfnLogBucket.addPropertyDeletionOverride('AccessControl');
ãªããaddProperty
ç³»ã¡ã½ããã§ããããã£ã«é
åã®ãã¼ãæå®ããæ¹æ³ã¯ã以ä¸ã®CDKå
¬å¼ããã¥ã¡ã³ããåç
§ãã¦ãã ããã
addOverride(path, value)
åèæ å ±ï¼AWS CDKã§ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããæ¹æ³
AWS CDKã§ãµã¼ãã¼ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããå ´åãä¸è¨ã³ã¼ããè¨è¼ãã¾ãã
import * as cdk from 'aws-cdk-lib'; import { aws_iam as iam, aws_s3 as s3 } from 'aws-cdk-lib'; // ã¢ã¯ã»ã¹ãã°ãæå¹ã«ãããã±ããã®ãã±ããå const departureBucketName = `departure`; // ã¢ã¯ã»ã¹ãã°ã®éä¿¡å Bucket const logBucket = new s3.Bucket(this, `LogBucket`, { bucketName: 'destinationLogBucket' , removalPolicy: cdk.RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE, }); // ã¢ã¯ã»ã¹ãã°ã®éä¿¡å Bucketã®ãã±ããããªã·ã¼ logBucket.addToResourcePolicy( new iam.PolicyStatement({ effect: iam.Effect.ALLOW, principals: [new iam.ServicePrincipal('logging.s3.amazonaws.com')], actions: ['s3:PutObject'], resources: [`arn:aws:s3:::destinationLogBucket/*`], conditions: { ArnLike: { 'aws:SourceArn': `arn:aws:s3:::${departureBucketName}`, }, StringEquals: { 'aws:SourceAccount': <ã¢ã«ã¦ã³ãçªå·>, }, }, }), ); // ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããBucket const departureBucket = new s3.Bucket(this, `DepartureBucket`, { // blockPublicAccessã¯ç¡ãã¦ããããã blockPublicAccess: s3.BlockPublicAccess.BLOCK_ACLS, bucketName: departureBucketName, publicReadAccess: true, removalPolicy: cdk.RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE, serverAccessLogsBucket: logBucket, serverAccessLogsPrefix: 'logs/', }); // ã¢ã¯ã»ã¹ãã°ãæå¹ã«ããBucketã®ãã±ããããªã·ã¼ // iam.StarPrincipal()ã¯ãPrincipal: '*'ãã¨ããå®ç¾©ãä½æããã¡ã½ãã departureBucket .addToResourcePolicy( new iam.PolicyStatement({ effect: iam.Effect.ALLOW, principals: [new iam.StarPrincipal()], actions: ['s3:GetObject'], resources: [`arn:aws:s3:::${departureBucketName}/*`], }), );
ãªãããµã¼ãã¼ã¢ã¯ã»ã¹ãã°ã§éä¿¡å
ãã±ããã«å¿
è¦ãªãã±ããããªã·ã¼ã«ã¤ãã¦ã¯ãä¸è¨ã®AWSå
¬å¼ããã¥ã¡ã³ããåèã«ãã¦ãã ããã
docs.aws.amazon.com
ããã§ã¯ãä»åã¯ãã®è¾ºã§