Skip to content

Commit bc3c9ce

Browse files
zohar-mongoZohar Meir
andauthored
Working example for Atlas-encryptionAtRest-roles with a single tf apply (#415)
* Update aws-roles.tf * Update aws-roles.tf * Update aws-roles.tf * Update aws-roles.tf * two options for aws encryption at rest with iam roles * removed extra spaces and notes Co-authored-by: Zohar Meir <[email protected]>
1 parent 75ea01d commit bc3c9ce

File tree

16 files changed

+196
-11
lines changed

16 files changed

+196
-11
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
resource "mongodbatlas_cloud_provider_access" "test" {
2+
project_id = var.project_id
3+
provider_name = "AWS"
4+
5+
#(Optional) Since we update the `iam_assumed_role_arn` resource using an HTTP call and not by the `mongodbatlas_cloud_provider_access` resource argument,
6+
#the lifecycle argument was added so that terraform would ignore changes of the `iam_assumed_role_arn` argument in future terraform applies.
7+
lifecycle {
8+
ignore_changes = [
9+
iam_assumed_role_arn
10+
]
11+
}
12+
}
13+
14+
resource "aws_iam_role_policy" "test_policy" {
15+
name = "test_policy"
16+
role = aws_iam_role.test_role.id
17+
18+
policy = <<-EOF
19+
{
20+
"Version": "2012-10-17",
21+
"Statement": [
22+
{
23+
"Effect": "Allow",
24+
"Action": "*",
25+
"Resource": "*"
26+
}
27+
]
28+
}
29+
EOF
30+
}
31+
32+
resource "aws_iam_role" "test_role" {
33+
name = "test_role"
34+
35+
assume_role_policy = <<EOF
36+
{
37+
"Version": "2012-10-17",
38+
"Statement": [
39+
{
40+
"Effect": "Allow",
41+
"Principal": {
42+
"AWS": "${mongodbatlas_cloud_provider_access.test.atlas_aws_account_arn}"
43+
},
44+
"Action": "sts:AssumeRole",
45+
"Condition": {
46+
"StringEquals": {
47+
"sts:ExternalId": "${mongodbatlas_cloud_provider_access.test.atlas_assumed_role_external_id}"
48+
}
49+
}
50+
}
51+
]
52+
}
53+
EOF
54+
}
55+
56+
# The null resource updates the `mongodbatlas_cloud_provider_access` resource with the correct IAM role ARN using an API HTTP PATCH request.
57+
# sleep 10 - Waits ten seconds to make sure that all AWS servers are updated with the new IAM Role.
58+
resource "null_resource" "link_role_arn_to_cloud_provider_access" {
59+
provisioner "local-exec" {
60+
command = <<EOT
61+
sleep 10;
62+
curl --user "${var.public_key}:${var.private_key}" -X PATCH --digest \
63+
--header "Accept: application/json" \
64+
--header "Content-Type: application/json" \
65+
"https://cloud.mongodb.com/api/atlas/v1.0/groups/${var.project_id}/cloudProviderAccess/${mongodbatlas_cloud_provider_access.test.role_id}?pretty=true" \
66+
--data '{ "providerName": "AWS", "iamAssumedRoleArn" : "${aws_iam_role.test_role.arn}" }'
67+
68+
EOT
69+
}
70+
}
71+
72+
73+
output "cpa_role_id" {
74+
value = mongodbatlas_cloud_provider_access.test.role_id
75+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/atlas-encryptionAtRest-roles/versions.tf renamed to examples/atlas-encryptionAtRest-roles-one-step-workaround/versions.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ terraform {
55
}
66
mongodbatlas = {
77
source = "mongodb/mongodbatlas"
8-
//version = "0.7-dev"
98
}
109
}
1110
required_version = ">= 0.13"

examples/atlas-encryptionAtRest-roles/aws-roles.tf renamed to examples/atlas-encryptionAtRest-roles-two-step/aws-roles.tf

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
21
resource "mongodbatlas_cloud_provider_access" "test" {
3-
project_id = var.project_id
4-
provider_name = "AWS"
5-
iam_assumed_role_arn = var.aws_iam_role_arn
6-
}
2+
project_id = mongodbatlas_project.my_project.id
3+
provider_name = "AWS"
4+
#after first apply, add the following line:
5+
#iam_assumed_role_arn = aws_iam_role.test_role.arn
6+
}
77

88
resource "aws_iam_role_policy" "test_policy" {
99
name = "test_policy"
@@ -45,13 +45,9 @@ resource "aws_iam_role" "test_role" {
4545
]
4646
}
4747
EOF
48-
49-
5048
}
5149

52-
output "aws_iam_role_arn" {
53-
value = aws_iam_role.test_role.arn
54-
}
50+
5551
output "cpa_role_id" {
5652
value = mongodbatlas_cloud_provider_access.test.role_id
5753
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "mongodbatlas" {
2+
public_key = var.public_key
3+
private_key = var.private_key
4+
}
5+
provider "aws" {
6+
access_key = var.access_key
7+
secret_key = var.secret_key
8+
region = var.aws_region
9+
}

0 commit comments

Comments
 (0)