简体   繁体   English

将 ELB 帐户分配给 S3 存储桶策略

[英]Assign ELB Account to S3 Bucket Policy

I used the AWS console from the load balancer edit attributes screen and used it to create a bucket to use for access logging.我使用了负载均衡器编辑属性屏幕中的 AWS 控制台,并用它创建了一个用于访问日志记录的存储桶。 I'm using this policy to form CDK code in typescript to stand up new S3 buckets to use for access logging in higher level environments where I cannot use the console.我正在使用此策略在 typescript 中形成 CDK 代码,以建立新的 S3 存储桶,用于在我无法使用控制台的更高级别环境中进行访问日志记录。 This is the policy I need to somehow form in typescript CDK code:这是我需要以某种方式在 typescript CDK 代码中形成的策略:

"Statement": [
{
    "Effect":Allow",
    "Principal": {
        "AWS": "arn:--ELB-arnstuff--:root"
    },
    "Action": "s3:PutObject",
    "Resource": "arn:--S3-Bucket-arnstuff--/AWSLogs/123456789/*"
}
]

I've managed to get the cdk code figured out to this point:到目前为止,我已经设法弄清楚了 cdk 代码:

bucket.addToResourcePolicy(
    new cdk.aws_iam.PolicyStatement({ 
      effect: awsIam.Effect.ALLOW,
      principals: //'**This is part I haven't figured out**',
      actions: ['s3:PutObject'],
      resources: ['${bucket.bucketArn}/*']
    })
);

At this point I don't care if it's hard coded in the CDK, I just need something to help keep the ball rolling forward.在这一点上,我不关心它是否在 CDK 中进行了硬编码,我只需要一些东西来帮助保持球向前滚动。 Any help is appreciated, thanks任何帮助表示赞赏,谢谢

The bucket policy, along with aws accounts to be used are described in aws docs :存储桶策略以及要使用的 aws 帐户在aws 文档中进行了描述:

Region  Region name     Elastic Load Balancing account ID
us-east-1   US East (N. Virginia)   127311923021
us-east-2   US East (Ohio)  033677994240
us-west-1   US West (N. California)     027434742980
us-west-2   US West (Oregon)    797873946194
af-south-1  Africa (Cape Town)  098369216593
ca-central-1    Canada (Central)    985666609251
eu-central-1    Europe (Frankfurt)  054676820928
eu-west-1   Europe (Ireland)    156460612806
eu-west-2   Europe (London)     652711504416
eu-south-1  Europe (Milan)  635631232127
eu-west-3   Europe (Paris)  009996457667
eu-north-1  Europe (Stockholm)  897822967062
ap-east-1   Asia Pacific (Hong Kong)    754344448648
ap-northeast-1  Asia Pacific (Tokyo)    582318560864
ap-northeast-2  Asia Pacific (Seoul)    600734575887
ap-northeast-3  Asia Pacific (Osaka)    383597477331
ap-southeast-1  Asia Pacific (Singapore)    114774131450
ap-southeast-2  Asia Pacific (Sydney)   783225319266
ap-southeast-3  Asia Pacific (Jakarta)  589379963580
ap-south-1  Asia Pacific (Mumbai)   718504428378
me-south-1  Middle East (Bahrain)   076674570225
sa-east-1   South America (São Paulo)   507241528517
us-gov-west-1*  AWS GovCloud (US-West)  048591011584
us-gov-east-1*  AWS GovCloud (US-East)  190560391635
cn-north-1*     China (Beijing)     638102146993
cn-northwest-1*     China (Ningxia)     037604701340

I figured out why it didn't work initially when I tried to do a.fromJson and just take the AWS generated policy and consume it directly into the CDK.当我尝试执行 a.fromJson 并只是采用 AWS 生成的策略并将其直接用于 CDK 时,我弄清楚了为什么它最初不起作用。 The addToResourcePolicy function expects only one object at a time. addToResourcePolicy function 一次只期望一个 object。 The AWS generated policy had 3 objects in it. AWS 生成的策略中有 3 个对象。 When I tried to use.fromJson I was passing too many objects at once.当我尝试使用 .fromJson 时,我一次传递了太多对象。

In my snippet above I only provided one of those and I was trying to find the cdk equivalent Principal object to use for an AWS ELB Account.在我上面的代码片段中,我只提供了其中一个,我试图找到 cdk 等效的 Principal object 以用于 AWS ELB 帐户。 I found a work around by using the.fromJson for just this one policy object:我通过将 the.fromJson 用于这个策略 object 找到了解决方法:

bucket.addToResourcePolicy(
  cdk.aws_iam.PolicyStatement.fromJson({
    "Effect":Allow",
    "Principal": {
      "AWS": "arn:--ELB-arnstuff--:root"
    },
    "Action": "s3:PutObject",
    "Resource": "arn:--S3-Bucket-arnstuff--/AWSLogs/123456789/*"
  })
);

Note the removal of the new operator when consuming a Json object and if you're running into the issue I had with multiple policies you'll need to create a bucket.addToResourcePolicy block of code for each of the Json object policies you wish to apply.请注意,在使用 Json object 时删除了 new 运算符,如果您遇到我在使用多个策略时遇到的问题,则需要为您希望应用的每个 Json object 策略创建一个 bucket.addToResourcePolicy 代码块.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM