|
| 1 | +# Attention |
| 2 | +# this file should only run for the initial project setup |
| 3 | +# this will setup: |
| 4 | +# a s3 bucket for the tfstate |
| 5 | +# a iam user to manage the rest from github |
| 6 | + |
| 7 | +provider "aws" { |
| 8 | + region = "eu-central-1" |
| 9 | + profile = "" |
| 10 | + |
| 11 | + default_tags { |
| 12 | + tags = { |
| 13 | + setup = "automation" |
| 14 | + service = "kaas" |
| 15 | + Owner = "ops" |
| 16 | + Name = "ops-k8s-bootstrap" |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +data "aws_caller_identity" "current" {} |
| 22 | + |
| 23 | +resource "aws_iam_user" "ops_github_kaas" { |
| 24 | + name = "ops-github-kaas" |
| 25 | + path = "/automation/" |
| 26 | +} |
| 27 | + |
| 28 | +resource "aws_iam_access_key" "github" { |
| 29 | + user = aws_iam_user.ops_github_kaas.name |
| 30 | +} |
| 31 | + |
| 32 | +resource "aws_s3_bucket" "tfstate" { |
| 33 | + bucket = "ops-kaas-tfstate" |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_s3_bucket_acl" "tfstate" { |
| 37 | + bucket = aws_s3_bucket.tfstate.id |
| 38 | + acl = "private" |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_s3_bucket_versioning" "tfstate" { |
| 42 | + bucket = aws_s3_bucket.tfstate.id |
| 43 | + versioning_configuration { |
| 44 | + status = "Enabled" |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +resource "aws_s3_bucket_server_side_encryption_configuration" "default" { |
| 49 | + bucket = aws_s3_bucket.tfstate.id |
| 50 | + |
| 51 | + rule { |
| 52 | + apply_server_side_encryption_by_default { |
| 53 | + sse_algorithm = "AES256" |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +resource "aws_s3_bucket_public_access_block" "public_access" { |
| 59 | + bucket = aws_s3_bucket.tfstate.id |
| 60 | + block_public_acls = true |
| 61 | + block_public_policy = true |
| 62 | + ignore_public_acls = true |
| 63 | + restrict_public_buckets = true |
| 64 | +} |
| 65 | + |
| 66 | +resource "aws_s3_bucket_policy" "allow_github_user" { |
| 67 | + bucket = aws_s3_bucket.tfstate.id |
| 68 | + policy = <<JSON |
| 69 | +{ |
| 70 | + "Id": "Policy1666800308880", |
| 71 | + "Version": "2012-10-17", |
| 72 | + "Statement": [ |
| 73 | + { |
| 74 | + "Sid": "Stmt1666800306128", |
| 75 | + "Action": [ |
| 76 | + "s3:GetObject", |
| 77 | + "s3:ListBucket", |
| 78 | + "s3:PutBucketVersioning", |
| 79 | + "s3:PutEncryptionConfiguration", |
| 80 | + "s3:PutObject", |
| 81 | + "s3:GetBucketVersioning", |
| 82 | + "s3:GetEncryptionConfiguration", |
| 83 | + "s3:GetBucketPublicAccessBlock", |
| 84 | + "s3:PutBucketPublicAccessBlock" |
| 85 | + ], |
| 86 | + "Effect": "Allow", |
| 87 | + "Resource": [ |
| 88 | + "arn:aws:s3:::ops-kaas-tfstate", |
| 89 | + "arn:aws:s3:::ops-kaas-tfstate/*" |
| 90 | + ], |
| 91 | + "Principal": { |
| 92 | + "AWS": [ |
| 93 | + "arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/automation/${aws_iam_user.ops_github_kaas.name}" |
| 94 | + ] |
| 95 | + } |
| 96 | + } |
| 97 | + ] |
| 98 | +} |
| 99 | +JSON |
| 100 | +} |
| 101 | + |
| 102 | +output "github_access_key" { |
| 103 | + value = aws_iam_access_key.github.id |
| 104 | +} |
| 105 | +output "github_secret_key" { |
| 106 | + value = aws_iam_access_key.github.secret |
| 107 | + sensitive = true |
| 108 | +} |
0 commit comments