Skip to content

Commit

Permalink
Merge pull request #46 from ministryofjustice/HEAT-61-test-irsa
Browse files Browse the repository at this point in the history
Add output for IAM policy arn for use in IRSA configuration
  • Loading branch information
jakemulley authored May 5, 2023
2 parents 99e7836 + c1904ad commit 747c409
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ No modules.
| Name | Type |
|------|------|
| [aws_iam_access_key.key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_access_key) | resource |
| [aws_iam_policy.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_user.user](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
| [aws_iam_user_policy.userpol](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy) | resource |
| [aws_kms_alias.alias](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_key.kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
| [aws_sqs_queue.terraform_queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
| [random_id.id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs
Expand Down Expand Up @@ -162,6 +164,7 @@ No modules.
| Name | Description |
|------|-------------|
| <a name="output_access_key_id"></a> [access\_key\_id](#output\_access\_key\_id) | Access key id for the credentials |
| <a name="output_irsa_policy_arn"></a> [irsa\_policy\_arn](#output\_irsa\_policy\_arn) | n/a |
| <a name="output_secret_access_key"></a> [secret\_access\_key](#output\_secret\_access\_key) | Secret for the new credentials |
| <a name="output_sqs_arn"></a> [sqs\_arn](#output\_sqs\_arn) | The ARN of the SQS queue. |
| <a name="output_sqs_id"></a> [sqs\_id](#output\_sqs\_id) | The URL for the created Amazon SQS queue. |
Expand Down
2 changes: 1 addition & 1 deletion example/sqs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module "example_sqs" {
# source = "github.com/ministryofjustice/cloud-platform-terraform-sqs?ref=4.9.1"
source = "../"

sqs_name = "example_sqs_name"
sqs_name = "example_sqs_name"
# if true, the sqs_name above must end with ".fifo", it's an API quirk
fifo_queue = false
team_name = var.team_name
Expand Down
42 changes: 33 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
locals {
default_tags = {
# Mandatory
business-unit = var.business-unit
application = var.application
is-production = var.is-production
owner = var.team_name
namespace = var.namespace # for billing and identification purposes
# Optional
environment-name = var.environment-name
infrastructure-support = var.infrastructure-support
}
}

data "aws_caller_identity" "current" {}

resource "random_id" "id" {
Expand Down Expand Up @@ -113,17 +127,10 @@ resource "aws_sqs_queue" "terraform_queue" {
redrive_policy = var.redrive_policy
fifo_queue = var.fifo_queue

tags = {
business-unit = var.business-unit
application = var.application
is-production = var.is-production
environment-name = var.environment-name
owner = var.team_name
infrastructure-support = var.infrastructure-support
namespace = var.namespace
}
tags = local.default_tags
}

# Legacy long-lived credentials
locals {
create_user = replace(var.existing_user_name, "cp-", "") == var.existing_user_name ? 1 : 0
}
Expand Down Expand Up @@ -157,3 +164,20 @@ data "aws_iam_policy_document" "policy" {
}
}

# Short-lived credentials (IRSA)
data "aws_iam_policy_document" "irsa" {
version = "2012-10-17"
statement {
sid = "AllowSQSActions"
effect = "Allow"
actions = ["sqs:*"]
resources = [aws_sqs_queue.terraform_queue.arn]
}
}

resource "aws_iam_policy" "irsa" {
name = "cloud-platform-sqs-${random_id.id.hex}"
path = "/cloud-platform/sqs/"
policy = data.aws_iam_policy_document.irsa.json
tags = local.default_tags
}
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ output "sqs_name" {
value = aws_sqs_queue.terraform_queue.name
}

output "irsa_policy_arn" {
value = aws_iam_policy.irsa.arn
}

0 comments on commit 747c409

Please sign in to comment.