Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working example for Atlas-encryptionAtRest-roles with a single tf apply #415

Merged
merged 6 commits into from
Mar 3, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions examples/atlas-encryptionAtRest-roles/aws-roles.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
resource "mongodbatlas_cloud_provider_access" "test" {
project_id = var.project_id
provider_name = "AWS"
iam_assumed_role_arn = var.aws_iam_role_arn

#(Optional) Since we update the `iam_assumed_role_arn` resource using an HTTP call and not by the `mongodbatlas_cloud_provider_access` resource argument,
#the lifecycle argument was added so that terraform would ignore changes of the `iam_assumed_role_arn` argument in future terraform applies.
lifecycle {
ignore_changes = [
iam_assumed_role_arn
]
}
}

resource "aws_iam_role_policy" "test_policy" {
Expand Down Expand Up @@ -45,13 +52,25 @@ resource "aws_iam_role" "test_role" {
]
}
EOF
}

# The null resource updates the `mongodbatlas_cloud_provider_access` resource with the correct IAM role ARN using an API HTTP PATCH request.
# sleep 10 - Waits ten seconds to make sure that all AWS servers are updated with the new IAM Role.
resource "null_resource" "link_role_arn_to_cloud_provider_access" {
provisioner "local-exec" {
command = <<EOT
sleep 10;
curl --user "${var.public_key}:${var.private_key}" -X PATCH --digest \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
"https://cloud.mongodb.com/api/atlas/v1.0/groups/${var.project_id}/cloudProviderAccess/${mongodbatlas_cloud_provider_access.test.role_id}?pretty=true" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty cool !

--data '{ "providerName": "AWS", "iamAssumedRoleArn" : "${aws_iam_role.test_role.arn}" }'

EOT
}
}

output "aws_iam_role_arn" {
value = aws_iam_role.test_role.arn
}

output "cpa_role_id" {
value = mongodbatlas_cloud_provider_access.test.role_id
}