-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1247 from alphagov/samsimpson1/big-import
Import more S3 resources from govuk-aws
- Loading branch information
Showing
6 changed files
with
304 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
terraform/deployments/govuk-publishing-infrastructure/athena_query_results_s3.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
resource "aws_s3_bucket" "athena_query_results" { | ||
bucket = "govuk-${var.govuk_environment}-athena-query-results" | ||
} | ||
|
||
resource "aws_s3_bucket_lifecycle_configuration" "athena_query_results" { | ||
bucket = aws_s3_bucket.athena_query_results.id | ||
|
||
rule { | ||
id = "govuk-${var.govuk_environment}-csp-reports-lifecycle" | ||
status = "Enabled" | ||
|
||
expiration { | ||
days = 7 | ||
} | ||
} | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket.athena_query_results | ||
id = "govuk-${var.govuk_environment}-athena-query-results" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_lifecycle_configuration.athena_query_results | ||
id = "govuk-${var.govuk_environment}-athena-query-results" | ||
} |
47 changes: 47 additions & 0 deletions
47
terraform/deployments/govuk-publishing-infrastructure/content_data_s3.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
resource "aws_s3_bucket" "content_data_csvs" { | ||
bucket = "govuk-${var.govuk_environment}-content-data-csvs" | ||
} | ||
|
||
resource "aws_s3_bucket_acl" "content_data_csvs" { | ||
bucket = aws_s3_bucket.content_data_csvs.id | ||
acl = "public-read" | ||
} | ||
|
||
resource "aws_s3_bucket_logging" "content_data_csvs" { | ||
bucket = aws_s3_bucket.content_data_csvs.id | ||
target_bucket = "govuk-${var.govuk_environment}-aws-logging" | ||
target_prefix = "s3/govuk-${var.govuk_environment}-content-data-csvs/" | ||
} | ||
|
||
resource "aws_s3_bucket_lifecycle_configuration" "content_data_csvs" { | ||
bucket = aws_s3_bucket.content_data_csvs.id | ||
|
||
rule { | ||
id = "all" | ||
status = "Enabled" | ||
|
||
expiration { | ||
days = 7 | ||
} | ||
} | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_lifecycle_configuration.content_data_csvs | ||
id = "govuk-${var.govuk_environment}-content-data-csvs" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket.content_data_csvs | ||
id = "govuk-${var.govuk_environment}-content-data-csvs" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_logging.content_data_csvs | ||
id = "govuk-${var.govuk_environment}-content-data-csvs" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_acl.content_data_csvs | ||
id = "govuk-${var.govuk_environment}-content-data-csvs" | ||
} |
105 changes: 105 additions & 0 deletions
105
terraform/deployments/govuk-publishing-infrastructure/content_publisher_activestorage_iam.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Replication IAM role/policy | ||
|
||
data "aws_iam_policy_document" "content_publisher_activestorage_replication_role" { | ||
statement { | ||
actions = ["sts:AssumeRole"] | ||
principals { | ||
type = "Service" | ||
identifiers = ["s3.amazonaws.com"] | ||
} | ||
effect = "Allow" | ||
} | ||
} | ||
|
||
resource "aws_iam_role" "content_publisher_activestorage_replication_role" { | ||
name = "govuk-content-publisher-activestorage-replication-role" | ||
assume_role_policy = data.aws_iam_policy_document.content_publisher_activestorage_replication_role.json | ||
} | ||
|
||
data "aws_iam_policy_document" "content_publisher_activestorage_replication_policy" { | ||
statement { | ||
actions = [ | ||
"s3:GetReplicationConfiguration", | ||
"s3:ListBucket" | ||
] | ||
resources = [aws_s3_bucket.content_publisher_activestorage.arn] | ||
effect = "Allow" | ||
} | ||
statement { | ||
actions = [ | ||
"s3:GetObjectVersion", | ||
"s3:GetObjectVersionAcl", | ||
"s3:GetObjectVersionTagging" | ||
] | ||
resources = ["${aws_s3_bucket.content_publisher_activestorage.arn}/*"] | ||
effect = "Allow" | ||
} | ||
statement { | ||
actions = [ | ||
"s3:ReplicateObject", | ||
"s3:ReplicateDelete" | ||
] | ||
resources = ["${aws_s3_bucket.content_publisher_activestorage_replica.arn}/*"] | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "content_publisher_activestorage_replication_policy" { | ||
name = "govuk-${var.govuk_environment}-content-publisher-activestorage-replication-policy" | ||
policy = data.aws_iam_policy_document.content_publisher_activestorage_replication_policy.json | ||
description = "Allows replication of the content publisher activestorage bucket" | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "content_publisher_activestorage_replication_policy" { | ||
role = aws_iam_role.content_publisher_activestorage_replication_role.name | ||
policy_arn = aws_iam_policy.content_publisher_activestorage_replication_policy.arn | ||
} | ||
|
||
# Imports (temporary) | ||
|
||
import { | ||
to = aws_iam_role.content_publisher_activestorage_replication_role | ||
id = "govuk-content-publisher-activestorage-replication-role" | ||
} | ||
|
||
import { | ||
to = aws_iam_policy.content_publisher_activestorage_replication_policy | ||
id = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/govuk-${var.govuk_environment}-content-publisher-activestorage-replication-policy" | ||
} | ||
|
||
import { | ||
to = aws_iam_role_policy_attachment.content_publisher_activestorage_replication_policy | ||
id = "govuk-content-publisher-activestorage-replication-role/arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/govuk-${var.govuk_environment}-content-publisher-activestorage-replication-policy" | ||
} | ||
|
||
# App access role/policy | ||
|
||
data "aws_iam_policy_document" "content_publisher_s3" { | ||
statement { | ||
actions = ["s3:GetBucketLocation", "s3:ListBucket"] | ||
resources = [aws_s3_bucket.content_publisher_activestorage.arn] | ||
} | ||
|
||
statement { | ||
actions = [ | ||
"s3:*MultipartUpload*", | ||
"s3:*Object", | ||
"s3:*ObjectAcl", | ||
"s3:*ObjectVersion", | ||
"s3:GetObject*Attributes" | ||
] | ||
resources = ["${aws_s3_bucket.content_publisher_activestorage.arn}/*"] | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "content_publisher_s3" { | ||
name = "content_publisher_s3" | ||
description = "Read and write to this environment's content-publisher-activestorage bucket." | ||
|
||
policy = data.aws_iam_policy_document.content_publisher_s3.json | ||
} | ||
|
||
# TODO: consider IRSA (pod identity) rather than granting to nodes. | ||
resource "aws_iam_role_policy_attachment" "content_publisher_s3" { | ||
role = data.tfe_outputs.cluster_infrastructure.nonsensitive_values.worker_iam_role_name | ||
policy_arn = aws_iam_policy.content_publisher_s3.arn | ||
} |
109 changes: 88 additions & 21 deletions
109
terraform/deployments/govuk-publishing-infrastructure/content_publisher_s3.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,97 @@ | ||
data "aws_iam_policy_document" "content_publisher_s3" { | ||
statement { | ||
actions = ["s3:GetBucketLocation", "s3:ListBucket"] | ||
resources = [data.terraform_remote_state.infra_content_publisher.outputs.activestorage_s3_bucket_arn] | ||
resource "aws_s3_bucket" "content_publisher_activestorage" { | ||
bucket = "govuk-${var.govuk_environment}-content-publisher-activestorage" | ||
} | ||
|
||
resource "aws_s3_bucket_replication_configuration" "content_publisher_activestorage" { | ||
bucket = aws_s3_bucket.content_publisher_activestorage.id | ||
role = aws_iam_role.content_publisher_activestorage_replication_role.arn | ||
|
||
rule { | ||
id = "govuk-content-publisher-activestorage-replication-whole-bucket-rule" | ||
# Enabled in all envs except integration | ||
status = var.govuk_environment == "integration" ? "Disabled" : "Enabled" | ||
|
||
destination { | ||
bucket = aws_s3_bucket.content_publisher_activestorage_replica.arn | ||
storage_class = "STANDARD" | ||
} | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_logging" "content_publisher_activestorage" { | ||
bucket = aws_s3_bucket.content_publisher_activestorage.id | ||
target_bucket = "govuk-${var.govuk_environment}-aws-logging" | ||
target_prefix = "s3/govuk-${var.govuk_environment}-content-publisher-activestorage/" | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "content_publisher_activestorage" { | ||
bucket = aws_s3_bucket.content_publisher_activestorage.id | ||
versioning_configuration { status = "Enabled" } | ||
} | ||
|
||
resource "aws_s3_bucket" "content_publisher_activestorage_replica" { | ||
bucket = "govuk-${var.govuk_environment}-content-publisher-activestorage-replica" | ||
provider = aws.replica | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "content_publisher_activestorage_replica" { | ||
bucket = aws_s3_bucket.content_publisher_activestorage_replica.id | ||
provider = aws.replica | ||
versioning_configuration { status = "Enabled" } | ||
} | ||
|
||
statement { | ||
actions = [ | ||
"s3:*MultipartUpload*", | ||
"s3:*Object", | ||
"s3:*ObjectAcl", | ||
"s3:*ObjectVersion", | ||
"s3:GetObject*Attributes" | ||
] | ||
resources = ["${data.terraform_remote_state.infra_content_publisher.outputs.activestorage_s3_bucket_arn}/*"] | ||
resource "aws_s3_bucket_lifecycle_configuration" "content_publisher_activestorage_replica" { | ||
bucket = aws_s3_bucket.content_publisher_activestorage_replica.id | ||
provider = aws.replica | ||
|
||
rule { | ||
id = "whole_bucket_lifecycle_rule_integration" | ||
# Only enable in integration | ||
status = var.govuk_environment == "integration" ? "Enabled" : "Disabled" | ||
|
||
expiration { | ||
days = 7 | ||
} | ||
|
||
noncurrent_version_expiration { | ||
noncurrent_days = 1 | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "content_publisher_s3" { | ||
name = "content_publisher_s3" | ||
description = "Read and write to this environment's content-publisher-activestorage bucket." | ||
# Imports (temporary) | ||
|
||
import { | ||
to = aws_s3_bucket.content_publisher_activestorage | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket.content_publisher_activestorage_replica | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage-replica" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_versioning.content_publisher_activestorage_replica | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage-replica" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_lifecycle_configuration.content_publisher_activestorage_replica | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage-replica" | ||
} | ||
|
||
import { | ||
to = aws_s3_bucket_versioning.content_publisher_activestorage | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage" | ||
} | ||
|
||
policy = data.aws_iam_policy_document.content_publisher_s3.json | ||
import { | ||
to = aws_s3_bucket_logging.content_publisher_activestorage | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage" | ||
} | ||
|
||
# TODO: consider IRSA (pod identity) rather than granting to nodes. | ||
resource "aws_iam_role_policy_attachment" "content_publisher_s3" { | ||
role = data.tfe_outputs.cluster_infrastructure.nonsensitive_values.worker_iam_role_name | ||
policy_arn = aws_iam_policy.content_publisher_s3.arn | ||
import { | ||
to = aws_s3_bucket_replication_configuration.content_publisher_activestorage | ||
id = "govuk-${var.govuk_environment}-content-publisher-activestorage" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters