Skip to content

Commit adea5ff

Browse files
Merge pull request #21 from htc-demo-00/resource-account
feat: resource account
2 parents 3a6331e + a9d1050 commit adea5ff

File tree

6 files changed

+76
-49
lines changed

6 files changed

+76
-49
lines changed

examples/with-backstage/backstage-humanitec.tf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ resource "humanitec_resource_definition_criteria" "backstage_k8s_service_account
126126
module "backstage_iam_policy_ecr_create_repository" {
127127
source = "git::https://github.com/humanitec-architecture/resource-packs-aws.git//humanitec-resource-defs/iam-policy/ecr-create-repository"
128128

129-
access_key = module.base.aws_access_key_id
130-
secret_key = module.base.aws_secret_access_key
129+
driver_account = module.base.humanitec_resource_account_id
131130
resource_packs_aws_rev = var.resource_packs_aws_rev
132131
region = var.aws_region
133132

@@ -146,8 +145,7 @@ resource "humanitec_resource_definition_criteria" "backstage_iam_policy_ecr_crea
146145
module "backstage_iam_role_service_account" {
147146
source = "git::https://github.com/humanitec-architecture/resource-packs-aws.git//humanitec-resource-defs/iam-role/service-account"
148147

149-
access_key = module.base.aws_access_key_id
150-
secret_key = module.base.aws_secret_access_key
148+
driver_account = module.base.humanitec_resource_account_id
151149
resource_packs_aws_rev = var.resource_packs_aws_rev
152150
region = var.aws_region
153151

modules/base/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Module that provides the reference architecture.
1919
| <a name="provider_helm"></a> [helm](#provider\_helm) | n/a |
2020
| <a name="provider_humanitec"></a> [humanitec](#provider\_humanitec) | n/a |
2121
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | >= 2.0.3 |
22+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
2223

2324
## Modules
2425

@@ -32,31 +33,33 @@ Module that provides the reference architecture.
3233

3334
| Name | Type |
3435
|------|------|
35-
| [aws_iam_access_key.humanitec_svc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_access_key) | resource |
36-
| [aws_iam_user.humanitec_svc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
37-
| [aws_iam_user_policy_attachment.humanitec_svc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy_attachment) | resource |
36+
| [aws_iam_role.humanitec_svc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
37+
| [aws_iam_role_policy_attachment.humanitec_svc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
3838
| [helm_release.ingress_nginx](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
39+
| [humanitec_resource_account.cluster_account](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_account) | resource |
3940
| [humanitec_resource_definition.k8s_cluster_driver](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition) | resource |
4041
| [humanitec_resource_definition.k8s_namespace](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition) | resource |
4142
| [humanitec_resource_definition_criteria.k8s_cluster_driver](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
4243
| [humanitec_resource_definition_criteria.k8s_namespace](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
44+
| [random_password.external_id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
4345
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
4446
| [aws_elb_hosted_zone_id.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_hosted_zone_id) | data source |
47+
| [aws_iam_policy_document.instance_assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
4548
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
4649
| [kubernetes_service.ingress_nginx_controller](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service) | data source |
4750

4851
## Inputs
4952

5053
| Name | Description | Type | Default | Required |
5154
|------|-------------|------|---------|:--------:|
52-
| <a name="input_additional_aws_auth_users"></a> [additional\_aws\_auth\_users](#input\_additional\_aws\_auth\_users) | Additional users add to the k8s aws-auth configmap | `list(any)` | `[]` | no |
55+
| <a name="input_additional_k8s_access_entries"></a> [additional\_k8s\_access\_entries](#input\_additional\_k8s\_access\_entries) | Additional access entries add to the k8s aws-auth configmap | <pre>list(object({<br> id = string<br> principal_arn = string<br> groups = list(string)<br> }))</pre> | `[]` | no |
5356
| <a name="input_capacity_type"></a> [capacity\_type](#input\_capacity\_type) | Defines whether to use ON\_DEMAND or SPOT EC2 instances for EKS nodes | `string` | `"ON_DEMAND"` | no |
5457
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name for the EKS cluster | `string` | `"ref-arch"` | no |
5558
| <a name="input_cluster_version"></a> [cluster\_version](#input\_cluster\_version) | Version of the EKS cluster to deploy | `string` | `null` | no |
5659
| <a name="input_disk_size"></a> [disk\_size](#input\_disk\_size) | Disk size in GB to use for EKS nodes | `number` | `20` | no |
5760
| <a name="input_eks_public_access_cidrs"></a> [eks\_public\_access\_cidrs](#input\_eks\_public\_access\_cidrs) | List of CIDRs that can access the EKS cluster's public endpoint | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
5861
| <a name="input_environment"></a> [environment](#input\_environment) | Name of the environment to be deployed into | `string` | `"development"` | no |
59-
| <a name="input_iam_user_name"></a> [iam\_user\_name](#input\_iam\_user\_name) | Name of the IAM user to create for Humanitec EKS access | `string` | `"svc-humanitec"` | no |
62+
| <a name="input_iam_role_name"></a> [iam\_role\_name](#input\_iam\_role\_name) | Name of the IAM user to create for Humanitec EKS access | `string` | `"svc-humanitec"` | no |
6063
| <a name="input_ingress_nginx_min_unavailable"></a> [ingress\_nginx\_min\_unavailable](#input\_ingress\_nginx\_min\_unavailable) | Number of allowed unavaiable replicas for the ingress-nginx controller | `number` | `1` | no |
6164
| <a name="input_ingress_nginx_replica_count"></a> [ingress\_nginx\_replica\_count](#input\_ingress\_nginx\_replica\_count) | Number of replicas for the ingress-nginx controller | `number` | `2` | no |
6265
| <a name="input_instance_types"></a> [instance\_types](#input\_instance\_types) | List of EC2 instances types to use for EKS nodes | `list(string)` | <pre>[<br> "t3.large"<br>]</pre> | no |
@@ -70,14 +73,13 @@ Module that provides the reference architecture.
7073

7174
| Name | Description |
7275
|------|-------------|
73-
| <a name="output_aws_access_key_id"></a> [aws\_access\_key\_id](#output\_aws\_access\_key\_id) | n/a |
74-
| <a name="output_aws_secret_access_key"></a> [aws\_secret\_access\_key](#output\_aws\_secret\_access\_key) | n/a |
7576
| <a name="output_eks_cluster_certificate_authority_data"></a> [eks\_cluster\_certificate\_authority\_data](#output\_eks\_cluster\_certificate\_authority\_data) | Base64 encoded certificate data required to communicate with the cluster |
7677
| <a name="output_eks_cluster_endpoint"></a> [eks\_cluster\_endpoint](#output\_eks\_cluster\_endpoint) | Endpoint for your Kubernetes API server |
7778
| <a name="output_eks_cluster_name"></a> [eks\_cluster\_name](#output\_eks\_cluster\_name) | The name of the EKS cluster |
7879
| <a name="output_eks_oidc_provider"></a> [eks\_oidc\_provider](#output\_eks\_oidc\_provider) | The OpenID Connect identity provider (issuer URL without leading `https://`) |
7980
| <a name="output_eks_oidc_provider_arn"></a> [eks\_oidc\_provider\_arn](#output\_eks\_oidc\_provider\_arn) | The ARN of the OIDC Provider |
8081
| <a name="output_environment"></a> [environment](#output\_environment) | Name of the environment to be deployed into |
82+
| <a name="output_humanitec_resource_account_id"></a> [humanitec\_resource\_account\_id](#output\_humanitec\_resource\_account\_id) | Humanitec resource account id for the cluster |
8183
| <a name="output_ingress_nginx_external_dns"></a> [ingress\_nginx\_external\_dns](#output\_ingress\_nginx\_external\_dns) | External DNS entry for the Nginx ingress controller |
8284
| <a name="output_vpc_id"></a> [vpc\_id](#output\_vpc\_id) | VPC id |
8385
<!-- END_TF_DOCS -->

modules/base/humanitec.tf

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,32 @@ locals {
66

77
data "aws_elb_hosted_zone_id" "main" {}
88

9+
resource "humanitec_resource_account" "cluster_account" {
10+
id = var.cluster_name
11+
name = var.cluster_name
12+
type = "aws-role"
13+
14+
credentials = jsonencode({
15+
aws_role = aws_iam_role.humanitec_svc.arn
16+
external_id = random_password.external_id.result
17+
})
18+
19+
depends_on = [aws_iam_role_policy_attachment.humanitec_svc]
20+
}
21+
922
resource "humanitec_resource_definition" "k8s_cluster_driver" {
1023
driver_type = "humanitec/k8s-cluster-eks"
1124
id = var.cluster_name
1225
name = var.cluster_name
1326
type = "k8s-cluster"
1427

28+
driver_account = humanitec_resource_account.cluster_account.id
1529
driver_inputs = {
1630
values_string = jsonencode({
1731
"name" = module.aws_eks.cluster_name
1832
"loadbalancer" = local.ingress_address
1933
"loadbalancer_hosted_zone" = data.aws_elb_hosted_zone_id.main.id
2034
"region" = var.region
21-
}),
22-
secrets_string = jsonencode({
23-
"credentials" = {
24-
"aws_access_key_id" = aws_iam_access_key.humanitec_svc.id
25-
"aws_secret_access_key" = aws_iam_access_key.humanitec_svc.secret
26-
}
2735
})
2836
}
2937
}

modules/base/main.tf

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11
locals {
2-
admin_policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
2+
admin_policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
3+
humanitec_user_arn = "arn:aws:iam::767398028804:user/humanitec"
34
tags = {
45
Terraform = "true"
56
Environment = var.environment
67
}
78
}
89

9-
# User for Humanitec to access the EKS cluster
10-
11-
resource "aws_iam_user" "humanitec_svc" {
12-
name = var.iam_user_name
10+
resource "random_password" "external_id" {
11+
length = 16
12+
special = false
1313
}
1414

15-
resource "aws_iam_user_policy_attachment" "humanitec_svc" {
16-
user = aws_iam_user.humanitec_svc.name
17-
policy_arn = local.admin_policy_arn
15+
data "aws_iam_policy_document" "instance_assume_role_policy" {
16+
statement {
17+
actions = ["sts:AssumeRole"]
18+
19+
principals {
20+
type = "AWS"
21+
identifiers = [local.humanitec_user_arn]
22+
}
23+
24+
condition {
25+
test = "StringEquals"
26+
variable = "sts:ExternalId"
27+
values = [random_password.external_id.result]
28+
}
29+
}
1830
}
1931

20-
resource "aws_iam_access_key" "humanitec_svc" {
21-
user = aws_iam_user.humanitec_svc.name
2232

23-
# Ensure that the policy is not deleted before the access key
24-
depends_on = [aws_iam_user_policy_attachment.humanitec_svc]
33+
# User for Humanitec to access the EKS cluster
34+
resource "aws_iam_role" "humanitec_svc" {
35+
name = var.iam_role_name
36+
37+
assume_role_policy = data.aws_iam_policy_document.instance_assume_role_policy.json
38+
}
39+
40+
resource "aws_iam_role_policy_attachment" "humanitec_svc" {
41+
role = aws_iam_role.humanitec_svc.name
42+
policy_arn = local.admin_policy_arn
2543
}
2644

2745
# VPC and EKS cluster
@@ -47,14 +65,14 @@ module "aws_vpc" {
4765
data "aws_caller_identity" "current" {}
4866

4967
locals {
50-
default_aws_auth_users = [
68+
default_k8s_access_entries = [
5169
{
52-
userarn = aws_iam_user.humanitec_svc.arn
53-
username = aws_iam_user.humanitec_svc.name
54-
groups = ["system:masters"]
70+
id = aws_iam_role.humanitec_svc.name
71+
principal_arn = aws_iam_role.humanitec_svc.arn
72+
groups = ["system:masters"]
5573
}
5674
]
57-
aws_auth_users = concat(local.default_aws_auth_users, var.additional_aws_auth_users)
75+
k8s_access_entries = concat(local.default_k8s_access_entries, var.additional_k8s_access_entries)
5876
}
5977

6078
module "ebs_csi_irsa_role" {
@@ -114,9 +132,9 @@ module "aws_eks" {
114132
enable_cluster_creator_admin_permissions = true
115133

116134
access_entries = {
117-
for s in local.aws_auth_users : s.username => {
135+
for s in local.k8s_access_entries : s.id => {
118136
kubernetes_groups = []
119-
principal_arn = s.userarn
137+
principal_arn = s.principal_arn
120138

121139
policy_associations = {
122140
cluster_admin = {

modules/base/outputs.tf

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ output "ingress_nginx_external_dns" {
4747
}
4848

4949

50-
# Key
50+
# Humanitec
5151

52-
output "aws_access_key_id" {
53-
value = aws_iam_access_key.humanitec_svc.id
54-
}
55-
56-
output "aws_secret_access_key" {
57-
value = aws_iam_access_key.humanitec_svc.secret
52+
output "humanitec_resource_account_id" {
53+
description = "Humanitec resource account id for the cluster"
54+
value = humanitec_resource_account.cluster_account.id
5855
}

modules/base/variables.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ variable "capacity_type" {
6666
default = "ON_DEMAND"
6767
}
6868

69-
variable "iam_user_name" {
69+
variable "iam_role_name" {
7070
description = "Name of the IAM user to create for Humanitec EKS access"
7171
type = string
7272
default = "svc-humanitec"
7373
}
7474

75-
variable "additional_aws_auth_users" {
76-
description = "Additional users add to the k8s aws-auth configmap"
77-
type = list(any)
78-
default = []
75+
variable "additional_k8s_access_entries" {
76+
description = "Additional access entries add to the k8s aws-auth configmap"
77+
type = list(object({
78+
id = string
79+
principal_arn = string
80+
groups = list(string)
81+
}))
82+
default = []
7983
}
8084

8185
variable "ingress_nginx_replica_count" {
@@ -94,4 +98,4 @@ variable "disk_size" {
9498
description = "Disk size in GB to use for EKS nodes"
9599
type = number
96100
default = 20
97-
}
101+
}

0 commit comments

Comments
 (0)