I am trying to deploy some terraform code into an AWS environment that I have admin access setup for. The purpose of this code is to send logs from an application load balancer into an S3 bucket. The code is able to create the bucket with no issue, but when it comes to logging part of it I am being presented with the following error:
I am having trouble how to get around this error. Below is my code for the creation of the load balancer, the S3 bucket, and also the policy I have implemented for the logging to take place. Any advice would be helpful. Thanks in advance.
S3 Bucket
data "aws_elb_service_account" "javahome" {}
resource "aws_s3_bucket" "alb_access_logs" {
bucket = var.alb_s3_logs
acl = "private"
region = var.region
tags = {
Name = "jalb-access-logs"
Environment = terraform.workspace
}
policy = templatefile("${path.module}/scripts/iam/alb-s3-access-logs.json", {
bucket_name = var.alb_s3_logs
prefix = var.prefix
policy_arn = data.aws_elb_service_account.javahome.arn
}
)
}
Application Load Balancer
resource "aws_lb" "javahome" {
name = var.alb_name
internal = false
load_balancer_type = var.lb_type
security_groups = [aws_security_group.elb_sg.id]
subnets = local.pub_sub_ids
access_logs {
bucket = aws_s3_bucket.alb_access_logs.bucket
prefix = var.prefix
enabled = true
}
tags = {
Environment = terraform.workspace
}
}
Policy
{
"Version": "2012-10-17",
"Id": "javahome-alb-pilicy",
"Statement": [
{
"Sid": "root-access",
"Effect": "Allow",
"Principal": {
"AWS": "${policy_arn}"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::${bucket_name}/${prefix}/AWSLogs/*"
},
{
"Sid": "log-delivery",
"Effect": "Allow",
"Principal": {
"Service": "delivery.logs.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::${bucket_name}/${prefix}/AWSLogs/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
},
{
"Sid": "log-delivery-access-check",
"Effect": "Allow",
"Principal": {
"Service": "delivery.logs.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::${bucket_name}"
}
]
}