Skip to content

Commit fdef1c0

Browse files
authored
Merge pull request #8 from chris-qa-org/add-s3-frontend-iam-user
Create S3 frontend IAM user
2 parents 996b93a + 1f4f742 commit fdef1c0

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Terraform module to host a static site generated by Publii
2323

2424
| Name | Type |
2525
|------|------|
26+
| [aws_iam_policy.publii_s3_frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
27+
| [aws_iam_user.publii_s3_frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
28+
| [aws_iam_user_policy_attachment.publii_s3_frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy_attachment) | resource |
2629
| [aws_kms_key.s3_bucket_frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
2730
| [aws_kms_key.s3_bucket_frontend_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
2831
| [aws_s3_bucket.frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
@@ -44,6 +47,7 @@ Terraform module to host a static site generated by Publii
4447
| [template_file.frontend_bucket_policy](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
4548
| [template_file.frontend_logging_bucket_enforce_tls_statement](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
4649
| [template_file.frontend_logging_bucket_policy](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
50+
| [template_file.publii_s3_frontend_policy](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
4751

4852
## Inputs
4953

@@ -56,6 +60,7 @@ Terraform module to host a static site generated by Publii
5660

5761
| Name | Description |
5862
|------|-------------|
63+
| <a name="output_iam_user_publii_s3_frontend"></a> [iam\_user\_publii\_s3\_frontend](#output\_iam\_user\_publii\_s3\_frontend) | IAM User attributes for Publii S3 bucket |
5964
| <a name="output_project_name"></a> [project\_name](#output\_project\_name) | Project name. Generated from the site\_url and project\_random\_id |
6065
| <a name="output_project_random_id"></a> [project\_random\_id](#output\_project\_random\_id) | The random ID generated to ensure unique resource names |
6166
| <a name="output_s3_bucket_frontend"></a> [s3\_bucket\_frontend](#output\_s3\_bucket\_frontend) | S3 bucket frontend attributes |

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ output "s3_bucket_frontend" {
1212
description = "S3 bucket frontend attributes"
1313
value = aws_s3_bucket.frontend
1414
}
15+
16+
output "iam_user_publii_s3_frontend" {
17+
description = "IAM User attributes for Publii S3 bucket"
18+
value = aws_iam_user.publii_s3_frontend
19+
}

policies/s3-rw.json.tpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Action": [
6+
"s3:GetObject",
7+
"s3:PutObject",
8+
"s3:List*",
9+
"s3:GetObjectVersion",
10+
"s3:GetBucketVersioning"
11+
],
12+
"Effect": "Allow",
13+
"Resource": [
14+
"${bucket_arn}",
15+
"${bucket_arn}/*"
16+
]
17+
},
18+
{
19+
"Action": [
20+
"kms:GenerateDataKey"
21+
],
22+
"Effect": "Allow",
23+
"Resource": [
24+
"${kms_key_arn}"
25+
]
26+
}
27+
]
28+
}

s3-frontend.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@ resource "aws_s3_bucket" "frontend" {
33
force_destroy = false
44
}
55

6+
resource "aws_iam_user" "publii_s3_frontend" {
7+
name = "publii-s3-${local.project_name}"
8+
}
9+
10+
data "template_file" "publii_s3_frontend_policy" {
11+
template = file("${path.module}/policies/s3-rw.json.tpl")
12+
13+
vars = {
14+
bucket_arn = aws_s3_bucket.frontend.arn
15+
kms_key_arn = aws_kms_key.s3_bucket_frontend.arn
16+
}
17+
}
18+
19+
resource "aws_iam_policy" "publii_s3_frontend" {
20+
name = "publii-s3-frontend-${local.project_name}"
21+
policy = data.template_file.publii_s3_frontend_policy.rendered
22+
}
23+
24+
resource "aws_iam_user_policy_attachment" "publii_s3_frontend" {
25+
user = aws_iam_user.publii_s3_frontend.name
26+
policy_arn = aws_iam_policy.publii_s3_frontend.arn
27+
}
28+
629
resource "aws_s3_bucket_versioning" "frontend" {
730
bucket = aws_s3_bucket.frontend.id
831
versioning_configuration {

0 commit comments

Comments
 (0)