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

dms-vpc-role not created before aws_dms_replication_instance #7748

Closed
ghost opened this issue Feb 27, 2019 · 6 comments · Fixed by #9173
Closed

dms-vpc-role not created before aws_dms_replication_instance #7748

ghost opened this issue Feb 27, 2019 · 6 comments · Fixed by #9173
Assignees
Labels
documentation Introduces or discusses updates to documentation.
Milestone

Comments

@ghost
Copy link

ghost commented Feb 27, 2019

This issue was originally opened by @neovasili as hashicorp/terraform#20346. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

Terraform v0.11.11

Terraform Configuration Files

resource "aws_dms_replication_instance" "dynamodb-import-instance" {
	engine_version 			= "3.1.2"
	multi_az 			= "false"
	publicly_accessible 		= "false"
	replication_instance_class 	= "${ var.dms_replication_instance_type }"
	replication_instance_id 	= "${ var.replication_instance_id }"

	tags {
		description = "test"
	}
}

resource "aws_iam_role" "dms-vpc-role" {
  name = "dms-vpc-role"

  assume_role_policy = "${ data.aws_iam_policy_document.dms-vpc-role-policy.json }"
}

data "aws_iam_policy_document" "dms-vpc-role-policy" {
  statement {
    actions = [ "sts:AssumeRole" ]

    principals {
      type        = "Service"
      identifiers = [ "dms.amazonaws.com" ]
    }
  }
}

Crash Output

Error: Error applying plan:

1 error(s) occurred:

  • aws_dms_replication_instance.dynamodb-import-instance: 1 error(s) occurred:

  • aws_dms_replication_instance.dynamodb-import-instance: error creating DMS Replication Instance: AccessDeniedFault: The IAM Role arn:aws:iam::xxxxxxxx:role/dms-vpc-role is not configured properly.
    status code: 400, request id: xxxxxxxxx

Expected Behavior

Apply complete! Resources: X added, 0 changed, 0 destroyed.

Actual Behavior

Fails to apply because iam role is created after dms replication instance

Steps to Reproduce

terraform apply

Additional context

If you perform a secondary terraform apply all changes are applied perfectly

@phillycheeze
Copy link

I get the same error when trying to create a replication instance with an additional aws_dms_replication_subnet_group.

Error:

module.datawarehouse.aws_dms_replication_subnet_group.dms: Creating...

Error: AccessDeniedFault: The IAM Role arn:aws:iam::123456789:role/dms-vpc-role is not configured properly.
	status code: 400, request id: 8cfa6491-8762-40a3-a1bb-31cc63a5a0f3

  on modules/datawarehouse/dms.tf line 4, in resource "aws_dms_replication_subnet_group" "dms":
   4: resource "aws_dms_replication_subnet_group" "dms" {

@phillycheeze
Copy link

So the resource assumes that a Role already exists to create the resources. This isn't the case if you have never created a DMS resource in your account before.

A quick way to solve this is to go into your AWS console and create a temp DMS replication instance. Once it is created, delete it. AWS will provision the roles for you and the terraform script will use them from now on.

It would be nice if Terraform documented this somewhere or provided a useful error message. I had to look at the original pull request to find out that this was assumed functionality.

For a full solution, you'd have to create the roles manually and name them exactly as terraform/AWS expects them. I suspect this will be a bad idea and causes issues later on. The roles/policies are quite complex.

@bflad
Copy link
Contributor

bflad commented Jun 27, 2019

Thanks for the heads up, @phillycheeze 👍 This is indeed a documentation issue on our end and as such am marking this issue accordingly.

The DMS service is where the specifically named IAM Role requirement comes from: https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Security.APIRole.html

Here is an example configuration that can accomplish the creation of these roles, using the available AWS managed service policies that automatically receive updates:

data "aws_iam_policy_document" "dms_assume_role" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      identifiers = ["dms.amazonaws.com"]
      type        = "Service"
    }
  }
}

resource "aws_iam_role" "dms-access-for-endpoint" {
  assume_role_policy = "${data.aws_iam_policy_document.dms_assume_role.json}"
  name               = "dms-access-for-endpoint"
}

resource "aws_iam_role_policy_attachment" "dms-access-for-endpoint-AmazonDMSRedshiftS3Role" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSRedshiftS3Role"
  role       = "${aws_iam_role.dms-access-for-endpoint.name}"
}

resource "aws_iam_role" "dms-cloudwatch-logs-role" {
  assume_role_policy = "${data.aws_iam_policy_document.dms_assume_role.json}"
  name               = "dms-cloudwatch-logs-role"
}

resource "aws_iam_role_policy_attachment" "dms-cloudwatch-logs-role-AmazonDMSCloudWatchLogsRole" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSCloudWatchLogsRole"
  role       = "${aws_iam_role.dms-cloudwatch-logs-role.name}"
}

resource "aws_iam_role" "dms-vpc-role" {
  assume_role_policy = "${data.aws_iam_policy_document.dms_assume_role.json}"
  name               = "dms-vpc-role"
}

resource "aws_iam_role_policy_attachment" "dms-vpc-role-AmazonDMSVPCManagementRole" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole"
  role       = "${aws_iam_role.dms-vpc-role.name}"
}

I have submitted a pull request to update the documentation here: #9173

Hope this helps.

@bflad bflad added documentation Introduces or discusses updates to documentation. and removed needs-triage Waiting for first response or review from a maintainer. labels Jun 27, 2019
@bflad bflad self-assigned this Jun 27, 2019
@bflad bflad added this to the v2.17.0 milestone Jun 27, 2019
@phillycheeze
Copy link

Thanks @bflad ! This probably goes without saying, but if aws has already created the roles for you, that code sample won't work since it'll throw an error saying the roles already exist. In that case, it's probably better to go and delete the roles that aws created for you. Just a heads up if anyone runs into this problem.

@leeuw471
Copy link

leeuw471 commented Jul 3, 2019

I am still getting the error the first run, a second apply straight after and it does work.

@ghost
Copy link
Author

ghost commented Nov 3, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation.
Projects
None yet
4 participants