func lambdaPermissionExists(rs *terraform.ResourceState, conn *lambda.Lambda) (*LambdaPolicyStatement, error) { params := &lambda.GetPolicyInput{ FunctionName: aws.String(rs.Primary.Attributes["function_name"]), } if v, ok := rs.Primary.Attributes["qualifier"]; ok { params.Qualifier = aws.String(v) } resp, err := conn.GetPolicy(params) if err != nil { return nil, fmt.Errorf("Lambda policy not found: %q", err) } if resp.Policy == nil { return nil, fmt.Errorf("Received Lambda policy is empty") } policyInBytes := []byte(*resp.Policy) policy := LambdaPolicy{} err = json.Unmarshal(policyInBytes, &policy) if err != nil { return nil, fmt.Errorf("Error unmarshalling Lambda policy: %s", err) } return findLambdaPolicyStatementById(&policy, rs.Primary.ID) }
func isLambdaPermissionGone(rs *terraform.ResourceState, conn *lambda.Lambda) error { params := &lambda.GetPolicyInput{ FunctionName: aws.String(rs.Primary.Attributes["function_name"]), } if v, ok := rs.Primary.Attributes["qualifier"]; ok { params.Qualifier = aws.String(v) } resp, err := conn.GetPolicy(params) if awsErr, ok := err.(awserr.Error); ok { if awsErr.Code() == "ResourceNotFoundException" { // no policy found => all statements deleted return nil } } if err != nil { return fmt.Errorf("Unexpected error when checking existence of Lambda permission: %s\n%s", rs.Primary.ID, err) } policyInBytes := []byte(*resp.Policy) policy := LambdaPolicy{} err = json.Unmarshal(policyInBytes, &policy) if err != nil { return fmt.Errorf("Error unmarshalling Lambda policy (%s): %s", *resp.Policy, err) } state, err := findLambdaPolicyStatementById(&policy, rs.Primary.ID) if err != nil { // statement not found => deleted return nil } return fmt.Errorf("Policy statement expected to be gone (%s):\n%s", rs.Primary.ID, *state) }