Bir*_*sky 8 amazon-s3 amazon-web-services aws-cloudformation amazon-iam aws-cdk
我想将这个 CloudFormation 片段翻译成 CDK:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: S3BucketImageUploadBuffer
PolicyDocument:
Version: "2012-10-17"
Statement:
Action:
- s3:PutObject
- s3:PutObjectAcl
Effect: Allow
Resource:
- ...
Run Code Online (Sandbox Code Playgroud)
小智 17
这是来自工作 CDK-Stack 的示例:
artifactBucket.addToResourcePolicy(
new PolicyStatement({
resources: [
this.pipeline.artifactBucket.arnForObjects("*"),
this.pipeline.artifactBucket.bucketArn],
],
actions: ["s3:List*", "s3:Get*"],
principals: [new ArnPrincipal(this.deploymentRole.roleArn)]
})
);
Run Code Online (Sandbox Code Playgroud)
基于@Thomas Wagner 的回答,我就是这样做的。我试图将存储桶限制为给定的 IP 范围:
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as s3Deployment from '@aws-cdk/aws-s3-deployment';
import * as iam from '@aws-cdk/aws-iam';
export class StaticSiteStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Bucket where frontend site goes.
const mySiteBucket = new s3.Bucket(this, 'mySiteBucket', {
websiteIndexDocument: "index.html"
});
let ipLimitPolicy = new iam.PolicyStatement({
actions: ['s3:Get*', 's3:List*'],
resources: [mySiteBucket.arnForObjects('*')],
principals: [new iam.AnyPrincipal()]
});
ipLimitPolicy.addCondition('IpAddress', {
"aws:SourceIp": ['1.2.3.4/22']
});
// Allow connections from my CIDR
mySiteBucket.addToResourcePolicy(ipLimitPolicy);
// Deploy assets
const mySiteDeploy = new s3Deployment.BucketDeployment(this, 'deployAdminSite', {
sources: [s3Deployment.Source.asset("./mysite")],
destinationBucket: mySiteBucket
});
}
}
Run Code Online (Sandbox Code Playgroud)
我能够使用s3.arnForObjects()和iam.AnyPrincipal()辅助函数,而不是直接指定 ARN 或 Principals。
我想要部署到存储桶的资产保存在我项目目录的根目录中的一个名为 的目录中mysite,然后通过调用s3Deployment.BucketDeployment. 当然,这可以是您的构建过程可以访问的任何目录。
| 归档时间: |
|
| 查看次数: |
8676 次 |
| 最近记录: |