Lon*_*Dev 12 amazon-s3 amazon-web-services terraform elastic-load-balancer
我在使用Terraform(v0.9.2)向ELB添加服务时遇到了问题(我正在使用:https://github.com/segmentio/stack/blob/master/s3-logs/main.tf).
当我运行时,terraform apply
我收到此错误:
* module.solr.module.elb.aws_elb.main: 1 error(s) occurred:
* aws_elb.main: Failure configuring ELB attributes:
InvalidConfigurationRequest: Access Denied for bucket: my-service-
logs. Please check S3bucket permission
status code: 409, request id: xxxxxxxxxx-xxxx-xxxx-xxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
我的服务如下:
module "solr" {
source = "github.com/segmentio/stack/service"
name = "${var.prefix}-${terraform.env}-solr"
environment = "${terraform.env}"
image = "123456789876.dkr.ecr.eu-west-2.amazonaws.com/my-docker-image"
subnet_ids = "${element(split(",", module.vpc_subnets.private_subnets_id), 3)}"
security_groups = "${module.security.apache_solr_group}"
port = "8983"
cluster = "${module.ecs-cluster.name}"
log_bucket = "${module.s3_logs.id}"
iam_role = "${aws_iam_instance_profile.ecs.id}"
dns_name = ""
zone_id = "${var.route53_zone_id}"
}
Run Code Online (Sandbox Code Playgroud)
我的s3-logs存储桶看起来像这样:
module "s3_logs" {
source = "github.com/segmentio/stack/s3-logs"
name = "${var.prefix}"
environment = "${terraform.env}"
account_id = "123456789876"
}
Run Code Online (Sandbox Code Playgroud)
我检查了S3,存储桶策略如下所示:
{
"Version": "2012-10-17",
"Id": "log-bucket-policy",
"Statement": [
{
"Sid": "log-bucket-policy",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789876:root"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-service-logs/*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
据我所知,ELB应该可以访问S3存储桶来存储日志(它在同一个AWS账户中运行).
水桶和ELB都在eu-west-2
.
任何关于问题可能出现的想法都会受到高度赞赏.
yda*_*coR 25
ELB访问日志的文档说您希望允许特定的亚马逊帐户能够写入S3,而不是您的帐户.
因此你需要这样的东西:
{
"Id": "Policy1429136655940",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1429136633762",
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::my-loadbalancer-logs/my-app/AWSLogs/123456789012/*",
"Principal": {
"AWS": [
"652711504416"
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
在Terraform中,您可以使用aws_elb_service_account数据源自动获取用于编写日志的帐户ID,如文档中的示例所示:
data "aws_elb_service_account" "main" {}
resource "aws_s3_bucket" "elb_logs" {
bucket = "my-elb-tf-test-bucket"
acl = "private"
policy = <<POLICY
{
"Id": "Policy",
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::my-elb-tf-test-bucket/AWSLogs/*",
"Principal": {
"AWS": [
"${data.aws_elb_service_account.main.arn}"
]
}
}
]
}
POLICY
}
resource "aws_elb" "bar" {
name = "my-foobar-terraform-elb"
availability_zones = ["us-west-2a"]
access_logs {
bucket = "${aws_s3_bucket.elb_logs.bucket}"
interval = 5
}
listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
Run Code Online (Sandbox Code Playgroud)
在存储桶策略中,帐号必须不是您的。相反,它属于 AWS,对于每个区域,您应在存储桶策略中使用的帐号列于:https: //docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-access-logs.html #附加桶策略
例如,对于us-east-1
地区,帐号是127311923021
。
虽然问题是关于 Terraform,但我发布了 CloudFormation 片段,为 ELB 的访问日志创建了一个存储桶,记录其存储桶策略:
MyAccessLogsBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
MyAllowELBAccessBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref MyAccessLogsBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
AWS: "arn:aws:iam::127311923021:root"
Action:
- "s3:PutObject"
Resource: !Sub "arn:aws:s3:::${MyAccessLogsBucket}/AWSLogs/*"
Run Code Online (Sandbox Code Playgroud)
原则上, 127311923021
使用 是因为这是 AWS 帐号,应该用作 中的帐号us-east-1
。