Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple policies to be attached to an s3 bucket #409

Closed
hashibot opened this issue Jun 13, 2017 · 8 comments
Closed

Allow multiple policies to be attached to an s3 bucket #409

hashibot opened this issue Jun 13, 2017 · 8 comments
Assignees
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/s3 Issues and PRs that pertain to the s3 service.
Milestone

Comments

@hashibot
Copy link

This issue was originally opened by @evanstachowiak as hashicorp/terraform#10543. It was migrated here as part of the provider split. The original body of the issue is below.


Hi there,

Terraform Version

0.7.13

Affected Resource(s)

  • aws_s3_bucket_policy

Expected Behavior

Instead of using a different bucket for different logs, I'm trying to use one bucket with different prefixes. The issue with this is that I cannot use more than one aws_s3_bucket_policy. If I specify this resource multiple times, the previous gets overwritten and only the last one is used. Ideally, there would be something like aws_iam_policy_attachment resource.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Try to use multiple s3 policies on a bucket.
@hashibot hashibot added the bug Addresses a defect in current functionality. label Jun 13, 2017
@mblakele
Copy link
Contributor

mblakele commented Dec 1, 2017

A related suggestion: provide a way to merge a list of data.aws_iam_policy_document into a single policy. The existing resource.aws_s3_bucket_policy could support a list of policies, and automatically merge them into a single policy.

This would allow modularization of the data.aws_iam_policy_document for specific purposes. As of tf 0.11, lack of something like this can result in a lot of repetition.

@radeksimko radeksimko added the service/s3 Issues and PRs that pertain to the s3 service. label Jan 25, 2018
@JonRoma
Copy link

JonRoma commented Mar 27, 2018

We want to be able to dynamically add policy statements to an S3 bucket policy.

We first tried having the count on the data object. This strategy generated N self-contained policy documents. In order to be useful, the N policy statements should really be concatenated into a single policy document that can be interpolated into the bucket_policy.

data "aws_iam_policy_document" "default" {
 count  = "${length(keys(var.statement))}"

  statement {
    sid       = "CloudfrontBucketActions"
    actions   = ["s3:GetObject"]
    resources = ["${aws_s3_bucket.default.arn}/${element(values(var.origin-access-identities), count.index)}/*"]
    principals {
      type        = "AWS"
      identifiers = ["${element(keys(var.statement), count.index)}"]
    }
  }

Next, we tried putting a count parameter on the statement block in the hope that Terraform would create N instances of that statement, each with the proper values.

data "aws_iam_policy_document" "default" {

  statement {
    count  = "${length(keys(var.statement))}"
    sid       = "CloudfrontBucketActions"
    actions   = ["s3:GetObject"]
    resources = ["${aws_s3_bucket.default.arn}/${element(values(var.origin-access-identities), count.index)}/*"]
    principals {
      type        = "AWS"
      identifiers = ["${element(keys(var.statement), count.index)}"]
    }
  }

This failed with the error invalid or unknown key: count

@bflad
Copy link
Contributor

bflad commented Mar 28, 2018

In #2890, which was released in v1.9.0 of the AWS provider, we added "layering" functionality to the aws_iam_policy_document data source via the source_json and override_json attributes. It should support functionality similar to what is mentioned in #409 (comment) (among other use cases) where you can merge these data sources together.

@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. and removed bug Addresses a defect in current functionality. labels Mar 28, 2018
@bflad
Copy link
Contributor

bflad commented Mar 28, 2018

For the count use case mentioned above in #409 (comment), we have some upcoming improvements to the Terraform configuration, which should allow better handling of looping through "sub" resources: hashicorp/terraform#17179 and hashicorp/terraform#7034

@bflad
Copy link
Contributor

bflad commented Jul 9, 2019

Hi folks 👋 There are a few solutions to this problem nowadays:

  • As mentioned above, the aws_iam_policy_document data source supports "layering" via the source_json and override_json arguments
  • Terraform 0.12 supports the new dynamic block, which can be used to dynamically build statement configuration blocks

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@bflad bflad closed this as completed Jul 9, 2019
@ghost
Copy link

ghost commented Nov 2, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 2, 2019
@YakDriver
Copy link
Member

YakDriver commented Feb 10, 2021

We have merged #12055 in to the Terraform AWS Provider. With this, aws_iam_policy_document provides the ability to merge multiple source and override policy documents. This is available now on the main branch and when version 3.28.0 is released (likely Feb. 11, 2021). If you have problems with the functionality or need further enhancements, please open a new issue. Thanks for your interest in the AWS Provider! 🎉

@YakDriver YakDriver self-assigned this Feb 10, 2021
@YakDriver YakDriver added this to the v3.28.0 milestone Feb 10, 2021
@ghost
Copy link

ghost commented Feb 12, 2021

This has been released in version 3.28.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost ghost unlocked this conversation Feb 12, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Feb 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/s3 Issues and PRs that pertain to the s3 service.
Projects
None yet
Development

No branches or pull requests

6 participants