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

removing unfound ecs service from state file #6039

Merged
merged 3 commits into from
Oct 5, 2018

Conversation

barrytam20
Copy link
Contributor

Changes proposed in this pull request:

  • Removing unfound ecs service from statefile instead of throwing an error when running terraform refresh to resolve drift

note about acceptance tests: my corporate aws account is not allowed to perform some actions required to run acceptance tests. below is the output, account id/profile have been scrubbed out

Output from acceptance testing:

make testacc TEST=./aws TESTARGS='-run=TestAccAWSEcsService*'==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSEcsService* -timeout 120m
=== RUN   TestAccAWSEcsServiceDataSource_basic
--- PASS: TestAccAWSEcsServiceDataSource_basic (52.43s)
=== RUN   TestAccAWSEcsService_withARN
--- PASS: TestAccAWSEcsService_withARN (77.58s)
=== RUN   TestAccAWSEcsService_basicImport
--- PASS: TestAccAWSEcsService_basicImport (44.08s)
=== RUN   TestAccAWSEcsService_withUnnormalizedPlacementStrategy
--- PASS: TestAccAWSEcsService_withUnnormalizedPlacementStrategy (48.04s)
=== RUN   TestAccAWSEcsService_withFamilyAndRevision
--- PASS: TestAccAWSEcsService_withFamilyAndRevision (89.89s)
=== RUN   TestAccAWSEcsService_withRenamedCluster
--- PASS: TestAccAWSEcsService_withRenamedCluster (82.52s)
=== RUN   TestAccAWSEcsService_healthCheckGracePeriodSeconds
--- FAIL: TestAccAWSEcsService_healthCheckGracePeriodSeconds (21.41s)
        testing.go:527: Step 2 error: Error applying: 2 errors occurred:
                        * aws_iam_role.ecs_service: 1 error occurred:
                        * aws_iam_role.ecs_service: Error creating IAM Role tf-acc-role-svc-w-hcgps-xvft7vse: AccessDenied: User: arn:aws:sts::aws_account_number:assumed-role/aws_profile is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::aws_account_number:role/tf-acc-role-svc-w-hcgps-xvft7vse
                        status code: 403, request id: 8c8d5c95-c5bf-11e8-9d6d-435ec3fa931d


                        * aws_vpc.main: 1 error occurred:
                        * aws_vpc.main: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 0dbb3119-7f40-4343-82a6-2a2595c01050




=== RUN   TestAccAWSEcsService_withIamRole
--- FAIL: TestAccAWSEcsService_withIamRole (16.24s)
        testing.go:527: Step 0 error: Error applying: 2 errors occurred:
                        * aws_iam_role.ecs_service: 1 error occurred:
                        * aws_iam_role.ecs_service: Error creating IAM Role tf-acc-role-svc-w-iam-role-4yc2quyc: AccessDenied: User: arn:aws:sts::aws_account_number:assumed-role/aws_profile is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::aws_account_number:role/tf-acc-role-svc-w-iam-role-4yc2quyc
                        status code: 403, request id: 977b8482-c5bf-11e8-82b7-9513ec8e5604


                        * aws_elb.main: 1 error occurred:
                        * aws_elb.main: AccessDenied: User: arn:aws:sts::aws_account_number:assumed-role/aws_profile is not authorized to perform: ec2:CreateSecurityGroup
                        status code: 403, request id: 97a02447-c5bf-11e8-ad34-a9caa0ed026f




=== RUN   TestAccAWSEcsService_withDeploymentValues
--- PASS: TestAccAWSEcsService_withDeploymentValues (51.68s)
=== RUN   TestAccAWSEcsService_withLbChanges
--- FAIL: TestAccAWSEcsService_withLbChanges (67.02s)
        testing.go:527: Step 0 error: Error applying: 2 errors occurred:
                        * aws_iam_role.ecs_service: 1 error occurred:
                        * aws_iam_role.ecs_service: Error creating IAM Role tf-acc-role-svc-w-lbc-l9ziw4xb: AccessDenied: User: arn:aws:sts::aws_account_number:assumed-role/aws_profile is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::aws_account_number:role/tf-acc-role-svc-w-lbc-l9ziw4xb
                        status code: 403, request id: c1862791-c5bf-11e8-8720-f72ce63c99cf


                        * aws_elb.main: 1 error occurred:
                        * aws_elb.main: AccessDenied: User: arn:aws:sts::aws_account_number:assumed-role/aws_profile is not authorized to perform: ec2:CreateSecurityGroup
                        status code: 403, request id: d5ab9fec-c5bf-11e8-afec-f737cfce2486




=== RUN   TestAccAWSEcsService_withEcsClusterName
--- PASS: TestAccAWSEcsService_withEcsClusterName (40.68s)
=== RUN   TestAccAWSEcsService_withAlb
--- FAIL: TestAccAWSEcsService_withAlb (29.47s)
        testing.go:527: Step 0 error: Error applying: 2 errors occurred:
                        * aws_iam_role.ecs_service: 1 error occurred:
                        * aws_iam_role.ecs_service: Error creating IAM Role tf-acc-role-svc-w-alb-jarde82x: AccessDenied: User: arn:aws:sts::aws_account_number:assumed-role/aws_profile is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::aws_account_number:role/tf-acc-role-svc-w-alb-jarde82x
                        status code: 403, request id: 06df72a2-c5c0-11e8-9d6d-435ec3fa931d


                        * aws_vpc.main: 1 error occurred:
                        * aws_vpc.main: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 186713db-f3c7-4b8b-a738-a3957b61d227




=== RUN   TestAccAWSEcsService_withPlacementStrategy
--- PASS: TestAccAWSEcsService_withPlacementStrategy (134.49s)
=== RUN   TestAccAWSEcsService_withPlacementConstraints
--- PASS: TestAccAWSEcsService_withPlacementConstraints (51.19s)
=== RUN   TestAccAWSEcsService_withPlacementConstraints_emptyExpression
--- PASS: TestAccAWSEcsService_withPlacementConstraints_emptyExpression (39.85s)
=== RUN   TestAccAWSEcsService_withLaunchTypeFargate
--- FAIL: TestAccAWSEcsService_withLaunchTypeFargate (56.44s)
        testing.go:527: Step 0 error: Error applying: 1 error occurred:
                        * aws_vpc.main: 1 error occurred:
                        * aws_vpc.main: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 845c93fc-0f3e-49fc-8d23-4d2921157e2d




        testing.go:527: Step 0 error: Error applying: 1 error occurred:
                        * aws_vpc.main: 1 error occurred:
                        * aws_vpc.main: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 0b9abc5b-3a8a-45b8-8a68-fc88590c4285




        testing.go:527: Step 0 error: Error applying: 1 error occurred:
                        * aws_vpc.main: 1 error occurred:
                        * aws_vpc.main: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 01bf5345-9953-447f-af31-dd2e7c4b67aa




=== RUN   TestAccAWSEcsService_withLaunchTypeEC2AndNetworkConfiguration
--- FAIL: TestAccAWSEcsService_withLaunchTypeEC2AndNetworkConfiguration (56.57s)
        testing.go:527: Step 0 error: Error applying: 1 error occurred:
                        * aws_vpc.main: 1 error occurred:
                        * aws_vpc.main: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 98fa3fdd-bcef-4080-8d05-3f3917811d28




=== RUN   TestAccAWSEcsService_withDaemonSchedulingStrategy
--- PASS: TestAccAWSEcsService_withDaemonSchedulingStrategy (49.64s)
=== RUN   TestAccAWSEcsService_withReplicaSchedulingStrategy
--- PASS: TestAccAWSEcsService_withReplicaSchedulingStrategy (52.12s)
=== RUN   TestAccAWSEcsService_withServiceRegistries
--- FAIL: TestAccAWSEcsService_withServiceRegistries (21.18s)
        testing.go:527: Step 0 error: Error applying: 1 error occurred:
                        * aws_vpc.test: 1 error occurred:
                        * aws_vpc.test: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 1799a702-d690-4b34-b2d1-42fe65182355




=== RUN   TestAccAWSEcsService_withServiceRegistries_container
--- FAIL: TestAccAWSEcsService_withServiceRegistries_container (21.69s)
        testing.go:527: Step 0 error: Error applying: 1 error occurred:
                        * aws_vpc.test: 1 error occurred:
                        * aws_vpc.test: Error creating VPC: UnauthorizedOperation: You are not authorized to perform this operation.
                        status code: 403, request id: 4704308a-daa5-4902-b28d-d0ba9b30074a




FAIL
exit status 1
FAIL    github.com/terraform-providers/terraform-provider-aws/aws       1108.355s
make: *** [testacc] Error 1

@ghost ghost added size/XS Managed by automation to categorize the size of a PR. service/ecs Issues and PRs that pertain to the ecs service. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. labels Oct 1, 2018
@bflad
Copy link
Contributor

bflad commented Oct 2, 2018

@barrytam20 do you have the error message and/or debug logs from this occurring?

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Oct 2, 2018
@barrytam20
Copy link
Contributor Author

@bflad this is the error i was seeing when i tried to do a terraform refresh. the service existed in the statefile, but was deleted through the AWS console

2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: ---[ REQUEST POST-SIGN ]-----------------------------
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: POST / HTTP/1.1
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Host: ecs.us-east-1.amazonaws.com
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: User-Agent: aws-sdk-go/1.14.26 (go1.9.2; linux; amd64) APN/1.0 HashiCorp/1.0 Terraform/0.11.8-dev
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Content-Length: 125
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: X-Amz-Target: AmazonEC2ContainerServiceV20141113.DescribeServices
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Accept-Encoding: gzip
2018-09-29T22:52:10.917Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: 
2018-09-29T22:52:11.242Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: -----------------------------------------------------
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: 2018/09/29 22:52:11 [DEBUG] [aws-sdk-go] DEBUG: Response ecs/DescribeServices Details:
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: ---[ RESPONSE ]--------------------------------------
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: HTTP/1.1 200 OK
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Connection: close
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Content-Length: 122
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Content-Type: application/x-amz-json-1.1
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: Date: Sat, 29 Sep 2018 22:52:13 GMT
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: X-Amzn-Requestid: 4eba7580-c43a-11e8-9e81-79369c8f1b51
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: 
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: 
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: -----------------------------------------------------
2018-09-29T22:52:11.435Z [DEBUG] plugin.terraform-provider-aws_v1.28.0_x4: 2018/09/29 22:52:11 [DEBUG] [aws-sdk-go] {"failures":[{"arn":"arn:aws:ecs:us-east-1:aws_account_id:service/foo-dev-a","reason":"MISSING"}],"services":[]}
2018/09/29 22:52:11 [ERROR] root.foo-service: eval: *terraform.EvalRefresh, err: aws_ecs_service.ecs-svc: No ECS service found: "arn:aws:ecs:us-east-1:aws_account_id:service/foo-dev-a"
2018/09/29 22:52:11 [ERROR] root.foo-service: eval: *terraform.EvalSequence, err: aws_ecs_service.ecs-svc: No ECS service found: "arn:aws:ecs:us-east-1:aws_account_id:service/foo-dev-a"
2018/09/29 22:52:11 [TRACE] [walkRefresh] Exiting eval tree: module.foo-service.aws_ecs_service.ecs-svc```

@bflad bflad added bug Addresses a defect in current functionality. and removed waiting-response Maintainers are waiting on response from community or contributor. labels Oct 2, 2018
@barrytam20
Copy link
Contributor Author

hey @bflad is there anything i can do to help with this PR? I haven't really found any other resources with tests that remove unfound resources from the state file

@bflad
Copy link
Contributor

bflad commented Oct 5, 2018

@barrytam20 check out _disappears acceptance tests 😄 The gist is to:

  • Config: : Apply a basic configuration
  • Check: : Use a TestCheckFunc to delete from the API
  • Add ExpectNonEmptyPlan: true

See also the testing added in #5967

@ghost ghost added size/S Managed by automation to categorize the size of a PR. and removed size/XS Managed by automation to categorize the size of a PR. labels Oct 5, 2018
@barrytam20
Copy link
Contributor Author

barrytam20 commented Oct 5, 2018

@bflad - pushed up test TestAccAWSEcsService_disappears

make testacc TEST=./aws TESTARGS='-run=TestAccAWSEcsService_disappears'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSEcsService_disappears -timeout 120m
=== RUN   TestAccAWSEcsService_disappears
--- PASS: TestAccAWSEcsService_disappears (23.10s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       45.025s

@bflad bflad added this to the v1.40.0 milestone Oct 5, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Thanks so much for reporting and fixing this @barrytam20! Some minor adjustments post-merge to the testing noted below. ECS will keep INACTIVE services around for awhile before reaping them asynchronously, which actually doesn't trigger the condition of them being missing in the out.Services from DescribeServices(). We don't want to sit around and guess how long to wait for that process to occur so keeping the nonexistent import test in there actually is the easiest way to capture the same behavior you originally saw. That said, the addition of the _disappears test is actually super helpful for verifying that INACTIVE services are recreated, which previously wasn't being tested. 👍

At some point we should probably clean up that retry logic there, its very messy. 😅

22 tests passed (all tests)
--- PASS: TestAccAWSEcsService_withUnnormalizedPlacementStrategy (7.01s)
--- PASS: TestAccAWSEcsService_basicImport (7.92s)
--- PASS: TestAccAWSEcsService_disappears (8.71s)
--- PASS: TestAccAWSEcsService_withPlacementConstraints_emptyExpression (9.28s)
--- PASS: TestAccAWSEcsService_withEcsClusterName (9.28s)
--- PASS: TestAccAWSEcsService_withReplicaSchedulingStrategy (13.36s)
--- PASS: TestAccAWSEcsService_withRenamedCluster (20.26s)
--- PASS: TestAccAWSEcsService_withIamRole (24.88s)
--- PASS: TestAccAWSEcsService_withServiceRegistries (18.67s)
--- PASS: TestAccAWSEcsService_withDeploymentValues (30.90s)
--- PASS: TestAccAWSEcsService_withPlacementConstraints (31.47s)
--- PASS: TestAccAWSEcsServiceDataSource_basic (32.33s)
--- PASS: TestAccAWSEcsService_withFamilyAndRevision (33.50s)
--- PASS: TestAccAWSEcsService_withARN (33.71s)
--- PASS: TestAccAWSEcsService_withDaemonSchedulingStrategy (40.96s)
--- PASS: TestAccAWSEcsService_withServiceRegistries_container (46.65s)
--- PASS: TestAccAWSEcsService_withPlacementStrategy (62.98s)
--- PASS: TestAccAWSEcsService_withLaunchTypeEC2AndNetworkConfiguration (71.74s)
--- PASS: TestAccAWSEcsService_withLbChanges (214.00s)
--- PASS: TestAccAWSEcsService_healthCheckGracePeriodSeconds (228.75s)
--- PASS: TestAccAWSEcsService_withAlb (234.15s)
--- PASS: TestAccAWSEcsService_withLaunchTypeFargate (307.47s)

ImportStateId: fmt.Sprintf("%s/nonexistent", clusterName),
ImportState: true,
ImportStateVerify: false,
ExpectError: regexp.MustCompile(`No ECS service found`),
Copy link
Contributor

Choose a reason for hiding this comment

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

We actually want to keep this test as its currently the only way we can verify the condition you were originally seeing. We just needed to grab something out of the new error message from import failing (properly!):

--- FAIL: TestAccAWSEcsService_basicImport (43.46s)
    testing.go:520: Step 2, expected error:

        1 error occurred:
        	* aws_ecs_service.jenkins (import id: tf-acc-cluster-svc-ye9z6h7j/nonexistent): 1 error occurred:
        	* import aws_ecs_service.jenkins result: nonexistent: import aws_ecs_service.jenkins (id: nonexistent): Terraform detected a resource with this ID doesn't
        exist. Please verify the ID is correct. You cannot import non-existent
        resources using Terraform import.





        To match:

        No ECS service found

I went with:

ExpectError:       regexp.MustCompile(`Please verify the ID is correct`),

Force: aws.Bool(true),
}

_, err := conn.DeleteService(input)
Copy link
Contributor

Choose a reason for hiding this comment

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

It turns out the ECS service needs some additional checking to ensure its gone INACTIVE looking at its Delete function:

func testAccCheckAWSEcsServiceDisappears(service *ecs.Service) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		conn := testAccProvider.Meta().(*AWSClient).ecsconn

		input := &ecs.DeleteServiceInput{
			Cluster: service.ClusterArn,
			Service: service.ServiceName,
			Force:   aws.Bool(true),
		}

		_, err := conn.DeleteService(input)

		if err != nil {
			return err
		}

		// Wait until it's deleted
		wait := resource.StateChangeConf{
			Pending:    []string{"ACTIVE", "DRAINING"},
			Target:     []string{"INACTIVE"},
			Timeout:    10 * time.Minute,
			MinTimeout: 1 * time.Second,
			Refresh: func() (interface{}, string, error) {
				resp, err := conn.DescribeServices(&ecs.DescribeServicesInput{
					Cluster:  service.ClusterArn,
					Services: []*string{service.ServiceName},
				})
				if err != nil {
					return resp, "FAILED", err
				}

				return resp, aws.StringValue(resp.Services[0].Status), nil
			},
		}

		_, err = wait.WaitForState()

		return err
	}
}

@bflad bflad merged commit 12f99d5 into hashicorp:master Oct 5, 2018
bflad added a commit that referenced this pull request Oct 5, 2018
@bflad
Copy link
Contributor

bflad commented Oct 10, 2018

This has been released in version 1.40.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 2, 2020

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 Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/ecs Issues and PRs that pertain to the ecs service. size/S Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants